Dim StrInComm1 As String '定义从串口1接收数据变量
Dim StrInComm2 As String '定义从串口2接收数据变量
Dim StrOut As String '定义发送数据变量
Private Sub Form_Load()
Text3.Text = 1
'清零
StrInComm1 = ""
StrInComm2 = ""
StrOut = ""
'打开端口
MSComm1.PortOpen = True
MSComm2.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
'关闭端口
MSComm1.PortOpen = False
MSComm2.PortOpen = False
End Sub
Private Sub Timer1_Timer()
Text3.Text = Text3.Text + 1
'每100毫秒从串口接收一次数据
Dim IntPositionD As Integer '定义“0D”位置变量"
StrOut = ""
StrInComm1 = StrInComm1 + MSComm1.Input '得到串口1数据
StrInComm2 = StrInComm2 + MSComm2.Input '得到串口2数据
IntPositionD = InStr(StrInComm1, Chr(13)) '判断从跟串口1中得到的数据是否有回车
If IntPositionD > 0 Then '如果有
StrOut = StrOut + Left(StrInComm1, IntPositionD) '取出左边的数据
StrInComm1 = Right(StrInComm1, Len(StrInComm1) - IntPositionD) '取出右边的数据
End If
IntPositionD = InStr(StrInComm2, Chr(13)) ''判断从跟串口1中得到的数据是否有回车
If IntPositionD > 0 Then
StrOut = StrOut + Left(StrInComm2, IntPositionD)
StrInComm2 = Right(StrInComm2, Len(StrInComm2) - IntPositionD)
End If
Text2.Text = StrOut '求出发送数据
Text1.Text = Text1.Text + Text2.Text
End Sub
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/