串口接收不到数据
串口和单片机通信,以前可以接收到数据,最近收不到了;有时调试的时候commevent值为2 ,可以看到input里面有数据,但是用一个string字符串取出来后就什么都没有了;有时 Select Case MSComm1.CommEvent可以看到有数据返回,经过case Input 里面的数据就变为空的,这是怎么回事,要怎么解决;有时候也不触发commevent事件
Private Sub Combo1_Click()
Dim Portstr As String
Dim keywords As String
Dim tmp As Long
Dim ret As Long
Dim I, JJ As Integer
Portstr = Combo1.Text
For JJ = 1 To Len(Portstr)
If Asc(Mid(Portstr, JJ, 1)) >= 48 And Asc(Mid(Portstr, JJ, 1)) <= 57 Then
comport = comport & Mid(Portstr, JJ, 1)
Else
I = I + Val(comport)
comport = ""
End If
Next
MSComm1.CommPort = comport
MSComm1.settings = "9600,n,8,1" '设置串口1的参数
MSComm1.InputMode = comInputModeBinary '接收二进制数据
MSComm1.InBufferSize = 2048
MSComm1.RThreshold = 1 ' MSComm1.Input = 0
MSComm1.InputLen = 0
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True '打开通信端口
MsgBox ("端口已打开")
Else
MsgBox ("请重新选择端口号"), 16
End If
If MSComm1.PortOpen = True Then
ret = mySearching(BROADCAST, 0)
If ret >= 0 Then
Shape1.FillColor = vbGreen
End If
End If
End Sub
Private Sub MSComm1_OnComm()
Dim J As Long
Dim I As Integer
Select Case MSComm1.CommEvent
Case 2
strBuff = MSComm1.Input
BytReceived() = strBuff
For I = 0 To UBound(BytReceived)
Print BytReceived(I)
Next
jieshou
'数据处理代码
End Select
End Sub
Private Sub Combo1_Click()
Dim Portstr As String
Dim keywords As String
Dim tmp As Long
Dim ret As Long
Dim I, JJ As Integer
Portstr = Combo1.Text
For JJ = 1 To Len(Portstr)
If Asc(Mid(Portstr, JJ, 1)) >= 48 And Asc(Mid(Portstr, JJ, 1)) <= 57 Then
comport = comport & Mid(Portstr, JJ, 1)
Else
I = I + Val(comport)
comport = ""
End If
Next
MSComm1.CommPort = comport
MSComm1.settings = "9600,n,8,1" '设置串口1的参数
MSComm1.InputMode = comInputModeBinary '接收二进制数据
MSComm1.InBufferSize = 2048
MSComm1.RThreshold = 1 ' MSComm1.Input = 0
MSComm1.InputLen = 0
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True '打开通信端口
MsgBox ("端口已打开")
Else
MsgBox ("请重新选择端口号"), 16
End If
If MSComm1.PortOpen = True Then
ret = mySearching(BROADCAST, 0)
If ret >= 0 Then
Shape1.FillColor = vbGreen
End If
End If
End Sub
Private Sub MSComm1_OnComm()
Dim J As Long
Dim I As Integer
Select Case MSComm1.CommEvent
Case 2
strBuff = MSComm1.Input
BytReceived() = strBuff
For I = 0 To UBound(BytReceived)
Print BytReceived(I)
Next
jieshou
'数据处理代码
End Select
End Sub
...全文
相关推荐
51单片机串口通信的发送与接收 字符串
还有人在发送数据之前,并没有关闭串口中断,当 TI = 1 时,是可以进入中断程序的。但是,却在中断函数中,将 TI 清零!这样,在主函数中的while(TI ==0);,将永远等不到发送结束的标志。
单片机串口接收字符串
在实际开发中,编写串口通信程序时需要注意以下几点:1. 串口中断的开放和关闭:在发送数据之前不要关闭中断,否则会导致接收不到数据。2.
搞定单片机多字节串口接收.docx
处理多设备通信:需要考虑到在多设备通信中,属于其他设备的帧数据最后一位可能是 aa(或者最后两位为 aa 55,或者最后 3 位为 aa 55 板选),下一次通信的数据就接收不到了。4.
stm32 HAL库 串口DMA接收不定长度数据及粘包处理.docx
- 在实际应用中,需处理可能出现的数据长度错误、找不到数据头等问题,确保数据正确解析。
搞定单片机多字节串口接收.pdf
在多设备通信中,属于其他设备的帧数据最后一位是 aa 的情况下,下一次通信的数据就接收不到。


控件
