Dim fasongcishu, jieshoucishu As Integer
Dim fasongzijie, jieshouzijie, jieshouzijie1, fasongzijie1 As Byte
Private Sub Command5_Click()
List1.Clear
StatusBar1.Panels(4).Text = "接收次数:"
StatusBar1.Panels(3).Text = "接收字节:"
StatusBar1.Panels(2).Text = "发送次数:"
StatusBar1.Panels(1).Text = "发送字节:"
End Sub
Private Sub Form_Load()
If Option1.Value = True Then
MSComm1.InputMode = comInputModeText
End If
If Option2.Value = True Then
MSComm1.InputMode = comInputModeBinary
End If
GetAvailablePorts
Combo1.Text = "COM1"
End Sub
Private Sub Command1_Click()
If MSComm1.PortOpen = True Then
MsgBox "串口已经打开"
Else
Dim bote, shuju, tingzhi, jiaoyan, Seting As String
bote = Combo2.Text
shuju = Combo3.Text
tingzhi = Combo4.Text
jiaoyan = Combo5.Text
Seting = bote & "," & jiaoyan & "," & shuju & "," & tingzhi
MSComm1.CommPort = Split(Combo1.Text, "COM")(1)
MSComm1.Settings = Seting
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
MSComm1.InputMode = 0
MSComm1.InputLen = 0
MSComm1.PortOpen = True
Label6.Caption = "串口已经连接"
Shape1.BackColor = RGB(0, 200, 0)
End If
End Sub
Private Sub Command2_Click()
If MSComm1.PortOpen = False Then
MsgBox "串口尚未打开"
Else
MSComm1.PortOpen = False
Label6.Caption = "串口已经断开"
Shape1.BackColor = RGB(200, 0, 0)
End If
End Sub
Private Sub Command3_Click()
If MSComm1.PortOpen = True Then
MsgBox "串口已经打开,请断开连接再刷新"
Else
GetAvailablePorts
Combo1.Text = "COM1"
End If
End Sub
Private Sub Command4_Click()
If MSComm1.PortOpen = False Then
MsgBox "串口尚未连接"
Else
If Option1.Value = True Then
MSComm1.InputMode = comInputModeText
End If
If Option2.Value = True Then
MSComm1.InputMode = comInputModeBinary
End If
Me.MSComm1.Output = Text1.Text
List1.AddItem ("来自计算机:" & Text1.Text)
fasongzijie1 = LenB(StrConv(Text1.Text, vbFromUnicode))
fasongzijie = fasongzijie + fasongzijie1
StatusBar1.Panels(1).Text = "发送字节:" & fasongzijie
fasongcishu = fasongcishu + 1
StatusBar1.Panels(2).Text = "发送次数:" & fasongcishu
Text1.Text = ""
StatusBar1.Panels(5).Text = "发送成功!"
End If
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
MSComm1.InputMode = 1
MSComm1.InputLen = 0
End Sub
Private Sub GetAvailablePorts()
Dim i As Integer
Dim blnNoPort As Boolean
With Combo1
.Clear
'尝试打开COM1到COM16
For i = 1 To 16
MSComm1.CommPort = i
'打开错误陷阱
On Error Resume Next
MSComm1.PortOpen = True
'如果串口被成功打开,则这个串口存在
If Err.Number = 0 Then
.AddItem "COM" & i
.ItemData(.NewIndex) = i
End If
Err.Clear
' 关闭已打开的串口
If MSComm1.PortOpen Then MSComm1.PortOpen = False
' 关闭错误陷阱
On Error GoTo 0
Next
blnNoPort = .ListCount = 0
End With
If blnNoPort Then
MsgBox "计算机上没有串行通信接口"
End If
End Sub
Private Sub MSComm1_OnComm()
Dim CommData As String
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent '事件发生
Case 2
Cls
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区
If strBuff = "" Then
Else
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
Else '方便显示观察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
CommData = strData
strData = ""
Else
CommData = strBuff
End If
List1.AddItem ("来自单片机:" & CommData)
jieshoucishu = jieshoucishu + 1
StatusBar1.Panels(4).Text = "接收次数:" & jieshoucishu
jieshouzijie1 = LenB(StrConv(CommData, vbFromUnicode))
jieshouzijie = jieshouzijie + jieshouzijie1
StatusBar1.Panels(3).Text = "接收字节:" & jieshouzijie
End If
End Select
End Sub