receive_char = receive_char & MSComm1.Input 处改下
if MSComm1.InBufferCount = 0 then ‘这里要注意,需要保证接受到完整的数据
Timer1.Enabled = True
exit sub
end if
receive_char = MSComm1.Input
考虑你采用的是查询方式接受数据,定时器和发送端不一定配合的好,所以在接受端需要添加纠错语句,纠正“接受到没有发送完成时的数据也进行解析”的错误。过程你自己根据你自己发送数据的规律去写,可以判断接受的字符LEN或者你自己写个小协议加个校验码什么的?判断数据接受不正确就延时等待下次查询。
查询方式在通信的实时性上稍微差点,建议换成中断加查询的方式。
Private Sub Form_Load() '初使化
Timer1.Interval = 100
Timer1.Enabled = True
MSComm1.CommPort = 1
MSComm1.Settings = "4800,n,8,1"
MSComm1.RThreshold = 0
MSComm1.SThreshold = 0
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.InputMode = 0
Text19 = ""
Text20 = ""
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
End Sub
Private Sub Timer1_Timer() '定时器定时100MS
Dim x, y, z As Integer
Dim redchar
Dim n, m As Integer
Dim receive_char As String
Dim intdate
'Timer1.Enabled = False
receive_char = receive_char & MSComm1.Input
x = InStr(1, receive_char, "a")
y = InStr(1, receive_char, "b")
z = InStr(1, receive_char, "c")
redchar = Left(receive_char, z)
If x <> 0 Then
intdate = Val(Mid(redchar, x + 1, y - x - 1)) '分解字符
Text19.Text = CStr(intdate)
intdate = Val(Mid(redchar, y + 1, z - y - 1))
Text20.Text = CStr(intdate)
End If
'Timer1.Enabled = True
End Sub
你跟踪不就可以了。
出错时,难道不定位到出错的地方?你看一下就知道哪个地方错了赛。
请问两路温度计分别对应两路串口,还是一路串口