VB MSComm 串口通讯 数据丢失
发送的代码如下PrivateSubCommand3_Click()DimbData(5)AsBytebData(0)=&HA0bData(1)=&H3bData(2)=&...
发送的代码如下
Private Sub Command3_Click()
Dim bData(5) As Byte
bData(0) = &HA0
bData(1) = &H3
bData(2) = &H82
bData(3) = &H4
bData(4) = &HD7
MSComm1.Output = bData
End Sub
接收的代码如下
Private Sub MSComm1_OnComm()
Dim Inbyte() As Byte
'Dim Showtext1 As String
Dim i As Integer
Dim rectext As String
Select Case MSComm1.CommEvent
Case comEvReceive
Inbyte = MSComm1.Input
Label1.Caption = UBound(Inbyte)
For i = LBound(Inbyte) To UBound(Inbyte) 'i循环从数组Inbyte的下届到上届
rectext = rectext & Hex(Inbyte(i)) & Chr(32)
Next i
End Select
Text2.Text = rectext
End Sub
正确的返回值应该是
E4 03 82 05 92
但是现在我接收到的值却总是丢失
有时丢失第一位,有时丢失前两位,甚至只接收到了最后一个数据
我在串口监视软件中看到的返回数据是没有问题的,现在问题到底出在哪里呢?
打开串口
Private Sub Command1_Click()
Dim mag As String
On Error GoTo strmsg1
MSComm1.CommPort = 3
MSComm1.InputMode = 1
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0 '读出接收缓冲区的所有内容
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.InputMode = comInputModeBinary '以二进制格式读取接收缓冲区
Exit Sub
strmsg1:
mag = "串口打开失败"
Call MsgBox(mag, vbOKOnly, "Error:串口")
Exit Sub
End Sub 展开
Private Sub Command3_Click()
Dim bData(5) As Byte
bData(0) = &HA0
bData(1) = &H3
bData(2) = &H82
bData(3) = &H4
bData(4) = &HD7
MSComm1.Output = bData
End Sub
接收的代码如下
Private Sub MSComm1_OnComm()
Dim Inbyte() As Byte
'Dim Showtext1 As String
Dim i As Integer
Dim rectext As String
Select Case MSComm1.CommEvent
Case comEvReceive
Inbyte = MSComm1.Input
Label1.Caption = UBound(Inbyte)
For i = LBound(Inbyte) To UBound(Inbyte) 'i循环从数组Inbyte的下届到上届
rectext = rectext & Hex(Inbyte(i)) & Chr(32)
Next i
End Select
Text2.Text = rectext
End Sub
正确的返回值应该是
E4 03 82 05 92
但是现在我接收到的值却总是丢失
有时丢失第一位,有时丢失前两位,甚至只接收到了最后一个数据
我在串口监视软件中看到的返回数据是没有问题的,现在问题到底出在哪里呢?
打开串口
Private Sub Command1_Click()
Dim mag As String
On Error GoTo strmsg1
MSComm1.CommPort = 3
MSComm1.InputMode = 1
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0 '读出接收缓冲区的所有内容
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.InputMode = comInputModeBinary '以二进制格式读取接收缓冲区
Exit Sub
strmsg1:
mag = "串口打开失败"
Call MsgBox(mag, vbOKOnly, "Error:串口")
Exit Sub
End Sub 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询