Private Sub Command1_Click() '开始测试
On Error GoTo BLAK
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
MSComm1.CommPort = Combo1.ListIndex + 1
MSComm1.PortOpen = True
Else
MSComm1.CommPort = Combo1.ListIndex + 1
MSComm1.PortOpen = True
End If
Exit Sub
BLAK:
MsgBox "串口不存在或者被占用!", vbOKOnly, "提示信息"
End Sub
Private Sub Command2_Click() '停止测试
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Command4_Click()
If MSComm1.PortOpen = True Then '防止串口没有打开 打开发送出错
MSComm1.Output = "B"
MSComm1.OutBufferCount = 0 '发送缓冲区清空
'MSComm1.Output = outbte() '动态数组
End If
End Sub
Private Sub Command5_Click()
If MSComm1.PortOpen = True Then '防止串口没有打开 打开发送出错
MSComm1.Output = "A"
MSComm1.OutBufferCount = 0 '发送缓冲区清空
'MSComm1.Output = outbte() '动态数组
End If
End Sub
Private Sub Command6_Click()
If MSComm1.PortOpen = True Then '防止串口没有打开 打开发送出错
MSComm1.Output = "C"
MSComm1.OutBufferCount = 0 '发送缓冲区清空
'MSComm1.Output = outbte() '动态数组
End If
End Sub
Private Sub Form_Load() '初始化
Combo1.ListIndex = 0
Combo2.ListIndex = 3
'Combo3.ListIndex = 3
MSComm1.InBufferSize = 8 '设置返回接收缓冲区的大小,以字符为单位。
MSComm1.OutBufferSize = 2
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False '关串口
MSComm1.RThreshold = 1 '设置并返回产生oncomm事件的字符数,以字符为单位. Rthreshold 为 1,接收缓冲区收到每一
'个字符都会使 MSComm 控件产生 OnComm 事件。
MSComm1.SThreshold = 2 '
MSComm1.InputLen = 0 '设置从接收缓冲区读取的字数,为0读取整个缓冲区
MSComm1.InputMode = comInputModeText '以文本方度接收
' If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.InBufferCount = 0 '清空接收缓冲区
Text1(0).Text = "00℃"
Text2(1).Text = "00%"
Text1(1).Text = "00%"
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive ' 收到 RThreshold # of
Dim hk As Variant
hk = MSComm1.Input
If hk4 = 2 Then
Text2(1).Text = hk & "℃"
MSComm1.RThreshold = 1
hk4 = 0
End If
If hk4 = 3 Then
Text1(0).Text = hk & "%"
MSComm1.RThreshold = 1
hk4 = 0
End If
If hk4 = 4 Then
Text1(1).Text = hk & "%"
MSComm1.RThreshold = 1
hk4 = 0
End If
If hk = "A" Then
MSComm1.RThreshold = 2
hk4 = 2
End If
If hk = "B" Then
MSComm1.RThreshold = 2
hk4 = 3
End If
If hk = "C" Then
MSComm1.RThreshold = 2
hk4 = 4
End If
MSComm1.InBufferCount = 0
End Select
End Sub