我现在有换了一种写法,如下:
Private Sub Timer2_Timer()
Dim OutByte(0) As Byte
Dim InByte() As Byte
Dim RevLen As Integer
Dim ss As String
Dim i As Integer
Timer2.Enabled = False
MyCom.Settings = "9600,M,8,1"
OutByte(0) = &H0
MyCom.Output = OutByte
MyCom.Settings = "9600,S,8,1"
InByte = MyCom.Input
For i = 0 To UBound(InByte)
ss = ss & Hex(InByte(i)) & " " '每收到6字节定义为一个字符串
Next i
If ss <> "" Then
List1.AddItem (ss) '添加收到的字符串到显示的表中
ss = "" '该字符串清空,供下次使用
End If
Timer2.Enabled = True '打开定时器
End Sub
定时周期为15
接收的数据的发收同步稍好点,但是6字节的数据很多时候只有5字节,出错的数据还是有不少的。改协议我是不指望了,希望有什么好办法可以解决
因为有人原先用pb没改协议也能很好的工作,所以我要vb最好也能工作,现在我用过轮询,可以收到正确数据,但是极不稳定,有段时间连出来几个数据,有时好一会一个数据也出不来,而且数据时不时的还会出现3F,定时器的定时周期也和数据出现的频率有关。程序如下:
Private Sub Timer2_Timer()
Dim OutByte(0) As Byte
Dim InByte() As Byte
Dim RevLen As Integer
Dim ss As String
Dim i As Integer
Timer2.Enabled = False
MyCom.Settings = "9600,M,8,1"
OutByte(0) = &H0
MyCom.Output = OutByte
MyCom.Settings = "9600,S,8,1"
If MyCom.InBufferCount > 0 Then
RevLen = MyCom.InBufferCount
InByte = MyCom.Input
If RevLen = 6 Then
For i = 0 To UBound(InByte)
ss = ss & Hex(InByte(i)) & " " '每收到6字节定义为一个字符串
Next i
List1.AddItem (ss) '添加收到的字符串到显示的表中
ss = "" '该字符串清空,供下次使用
End If
End If
Timer2.Enabled = True '打开定时器
End Sub
我是第一次用轮询方法处理串口,不是很清楚应该的效果。定时器周期为50