vb与RS485通讯一段时间后停止读数,何因?
前几天开了个帖,请教各位朋友VB如何跟RS485通讯的问题,最后拿到正确的仪表通讯协议后,数据现在读取出来了,只是有个问题:运行最初会实时不断的把数据读取出来(因为读取的流量是不断变化的,所以通过窗口的数据变化看得出来,一直在读取),但是过一会儿,时间不定,我大概数了下数据变化的次数,有时候是十几次,有时候是二十几次,有时候是四十几次,最后数据就停止不动了,代表通讯中断,或者发送数据后仪表未返回数据,未发现程序有什么问题,不知道是什么原因。如果将程序关闭后重启,又可以开始读数了,不过效果跟之前的一样,读到一定时间就停止了。
下面将代码发布上来,望高手朋友指点指点:
——————————————————————————————————————————————
Option Explicit
Private xz As Integer 'xz=0,则寻址表1,读表1数据
'xz=1,则寻址表2,读表2数据
Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
MSComm1.InputLen = 0
MSComm1.SThreshold = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
End Sub
Private Sub MSComm1_OnComm()
Dim indata() As Byte
Dim llb(3) As Byte '读取流量字节
Dim i As Integer
Dim llf As Single '流量浮点数
Dim ds As Single '带速
Select Case MSComm1.CommEvent
Case comEventRxOver
MSComm1.InBufferCount = 0
Case comEvReceive
indata = MSComm1.Input
llb(3) = indata(0)
llb(2) = indata(1)
llb(1) = indata(2)
llb(0) = indata(3)
CopyMemory ByVal VarPtr(llf), ByVal VarPtr(llb(0)), 4
If xz = 0 Then
Text1.Text = llf
Text2.Text = indata(10)
xz = 1
Else
Text3.Text = llf
Text4.Text = indata(10)
xz = 0
End If
End Select
End Sub
Private Sub Timer1_Timer() 'timer1的interval属性值为400
Dim od(0) As Byte
If xz = 0 Then
od(0) = &H21
Else
od(0) = &H22
End If
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.RThreshold = 16 '所要接收的数据长度
MSComm1.Output = od
End Sub
[解决办法]