VB comm接收数据首字节丢失疑难
大家好!
情况是这样的:
笔记本用的usb转485与arm通信,modbus协议(不了解也无所谓)
发送和接收基本没问题,问题是:
如果arm发送小于8字节,接收没问题
如果超过8个字节,第一个字节丢失,其余的可以收到
试了好多办法,还是不行
1.可能是程序问题,代码如下,大侠帮忙建议一下吧
2.如果是usb转串口设备的问题,有什么办法通过软件解决?
Private Sub Command4_Click()
ReDim t.data(2) As Byte
t.data(0) = 49
t.data(1) = 78
Call uartsend(1, 3, 16, 0, 0, 11, 0, t.data())
end sub
Public Function uartsend(ByVal add As Byte, ByVal funcode As Byte, _
ByVal regah As Byte, ByVal regal As Byte, ByVal regnumh As Byte, _
ByVal regnuml As Byte, ByVal datalen As Byte, ByRef data() As Byte)
On Error GoTo uarterr
Dim pack() As Byte
ReDim pack(0 To 8) As Byte
pack(0) = add
pack(1) = funcode
pack(2) = regah
pack(3) = regal
pack(4) = regnumh
pack(5) = regnuml
pack(6) = CRC16(pack, 6) Mod 256
pack(7) = CRC16(pack, 6) \ 256
form1.mscomm1.commport=4
Form1.MSComm1.InputMode = comInputModeBinary
Form1.MSComm1.InBufferSize = 512
Form1.MSComm1.OutBufferSize = 512
Form1.MSComm1.InBufferCount = 0
Form1.MSComm1.OutBufferCount = 0
Form1.MSComm1.SThreshold = 0
Form1.MSComm1.RThreshold = 1
Form1.MSComm1.InputLen = 0
Form1.MSComm1.Settings = "19200,N,8,1"
Form1.MSComm1.PortOpen = True
Form1.MSComm1.Output = pack
Exit Function
uarterr:
msg$ = "err号" '错误显示
Title$ = "串口调试助手"
X = MsgBox(msg$, 48, Title$) '48标示显示警告图标
End Function
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim RecDatas() As Byte
If (MSComm1.CommEvent = comEvReceive) Then
Ilens = MSComm1.InBufferCount
MSComm1.InputLen = Ilens
RecDatas = MSComm1.Input
For i = 0 To UBound(RecDatas)
text2.Text = text2.Text & Hex(RecDatas(i)) & " "
Next i
End If
End Sub
关键的代码就如上了,现象如下:
比如应该接收到 1 3 8 0 0 0 0 0 0 0 0 95 D7
实际text2显示为 3 8 0 0 0 0 0 0 0 0 95 D7
手动测试过接收数据的crc,arm确实将首字节发出来了,但是接收出现问题
真心求教,需要大家的经验,头疼了一晚上了
[解决办法]
http://download.csdn.net/source/1262066