|
正在为单片机做上位机程序。单片机连续发FF030A00000B441703069C33C0AA这样的字符串(以FF开头,AA结束,固定长度)。请问接收程序如何写?
收到的数据:FF 1 1 0 0 A4 3A 8 4B 41 8C 3B 18 0 2A 0 0 0 34 0 AA FF 1 1 0 0 A4 3A 8 4B 41 8C 3B 18 0 2A 0 0 0 34 0 AA FF 1 1 0 0 A4 3A 8 4B 41 8C 3B 18 0 2A 0 0 0 34 0 AA FF 1 1 0 0 A4 3A 8 4B 41 8C 3B 18 0 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0 FF 1 4B 41 0 0
开始的时候数据接收正常,但一会就变成上面这样了。。。。
程序设置:
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "9600,N,8,1"
.InBufferSize = 1024 '原来为19
.RThreshold = 1 '接收1字节触发oncomm事件
.InputMode = comInputModeBinary
.InputLen = 1 '输入长度为19
.InBufferCount = 0 '清除接收缓冲区
End With
'打开端口
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
If Err Then
MsgBox (Err.Description)
Exit Sub
End If
End If
End Sub
Private Sub MSComm1_OnComm()
Dim S() As Byte
Dim SS(1024) As Byte
Static N As Long
Static T As Variant
Dim intInputLen As Integer
If (MSComm1.CommEvent = comEvReceive) Then
S = MSComm1.Input '只要有数据就收进来,哪怕只是一个
T = Timer
For i = 0 To UBound(S)
'一个数据包可能产生若干个oncomm事件
If Check1.Value = 1 And Len(Text1.Text)> 2000 Then Text1.Text = ""
Text1.Text = Text1.Text & Right(Hex(S(i)), 3) + " "
SS(N + i) = S(i) '接收数据包缓存于SS()
N = N + UBound(S)
Next i
' MSComm1.InBufferCount = 0
End If
End Sub
请各位高手指点
本贴被 kunzhe 编辑过,最后修改时间:2008-12-11,14:05:46. |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|