关于VB接收串口来的数据
使用VB编了一个接收串口数据的程序,用来接收下位机每次发来的4byte数据,大约每两秒发来一次,但VB运行后仅显示一个字节,且显示的和用“串口助手”显示的不一致Priva...
使用VB编了一个接收串口数据的程序,用来接收下位机每次发来的4byte数据,大约每两秒发来一次,但VB运行后仅显示一个字节,且显示的和用“串口助手”显示的不一致
Private Sub InitialPort(SerialPort As String, BaudRate As String, ParityBit As String, Databit As String, StopBit As String)
On Error GoTo Errline
MSComm.CommPort = Val(Mid(SerialPort, 4, Len(SerialPort) - 3))
MSComm.Settings = BaudRate & "," & Left(ParityBit, 1) & "," & Databit & "," & StopBit
MSComm.InBufferSize = 1024
MSComm.OutBufferSize = 4096
MSComm.InBufferCount = 0
MSComm.OutBufferCount = 0
MSComm.SThreshold = 1
MSComm.RThreshold = 1
MSComm.InputLen = 4
MSComm.InputMode = comInputModeBinary
Exit Sub
Errline:
If Err.Number <> 0 Then
MsgBox CStr(Err.Number) + "" + Err.Description, vbOKOnly + vbInformation, ""
End If
Err.Clear
Private Sub MScomm_oncomm()
Dim ByteReceiveData() As Byte
Select Case MSComm.CommEvent
Case comEvReceive
ByteReceiveData() = MSComm.Input
End Select
Text1.Text = ByteReceiveData(0)
Text2.Text = MSComm.InBufferCount
End Sub
如果显示 ByteReceiveData(1),则提示“下标越界”,且MSComm.InBufferCount为0
求助! 展开
Private Sub InitialPort(SerialPort As String, BaudRate As String, ParityBit As String, Databit As String, StopBit As String)
On Error GoTo Errline
MSComm.CommPort = Val(Mid(SerialPort, 4, Len(SerialPort) - 3))
MSComm.Settings = BaudRate & "," & Left(ParityBit, 1) & "," & Databit & "," & StopBit
MSComm.InBufferSize = 1024
MSComm.OutBufferSize = 4096
MSComm.InBufferCount = 0
MSComm.OutBufferCount = 0
MSComm.SThreshold = 1
MSComm.RThreshold = 1
MSComm.InputLen = 4
MSComm.InputMode = comInputModeBinary
Exit Sub
Errline:
If Err.Number <> 0 Then
MsgBox CStr(Err.Number) + "" + Err.Description, vbOKOnly + vbInformation, ""
End If
Err.Clear
Private Sub MScomm_oncomm()
Dim ByteReceiveData() As Byte
Select Case MSComm.CommEvent
Case comEvReceive
ByteReceiveData() = MSComm.Input
End Select
Text1.Text = ByteReceiveData(0)
Text2.Text = MSComm.InBufferCount
End Sub
如果显示 ByteReceiveData(1),则提示“下标越界”,且MSComm.InBufferCount为0
求助! 展开
3个回答
展开全部
Private Sub MScomm_oncomm()
Dim ByteReceiveData() As Byte
Select Case MSComm.CommEvent
Case comEvReceive
ByteReceiveData() = MSComm.Input
Text1.Text = ByteReceiveData(0) '这两行要放到这里,也就是说要在接收到数据后再显示
Text2.Text = MSComm.InBufferCount '而不是不管有没有数据都显示
End Select
End Sub
Dim ByteReceiveData() As Byte
Select Case MSComm.CommEvent
Case comEvReceive
ByteReceiveData() = MSComm.Input
Text1.Text = ByteReceiveData(0) '这两行要放到这里,也就是说要在接收到数据后再显示
Text2.Text = MSComm.InBufferCount '而不是不管有没有数据都显示
End Select
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询