需对接收字符串是否符合要求作判断,在窗体初始化或打开串口接收代码中需对MSComm1的属性RThreshold赋值,最好与接收的字符串长等值,这样不易出错:
Private Sub MSComm1_OnComm()
Dim txtBuf As String
Dim i As Integer
Dim c As String
Dim wl As Integer
Dim l As Integer
With MSComm1
Select Case .CommEvent
Case comEvReceive
txtBuf = .Input
For i = 1 To Len(txtBuf)
c = Mid(txtBuf, i, 1)
txtResponse = txtResponse & c '输出显示
Next i
If Mid(txtResponse, 1, 1) = "$" Then '对接收字符串是否符合要求作判断,本句拟增加接收字符串长做判断.
l = Len(txtResponse)
wl = InStr(1, txtResponse, "#")
Text1 = Mid(txtResponse, 1, wl - 1)
Text2 = Mid(txtResponse, wl, l - wl + 1)
End If
End Select
End With
txtResponse.SelStart = Len(txtResponse)
End Sub
Private Sub MSComm1_OnComm()
Dim txtBuf As String
Dim i As Integer
Dim c As String
Dim wl As Integer
Dim l As Integer
With MSComm1
Select Case .CommEvent
Case comEvReceive
txtBuf = .Input
For i = 1 To Len(txtBuf)
c = Mid(txtBuf, i, 1)
txtResponse = txtResponse & c '输出显示
Next i
If Mid(txtResponse, 1, 1) = "$" Then '对接收字符串是否符合要求作判断,本句拟增加接收字符串长做判断.
l = Len(txtResponse)
wl = InStr(1, txtResponse, "#")
Text1 = Mid(txtResponse, 1, wl - 1)
Text2 = Mid(txtResponse, wl, l - wl + 1)
End If
End Select
End With
txtResponse.SelStart = Len(txtResponse)
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货