经典的串口调试工具源代码(一)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
经典的串口调试助手源代码(一)
Dim OutputAscii As Boolean
Dim InputString As String
Dim OutputString As String
'==============================================================================
=======
' 变量定义
'==============================================================================
=======
Option Explicit ' 强制显式声明
Dim ComSwitch As Boolean ' 串口开关状态判断
Dim FileData As String ' 要发送的文件暂存Dim SendCount As Long ' 发送数据字节计数器
Dim ReceiveCount As Long ' 接收数据字节计数器Dim InputSignal As String ' 接收缓冲暂存
Dim OutputSignal As String ' 发送数据暂存
Dim DisplaySwitch As Boolean ' 显示开关
Dim ModeSend As Boolean ' 发送方式判断
Dim Savetime As Single ' 时间数据暂存延时用Dim SaveTextPath As String ' 保存文本路径
' 网页超链接申明
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As
String, ByVal nShowCmd As Long) As Long
Private Sub CloseCom() '关闭串口
On Error GoTo Err
If MSComm.PortOpen = True Then MSComm.PortOpen = False ' 先判断串口是否打
开,如果打开则先关闭
txtstatus.Text = "STATUS:COM Port Cloced" ' 串口状态显示
mnuconnect.Caption = "断开串口"
cmdswitch.Caption = "打开串口"
'ImgSwitch.Picture = LoadPicture("f:\我的VB\串口调试软件\图片\guan.jpg") ' 显示串口已经关闭
的图标
ImgSwitchoff.Visible = True
ImgSwitchon.Visible = False
Err:
End Sub
Private Sub UpdateStatus()
If MSComm.PortOpen Then
StatusBar1.Panels(1).Text = "Connected"
mnuautosend.Caption = "自动发送"
mnuconnect.Caption = "断开串口"
Else
StatusBar1.Panels(1).Text = "断开串口"
mnuautosend.Caption = "disautosend"
mnuconnect.Caption = "打开串口"
End If
StatusBar1.Panels(2).Text = "COM" & mPort
StatusBar1.Panels(3).Text = MSComm.Settings
If (OutputAscii) Then
StatusBar1.Panels(4) = "ASCII"
Else
StatusBar1.Panels(4) = "HEX"
End If
'
On Error GoTo Err
If ChkAutoSend.Value = 1 Then ' 如果有效则,自动发送
If MSComm.PortOpen = True Then ' 串口状态判断
mnuautosend.Caption = "Dis&autosend"
TmrAutoSend.Interval = Val(TxtAutoSendTime) ' 设置自动发送时间
TmrAutoSend.Enabled = True ' 打开自动发送定时器
Else
mnuautosend.Caption = "autosend"
ChkAutoSend.Value = 0 ' 串口没有打开去掉自动发送MsgBox "串口没有打开,请打开串口", 48, "串口调试助手" ' 如果串口没有被打开,提
示打开串口
End If
ElseIf ChkAutoSend.Value = 0 Then ' 如果无效,不发送
mnuautosend.Caption = "autosend"
TmrAutoSend.Enabled = False ' 关闭自动发送定时器
End If
Err:
End Sub
Private Sub CmdSendFile_Click() '发送文件
On Error GoTo Err
If MSComm.PortOpen = True Then ' 如果串口打开了,则可以发送数
据
If FileData = "" Then ' 判断发送数据是否为空MsgBox "发送的文件为空", 16, "串口调试助手" ' 发送数据为空则提示
Else
If ChkHexReceive.Value = 1 Then ' 如果按十六进制接收时,按二进制
发送,否则按文本发送
MSComm.InputMode = comInputModeBinary ' 二进制发送
Else
MSComm.InputMode = comInputModeText ' 文本发送
End If
MSComm.Output = Trim(FileData) ' 发送数据
ModeSend = True ' 设置文本发送方式
End If
Else
MsgBox "串口没有打开,请打开串口", 48, "串口调试助手" ' 如果串口没有被打开,提
示打开串口
End If
Err:
End Sub