Option Explicit
Dim StrInComm1 As String 注释:定义从串口1接收数据变量
Dim StrInComm2 As String 注释:定义从串口2接收数据变量
Dim StrOut As String 注释:定义发送数据变量
代码段1、
Private Sub Form_Load()
Text3.Text = 1
注释:清零
StrInComm1 = ""
StrInComm2 = ""
StrOut = ""
注释:打开端口
MSComm1.PortOpen = True
MSComm2.PortOpen = True
End Sub
代码段2、
Private Sub Form_Unload(Cancel As Integer)
注释:关闭端口
MSComm1.PortOpen = False
MSComm2.PortOpen = False
End Sub
代码段3、
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/