串口接收数据问题,请高手解决,急!!!
串口通讯程序,接收数据时出了问题
我的部分代码如下,text5可以正常输出“01 04 2C 00 0D 00 06 00 07 00 17 00 0C 00 31 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 FE 70 00 00 FE 70 03 0C ” 但text1输出12(尾数0C的值),text2输出为7009,text3,4都无值,text6为112.,text7为7070 .
。Private Sub Command3_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
MSComm1.Output = Chr(&H1) & Chr(&H4) & Chr(&H70) & Chr(&H0) & Chr(&H0) & Chr(&H16) & Chr(&H6B) & Chr(&H4)
End Sub
Private Sub MSComm1_OnComm()
Dim bytreceivedata() As Byte
Dim i As Integer
Dim j As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
bytreceivedata() = MSComm1.Input
For i = 0 To UBound(bytreceivedata)
frame(i) = Hex(bytreceivedata(i))
If Len(frame(i)) = 1 Then
frame(i) = "0" + Hex(bytreceivedata(i))
End If
Next i
For j = 0 To UBound(bytreceivedata)
Text5.Text = Text5.Text & frame(j) & " "
Next j
Text1.Text = Val("&H" & frame(16))
Text2.Text = Text2.Text & frame(16)
Text3.Text = Val("&H" & frame(32))
Text4.Text = Text4.Text & frame(32)
Text6.Text = Val("&H" & frame(30))
Text7.Text = Text7.Text & frame(30)
End Select
End Sub
[解决办法]
SComm1.CommEvent触发时,数据并不是一下全过来了,所以你可以加以控制
Private Sub MSComm1_OnComm()
Dim bytreceivedata() As Byte
Dim i As Integer
Dim j As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
If MSComm1.InBufferCount >= 30 Then
bytreceivedata() = MSComm1.Input
For i = 0 To UBound(bytreceivedata)
frame(i) = Hex(bytreceivedata(i))
If Len(frame(i)) = 1 Then
frame(i) = "0" + Hex(bytreceivedata(i))
End If
Next i
For j = 0 To UBound(bytreceivedata)
Text5.Text = Text5.Text & frame(j) & " "
Next j
Text1.Text = Val("&H" & frame(16))
Text2.Text = Text2.Text & frame(16)
Text3.Text = Val("&H" & frame(32))
Text4.Text = Text4.Text & frame(32)
Text6.Text = Val("&H" & frame(30))
Text7.Text = Text7.Text & frame(30)
End Select
end if
End Sub