VB的mscomm控件oncomm事件中怎么用收到的数据不同值作为条件写条件语句

2024-12-24 17:18:39
推荐回答(2个)
回答1:

'假设控件名称为com
Private Sub com_OnComEvent(ByVal comEvent As Integer, ByVal eError As Integer)
Dim ss As String
Dim ff As String
Select Case comEvent
Case 0:
Case 1: ‘ 收到数据,处理数据
ss = com.Read
While (Trim(ss) <> "")
ff = ff + ss
ss = com.Read
Wend
Debug.Print ff '这个ff就是收到的数据,你想怎么判断使用就怎么使用。
End Select

End Sub

回答2:

Option Explicit
Dim sendvar(0) As Byte
Dim i As Integer
Dim sum As Integer
Private Sub Command1_Click()
For i = 0 To 15
sendvar(0) = i
MSComm1.Output = sendvar
Next
End Sub

Private Sub Command2_Click()
MSComm1.Output = " df s"
End Sub

Private Sub Form_Load()
MSComm1.PortOpen = True
MSComm1.SThreshold = 1
End Sub

Private Sub MSComm1_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer
Select Case MSComm1.CommEvent
Case comEvSend
sum = sum + 1 '统计comEvSend次数
Label1.Caption = sum
'MsgBox "afadfaf", vbOKOnly, ""
End Select
End Sub