基于vb的半双工串口通信调试软件设计
一、任务
使用RS-485标准实现两台PC之间的半双工通信。
二、硬件概况
硬件设计如图1所示,由于目前多数电脑不带DB9通信接口,因此需要购买usb转R-S232转换头,并安装好相关驱动。
此模块使用DB9接口的7脚(RTS端)作为485通信的数据收发控制端。
图1 使用max485和max232设计半双工通信硬件电路板
三、软件设计
1、界面设计
该程序有两个界面,主界面和设置界面。
图2所示,是程序的主界面。
图3所示,是通信参数设置界面。
图2 调试软件主界面
图3 设置界面2、变量及函数声明
Private tx As Integer ‘发送字符数
Private rx As Integer ‘接受字符数
Function addRx(X As Integer)
rx = rx + X
LabelRx.Caption = CStr(rx)
End Function
Function addTx(X As Integer)
tx = tx + X
LabelTx.Caption = CStr(tx)
End Function
Function clearRx()
rx = 0
LabelRx.Caption = "0"
End Function
Function clearTx()
tx = 0
LabelTx.Caption = "0"
End Function
3、窗体加载与卸载
Private Sub Form_Load()
'MSComm1.PortOpen = True '打开串口
' ss = False
Combo1.Text = "COM1"
With Combo1
.AddItem "COM1"
.AddItem "COM2"
.AddItem "COM3"
.AddItem "COM4"
.AddItem "COM5"
.AddItem "COM6"
End With
On Error GoTo err_handle
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
mPort = 1
MSComm1.PortOpen = True
Command3.Caption = "关闭串口"
If MSComm1.RTSEnable Then
Label3.Caption = "高"
Else
Label3.Caption = "低"
End If
Exit Sub
err_handle:
Command3.Caption = "打开串口"
Label3.Caption = ""
MsgBox "串口不存在或被占用"
End Sub
Private Sub Form_Unload(Cancel As Integer)
'关闭串口
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
4、串口设置与操作
(1)打开与关闭串口
Private Sub Command3_Click()
If Command3.Caption = "打开串口" Then
On Error GoTo err_handle1
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
mPort = Combo1.ListIndex + 1
MSComm1.PortOpen = True
Command3.Caption = "关闭串口"
Else
On Error GoTo err_handle2
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Command3.Caption = "打开串口"
End If
Exit Sub
err_handle1:
Command3.Caption = "打开串口"
MsgBox "串口不存在或被占用"
Exit Sub
err_handle2:
Exit Sub
End Sub
(2)串口参数设置
Private Sub CommSet_Click()
Form2.Show
End Sub
Public comport As Integer
Public comsetting As String
Private Sub command1_Click()
Dim t As Integer
If mand3.Caption = "打开串口" Then
MsgBox "打开串口后才能设置!", 52, "提示"
Else
comsetting = Combo2.Text & "," & Combo3.Text & "," & Combo4.Text & "," & Combo5.Text
If (Form1.MSComm1.PortOpen) Then
Form1.MSComm1.PortOpen = False
Form1.MSComm1.Settings = comsetting
Form1.MSComm1.PortOpen = True
End If
Form1.MSComm1.Settings = comsetting
End If
Unload Me
End Sub
Private Sub Command2_Click()
comport = 1
comsetting = "9600,N,8,1"
Unload Me
End Sub
Private Sub Form_Load()
comsetting = "9600,N,8,1"
End Sub
5、RTS状态设置
Private Sub Command2_Click()
If MSComm1.RTSEnable = False Then
MSComm1.RTSEnable = True '抬高电位
Label3.Caption = "高"
Else
MSComm1.RTSEnable = False
Label3.Caption = "低"
End If
End Sub
6、MSComm事件响应程序
Private Sub MSComm1_OnComm()
Select Case mEvent
Case comEvReceive
Dim Buffer As Variant
Buffer = MSComm1.Input
MSComm1.InputLen = 0
TextIn.Text = TextIn.Text + CStr(Buffer)
addRx Len(CStr(Buffer))
Case Else
MsgBox "没有发生comEvReceive事件"
End Select
End Sub
7、发送按钮响应事件
Private Sub Command7_Click()
'发送按钮
If MSComm1.PortOpen = False Then
MsgBox "串口没有打开", 52, "提示"
Else
Dim outstring As String
outstring = TextOut.Text
For i = 1 To Len(outstring)
MSComm1.Output = Mid(outstring, i, 1) Next i
addTx Len(outstring)
If outstring = "" Then
MsgBox "无发送数据!", 52, "提示"
End If
End If
四、结论
此软件可以实现两台电脑的半双工通信(发送与接受字符),调试结果如图4和图5所示。
图4 PC1发送送据
图4 PC2接收送据。