串口接收不到数据

羽熙 羽熙 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
串口和单片机通信,以前可以接收到数据,最近收不到了;有时调试的时候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
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
羽熙
控件 1,453 成员 41,351 内容