7,763
社区成员
发帖
与我相关
我的任务
分享
Private Sub Timer1_Timer()
'采用轮循法采集数据
Dim inx() As Byte
Dim strTemp As String
Dim strTemp1 As String
Dim ReceivedLen As Integer
Timer1.Enabled = False '关闭定时器
If MSComm1.InBufferCount > 0 Then
ReceivedLen = MSComm1.InBufferCount
inx = MSComm1.Input
For i = 0 To UBound(inx)
strTemp1 = Hex(inx(i))
If Len(strTemp1) > 1 Then
strTemp = strTemp & strTemp1 & " "
Else
strTemp = strTemp & "0" & strTemp1 & " "
End If
Next i
Text1.Text = Text1.Text & Format(Second(Now), "00") & Right(Format(Str(Timer), "0.00"), 3) & " " & strTemp & vbCrLf
Text1.SelStart = Len(Text1.Text)
End If
Timer1.Enabled = True '打开定时器
Label1.Caption = Now()
End Sub
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
'MSComm1属性设置,以下只是举例,请根据你的实际情况写
With MSComm1
.CommPort = 1
.Settings = "9600,E,7,1"
.RThreshold = 0 '这个必须为:0,表示不触发OnComm的接收事件
.InBufferCount = 0
.OutBufferCount = 0
.InBufferSize = 1024
.InputMode = comInputModeText
If Not .PortOpen Then .PortOpen = True
End With
Timer1.Interval = 5000 '设置Timer间隔时间
Timer1.Enabled = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MSComm1.PortOpen Then MSComm1.PortOpen = False
End Sub
Private Sub Timer1_Timer()
Dim strIn As String
Dim strOut As String
Dim lngP As Long
strOut = "aaaa" '此处输入你的输出内容,如果InputMode是:comInputModeBinary的话,那就要发送数组了
'将命令发出,然后进入等待接收过程,当收到结束字符,或等待时间超过100ms,时退出循环,结束等待。
MSComm1.Output = strOut
lngP = GetTickCount
Do
DoEvents
strIn = strIn & MSComm1.Input
Loop Until GetTickCount - lngP > 100 Or InStr(1, strIn, "结束字符", vbTextCompare) <> 0
Debug.Print strIn
End Sub