经典的串口调试工具源代码(二)
Private Sub cmdswitch_Click()
On Error GoTo Err
If MSComm.PortOpen = True Then
ComSwitch = True
Else
ComSwitch = False
End If
If ComSwitch = False Then
StatusBar1.Panels(1).Text = \ mnuconnect.Caption = \ OpenCom ' 打开串口
ComSwitch = True
Else
CloseCom ' 关闭串口
ComSwitch = False
StatusBar1.Panels(1).Text = \
mnuconnect.Caption = \
StatusBar1.Panels(2).Text = \
StatusBar1.Panels(3).Text = MSComm.Settings
If (OutputAscii) Then
StatusBar1.Panels(4) = \
Else
StatusBar1.Panels(4) = \
End If End If
Err: End Sub
Private Sub Form_Load()
On Error GoTo Err
lblWEB.FontUnderline = True ' WEB上加下划线 lblWEB.ForeColor = vbBlue ' 蓝色显示WEB
txtsend.Text = \' 载入发送信息
If MSComm.PortOpen = True Then MSComm.PortOpen = False ' 先判断串口是否打
开,如果打开则先关闭
' 初始化串口
Call Comm_initial(Val(Mid(cbocom.Text, 4, 1)), cbobaudrate.Text, Left(cboparitybit.Text, 1),
cbodatabit.Text, cbostopbit.Text)
' 数据位载入 cbodatabit.AddItem \ cbodatabit.AddItem \ cbodatabit.AddItem \
' 停止位载入 cbostopbit.AddItem \ cbostopbit.AddItem \ cbostopbit.AddItem \
Err: End Sub
Private Sub hexReceive()
On Error GoTo Err
Dim ReceiveArr() As Byte ' 接收数据数组 Dim receiveData As String ' 数据暂存 Dim Counter As Integer ' 接收数据个数计数器
Dim i As Integer ' 循环变量
If (MSComm.InBufferCount > 0) Then
Counter = MSComm.InBufferCount ' 读取接收数据个数
receiveData = \' 清缓冲
ReceiveArr = MSComm.Input ' 数据放入数组 For i = 0 To (Counter - 1) Step 1 ' 数据格式处理
If (ReceiveArr(i) < 16) Then
receiveData = receiveData & \' 小于16,前面加0
Else
receiveData = receiveData & Hex(ReceiveArr(i)) & Space(1) ' 加空格显示
End If Next i
TxtReceive.Text = TxtReceive.Text + receiveData ' 显示接收的十六进制数据
TxtReceive.SelStart = Len(TxtReceive.Text) ' 显示光标位置
End If
ReceiveCount = ReceiveCount + Counter ' 接收计数 txtRXcount.Text = \' 接收字节数显示
If chkautoclear.Value = 1 Then ' 自动清空判断
If ReceiveCount >= 65535 Then
TxtReceive.Text = \
End If End If Err: End Sub
Private Sub hexSend() On Error Resume Next
Dim outputLen As Integer ' 发送数据长度 Dim outData As String ' 发送数据暂存 Dim SendArr() As Byte ' 发送数组 Dim TemporarySave As String ' 数据暂存 Dim dataCount As Integer ' 数据个数计数
Dim i As Integer ' 局部变量
outData = UCase(Replace(txtsend.Text, Space(1), Space(0))) ' 先去掉空格,再转换为
大写字母
outData = UCase(outData) ' 转换成大写 outputLen = Len(outData) ' 数据长度
For i = 0 To outputLen
TemporarySave = Mid(outData, i + 1, 1) ' 取一位数据
If (Asc(TemporarySave) >= 48 And Asc(TemporarySave) <= 57) Or (Asc(TemporarySave) >= 65
And Asc(TemporarySave) <= 70) Then dataCount = dataCount + 1
Else Exit For Exit Sub End If Next
If dataCount Mod 2 <> 0 Then ' 判断十六进制数据是否为双数
dataCount = dataCount - 1 ' 不是双数,则减1
End If
outData = Left(outData, dataCount) ' 取出有效的十六进制数据
ReDim SendArr(dataCount / 2 - 1) ' 重新定义数组长度
For i = 0 To dataCount / 2 - 1
SendArr(i) = Val(\' 取出数据转换成十六进制并放入
数组中 Next
SendCount = SendCount + (dataCount / 2) ' 计算总发送数
txtTXcount.Text = \
MSComm.Output = SendArr ' 发送数据
End Sub
Private Sub OpenCom() '打开串口
On Error GoTo Err
If MSComm.PortOpen = True Then MSComm.PortOpen = False ' 先判断串口是否打
开,如果打开则先关闭
Call Comm_reSet(Val(Mid(cbocom.Text, 4, 1)), cbobaudrate.Text, Left(cboparitybit.Text, 1),
cbodatabit.Text, cbostopbit.Text) ' 串口设置
If MSComm.PortOpen = True Then
txtstatus.Text = \:\,\
Left(cboparitybit.Text, 1) & \
cmdswitch.Caption = \关闭串口\ mnuconnect.Caption = \
'ImgSwitch.Picture = LoadPicture(\我的VB\\串口调试软件\\图片\\kai.jpg\' 显示串口已经打开的
图标
ImgSwitchon.Visible = True
ImgSwitchoff.Visible = False
Else
txtstatus.Text = \:COM Port Cloced\' 串口状态显示
cmdswitch.Caption = \打开串口\ mnuconnect.Caption = \
'ImgSwitch.Picture = LoadPicture(\我的VB\\串口调试软件\\图片\\guan.jpg\' 显示串口已经关闭
的图标
ImgSwitchoff.Visible = True ImgSwitchon.Visible = False
End If Err: End Sub
Private Sub textReceive()
On Error GoTo Err
InputSignal = MSComm.Input
ReceiveCount = ReceiveCount + LenB(StrConv(InputSignal, vbFromUnicode)) ' 计算总接收数据
If DisplaySwitch = False Then ' 显示接收文本 TxtReceive.Text = TxtReceive.Text & InputSignal ' 单片机内存的值用
TextReceive显示出
TxtReceive.SelStart = Len(TxtReceive.Text) ' 显示光标位置
End If
txtRXcount.Text = \' 接收字节数显示
If chkautoclear.Value = 1 Then ' 自动清空判断
If ReceiveCount >= 65535 Then
TxtReceive.Text = \
End If End If Err: End Sub
Private Sub textSend() On Error GoTo Err If ModeSend = True Then
OutputSignal = FileData ' 发送文件
Else
OutputSignal = txtsend.Text ' 发送文本
End If
SendCount = SendCount + LenB(StrConv(OutputSignal, vbFromUnicode)) ' 计算总发送数
txtTXcount.Text = \' 发送字节数显示
Err: