7,759
社区成员
发帖
与我相关
我的任务
分享
dim com_string '存储读取数据
Dim t1 As Long, com_String As String
t1 = Timer
Select Case MSComm1.CommEvent
Case comEvReceive '收到 RThreshold定义的字符数1字节
Do
DoEvents
Loop While Timer - t1 < 0.05
com_String = MSComm1.Input
End Select
Dim com_String As String
Private Sub MSComm1_OnComm()
Dim rd As Long
Dim CommandStr As String
If MSComm1.CommEvent = comEvReceive Then
com_String = com_String & MSComm1.Input
rd = InStr(1, com_String, vbCrLf)
If rd <> 0 Then
'已经完整的收到数据
If rd = Len(com_String) - 2 Then
'正好收完一条命令
ExecCommand:
CommandStr = com_String
Select Case UCase(CommandStr)
Case "COMMAND1": ExecuteYourCommand1
'...
End Select
com_String = ""
Else
'同时收到了下一条命令的部分
CommandStr = Left(com_String, rd)
rd = rd + 2
com_String = Right(com_String, Len(com_String) - rd)
GoTo ExecCommand
End If
End If
End If
End Sub
dim com_string '存储读取数据
Dim t1 As Long, com_String As String
t1 = Timer
Select Case MSComm1.CommEvent
Case comEvReceive '收到 RThreshold定义的字符数1字节
Do
DoEvents
Loop While Timer - t1 < 0.05
com_String = MSComm1.Input
End Select
If Mid(com_string, 1, 1) = "#" And Right(com_string, 2) = vbCrLf Then
'在此写数据处理代码
Debug.Print com_string
com_string = ""
End If
Option Explicit
Dim com_string As String
Private Sub Form_Load()
MSComm1.InputMode = comInputModeText
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
com_string = com_string & MSComm1.Input
If Mid(com_string, 1, 1) = "#" And Right(com_string, 2) = vbCrLf Then
Debug.Print com_string
com_string = ""
End If
End Select
End Sub