串口调试助手源代码
用VB编写串口调试小助手
经典的串口调试助手源代码分类VB在单片机中的应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 quotshell32.dllquot Alias quotShellExecuteAquot 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 quotSTATUSCOM Port Clocedquot 串口状态显示mnuconnect.Caption quot断开串口quot cmdswitch.Caption quot打开串口quot ImgSwitch.Picture LoadPicturequotf:我的VB串口调试软件图片guan.jpgquot 显示串口已经关闭的图标ImgSwitchoff.Visible True ImgSwitchon.Visible False Err: End Sub Private Sub UpdateStatus If MSComm.PortOpen Then StatusBar1.Panels1.Text quotConnectedquot mnuautosend.Caption quot自动发送quot mnuconnect.Caption quot断开串口quot Else StatusBar1.Panels1.Text quot断开串口quot mnuautosend.Caption quotdisautosendquot mnuconnect.Caption quot打开串口quot End If StatusBar1.Panels2.Text quotCOMquot amp mPortStatusBar1.Panels3.Text MSComm.Settings If OutputAscii Then StatusBar1.Panels4 quotASCIIquot Else StatusBar1.Panels4 quotHEXquot End If On Error GoTo Err If ChkAutoSend.Value 1 Then 如果有效则自动发送If MSComm.PortOpen True Then 串口状态判断mnuautosend.Caption quotDisampautosendquot TmrAutoSend.Interval ValTxtAutoSendTime 设置自动发送时间TmrAutoSend.Enabled True 打开自动发送定时器Else mnuautosend.Caption quotautosendquot ChkAutoSend.Value 0 串口没有打开去掉自动发送MsgBox quot串口没有打开请打开串口quot 48 quot串口调试助手quot 如果串口没有被打开提示打开串口End If ElseIf ChkAutoSend.Value 0 Then 如果无效不发送mnuautosend.Caption quotautosendquot TmrAutoSend.Enabled False 关闭自动发送定时器End If Err: End Sub Private Sub CmdSendFile_Click 发送文件On Error GoTo Err If MSComm.PortOpen True Then 如果串口打开了则可以发送数据If FileData quotquot Then 判断发送数据是否为空MsgBox quot发送的文件为空quot 16 quot串口调试助手quot 发送数据为空则提示Else If ChkHexReceive.Value 1 Then 如果按十六进制接收时按二进制发送否则按文本发送MSComm.InputMode comInputModeBinary 二进制发送ElseMSComm.InputMode comInputModeText 文本发送End If MSComm.Output TrimFileData 发送数据ModeSend True 设置文本发送方式End If Else MsgBox quot串口没有打开请打开串口quot 48 quot串口调试助手quot 如果串口没有被打开提示打开串口End If Err: End Sub Private Sub Comm_initialPort As Byte BaudRate AsString ParityBit As String DataBit As Integer StopBit As Integer On Error GoTo ErrorTrap 错误则跳往错误处理If MSComm.PortOpen True ThenMSComm.PortOpen False 先判断串口是否打开如果打开则先关闭mPort Port 设定端口MSComm.Settings BaudRate amp quotquot amp ParityBit amp quotquot amp DataBit amp quotquot amp StopBit 设置波特率无校验8位数据位1位停止位MSComm.InBufferSize 1024 设置接收缓冲区为1024字节MSComm.OutBufferSize 4096 设置发送缓冲区为4096字节MSComm.InBufferCount 0 清空输入缓冲区MSComm.OutBufferCount 0 清空输出缓冲区MSComm.SThreshold 1 发送缓冲区空触发发送事件MSComm.RThreshold 1 每X个字符到接收缓冲区引起触发接收事件MSComm.OutBufferCount 0 清空发送缓冲区MSComm.InBufferCount 0 滑空接收缓冲MSComm.PortOpen True 打开串口If MSComm.PortOpen True Then txtstatus.Text quotSTATUSquot amp cbocom.Text amp quot OPENDquot amp cbobaudrate.Text amp quotquot amp Leftcboparitybit.Text 1 amp quotquot amp cbodatabit.Text amp quotquot amp cbostopbit.Text Else txtstatus.Text quotSTATUSCOM Port Clocedquot 串口没打开时提示串口关闭状态End If Exit Sub ErrorTrap: 错误处理Select Case Err.Number Case comPortAlreadyOpen 如果串口已经打开则提示MsgBox quot没有发现此串口或被占用quot 49 quot串口调试助手quot CloseCom Case Else MsgBox quot没有发现此串口或被占用quot 49 quot串口调试助手quot CloseCom End Select Err.Clear End Sub Private Sub Comm_reSetPort As Byte BaudRate As String ParityBit As String DataBit As Integer StopBit As Integer On Error GoTo ErrorHint 错误则跳往错误处理If MSComm.PortOpen True Then MSComm.PortOpen False 先判断串口是否打开如果打开则先关闭mPort Port 设定端口MSComm.Settings BaudRate amp quotquot amp ParityBit amp quotquot amp DataBit amp quotquot amp StopBit 设置波特率无校验8位数据位1位停止位MSComm.PortOpen True 打开串口IfMSComm.PortOpen True Then cmdswitch.Caption quot关闭串口quot ImgSwitch.Picture LoadPicturequotf:我的VB串口调试软件图片kai.jpgquot 显示串口已经打开的图标ImgSwitchoff.Visible False mnuconnect.Caption quotdisconnectquot ImgSwitchon.Visible True txtstatus.Text quotSTATUSquot amp cbocom.Text amp quot OPENDquot amp cbobaudrate.Text amp quotquot amp Leftcboparitybit.Text 1 amp quotquot amp cbodatabit.Text amp quotquot amp cbostopbit.Text Else cmdswitch.Caption quot打开串口quot ImgSwitch.Picture LoadPicturequotf:我的VB串口调试软件图片guan.jpgquot 显示串口已经关闭的图标ImgSwitchon.Visible False ImgSwitchoff.Visible True txtstatus.Text quotSTATUSCOM Port Clocedquot End If Exit Sub ErrorHint: 错误处理Select Case Err.Number Case comPortAlreadyOpen 如果串口已经打开则提示MsgBox quot没有成功请重试quot vbExclamation quot串口调试助手quot CloseCom 调用关闭串口函数Case Else MsgBox quot没有成功请重试quot vbExclamation quot串口调试助手quot CloseCom 调用关闭串口函数End Select Err.Clear 清除Err 对象的属性End Sub Private Sub Command1_Click End Sub Private Sub cbobaudrate_Change Call Comm_reSetValMidcbocom.Text 4 2 cbobaudrate.Text Leftcboparitybit.Text 1cbodatabit.Text cbostopbit.Text 串口设置End Sub Private Sub cbocom_Change Call Comm_reSetValMidcbocom.Text 4 2 cbobaudrate.Text Leftcboparitybit.Text 1 cbodatabit.Text cbostopbit.Text 串口设置End Sub Private Sub cbodatabit_Change Call Comm_reSetValMidcbocom.Text 4 2 cbobaudrate.Text Leftcboparitybit.Text 1 cbodatabit.Text cbostopbit.Text 串口设置End Sub Private Sub cboparitybit_Change Call Comm_reSetValMidcbocom.Text 4 2 cbobaudrate.Text Leftcboparitybit.Text 1 cbodatabit.Text cbostopbit.Text 串口设置End Sub Private Sub cbostopbit_Change Call Comm_reSetValMidcbocom.Text 4 2 cbobaudrate.Text Leftcboparitybit.Text 1 cbodatabit.Text cbostopbit.Text 串口设置End Sub Private Sub chkautosend_Click On Error GoTo Err If ChkAutoSend.Value 1 Then 如果有效则自动发送If MSComm.PortOpen True Then 串口状态判断mnuautosend.Caption quot取消自动发送quot TmrAutoSend.Interval ValTxtAutoSendTime 设置自动发送时间TmrAutoSend.Enabled True 打开自动发送定时器Else ChkAutoSend.Value 0串口没有打开去掉自动发送MsgBox quot串口没有打开请打开串口quot 48 quot串口调试助手quot 如果串口没有被打开提示打开串口End If ElseIf ChkAutoSend.Value 0 Then 如果无效不发送mnuautosend.Caption quot自动发送数据quot TmrAutoSend.Enabled False 关闭自动发送定时器End If Err: End Sub Private Sub cmdamend_Click Dim spShell As Object 定义存放引用对象的变量Dim spFolder As Object 定义存放引用对象的变量Dim spFolderItem As Object 定义存放引用对象的变量Dim spPath As String 定义存放的变量On Error GoTo Err 错误处理防止取消打开文件夹时报错Const WINDOW_HANDLE 0 ConstNO_OPTIONS 0 Set spShell CreateObjectquotShell.Applicationquot Set spFolder spShell.BrowseForFolderWINDOW_HANDLE quot选择目录:quot NO_OPTIONS quotC:Scriptsquot Set spFolderItem spFolder.Self spPath spFolderItem.Path spPath ReplacespPath quotquot quotquot Replace函数的返回值是一个字符串txtsavepath.Text spPath 把文件夹路径显示在标签上SaveTextPath txtsavepath.Text 路径暂存Err: End Sub Private Sub CmdClearCounter_Click On Error GoTo Err SendCount 0 发送计数器清零ReceiveCount 0 接收计数器清零txtRXcount.Text quotRX:quot amp 0 接收计数txtTXcount.Text quotTX:quot amp 0 发送计数Err: End Sub Private Sub cmdclearrecieve_Click TxtReceive.Text quotquot End Sub Private Sub cmdclearsend_Click txtsend.Text quotquot End Sub Private Sub CmdHelp_Click FrmHelp.Show End Sub Private Sub CmdQuit_Click If MSComm.PortOpen True Then MSComm.PortOpen False 先判断串口是否打开如果打开则先关闭Unload Me 卸载窗体并退出程序End End Sub Private Sub cmdsavedisp_Click On Error GoTo Err 错误处理SaveTextPath txtsavepath 路径暂存Open txtsavepath amp quot1.txtquot For Output As 1 打开文件不存在的话会创建文件如已存在会覆盖output 改为append 为追加改为input 则只读Print 1 YearDate amp quot年quot amp MonthDate amp quot月quot amp DayDate amp _ quot 日quot amp HourTime amp quot时quot amp MinuteTime amp quot分quot amp SecondTime amp _ quot秒quot amp vbCrLf amp TxtReceive.Text vbCrLf 把接收区的文本保存文本前加上保存时间0000年00月00日00时00分00秒vbcrlf 为回车换行Close 1 关闭文件txtsavepath quotOK1.txt Savequot 提示保存成功cmdsavedisp.Enabled False Savetime Timer 记下开始的时间While Timer lt Savetime 5 循环等待5 - 要延时的时间DoEvents 转让控制权以便让操作系统处理其它的事件。
经典的串口调试工具源代码(一)
经典的串口调试助手源代码(一)Dim OutputAscii As BooleanDim InputString As StringDim 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 AsString, ByVal nShowCmd As Long) As LongPrivate Sub CloseCom() '关闭串口On Error GoTo ErrIf 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 = TrueImgSwitchon.Visible = FalseErr:End SubPrivate Sub UpdateStatus()If MSComm.PortOpen ThenStatusBar1.Panels(1).Text = "Connected"mnuautosend.Caption = "自动发送"mnuconnect.Caption = "断开串口"ElseStatusBar1.Panels(1).Text = "断开串口"mnuautosend.Caption = "disautosend"mnuconnect.Caption = "打开串口"End IfStatusBar1.Panels(2).Text = "COM" & mPortStatusBar1.Panels(3).Text = MSComm.SettingsIf (OutputAscii) ThenStatusBar1.Panels(4) = "ASCII"ElseStatusBar1.Panels(4) = "HEX"End If'On Error GoTo ErrIf ChkAutoSend.Value = 1 Then ' 如果有效则,自动发送If MSComm.PortOpen = True Then ' 串口状态判断mnuautosend.Caption = "Dis&autosend"TmrAutoSend.Interval = Val(TxtAutoSendTime) ' 设置自动发送时间TmrAutoSend.Enabled = True ' 打开自动发送定时器Elsemnuautosend.Caption = "autosend"ChkAutoSend.Value = 0 ' 串口没有打开去掉自动发送MsgBox "串口没有打开,请打开串口", 48, "串口调试助手" ' 如果串口没有被打开,提示打开串口End IfElseIf ChkAutoSend.Value = 0 Then ' 如果无效,不发送mnuautosend.Caption = "autosend"TmrAutoSend.Enabled = False ' 关闭自动发送定时器End IfErr:End SubPrivate Sub CmdSendFile_Click() '发送文件On Error GoTo ErrIf MSComm.PortOpen = True Then ' 如果串口打开了,则可以发送数据If FileData = "" Then ' 判断发送数据是否为空MsgBox "发送的文件为空", 16, "串口调试助手" ' 发送数据为空则提示ElseIf ChkHexReceive.Value = 1 Then ' 如果按十六进制接收时,按二进制发送,否则按文本发送MSComm.InputMode = comInputModeBinary ' 二进制发送ElseMSComm.InputMode = comInputModeText ' 文本发送End IfMSComm.Output = Trim(FileData) ' 发送数据ModeSend = True ' 设置文本发送方式End IfElseMsgBox "串口没有打开,请打开串口", 48, "串口调试助手" ' 如果串口没有被打开,提示打开串口End IfErr:End SubPrivate Sub Comm_initial(Port As Byte, BaudRate As String, ParityBit As String, DataBit As Integer,StopBit As Integer)On Error GoTo ErrorTrap ' 错误则跳往错误处理If MSComm.PortOpen = True Then MSComm.PortOpen = False ' 先判断串口是否打开,如果打开则先关闭mPort = Port ' 设定端口MSComm.Settings = BaudRate & "," & ParityBit & "," & DataBit & "," & StopBit ' 设置波特率,无校验,8位数据位,1位停止位MSComm.InBufferSize = 1024 ' 设置接收缓冲区为1024字节MSComm.OutBufferSize = 4096 ' 设置发送缓冲区为4096字节MSComm.InBufferCount = 0 ' 清空输入缓冲区MSComm.OutBufferCount = 0 ' 清空输出缓冲区MSComm.SThreshold = 1 ' 发送缓冲区空触发发送事件MSComm.RThreshold = 1 ' 每X个字符到接收缓冲区引起触发接收事件MSComm.OutBufferCount = 0 ' 清空发送缓冲区MSComm.InBufferCount = 0 ' 滑空接收缓冲MSComm.PortOpen = True ' 打开串口If MSComm.PortOpen = True Thentxtstatus.Text = "STATUS:" & cbocom.Text & " OPEND," & cbobaudrate.Text & "," & Left(cboparitybit.Text, 1) & "," & cbodatabit.Text & "," & cbostopbit.TextElsetxtstatus.Text = "STATUS:COM Port Cloced" ' 串口没打开时,提示串口关闭状态End IfExit SubErrorTrap: ' 错误处理Select Case Err.NumberCase comPortAlreadyOpen ' 如果串口已经打开,则提示MsgBox "没有发现此串口或被占用", 49, "串口调试助手"CloseComCase ElseMsgBox "没有发现此串口或被占用", 49, "串口调试助手"CloseComEnd SelectErr.ClearEnd SubPrivate Sub Comm_reSet(Port As Byte, BaudRate As String, ParityBit As String, DataBit As Integer,StopBit As Integer)On Error GoTo ErrorHint ' 错误则跳往错误处理If MSComm.PortOpen = True Then MSComm.PortOpen = False ' 先判断串口是否打开,如果打开则先关闭mPort = Port ' 设定端口MSComm.Settings = BaudRate & "," & ParityBit & "," & DataBit & "," & StopBit ' 设置波特率,无校验,8位数据位,1位停止位MSComm.PortOpen = True ' 打开串口If MSComm.PortOpen = True Thencmdswitch.Caption = "关闭串口"'ImgSwitch.Picture = LoadPicture("f:\我的VB\串口调试软件\图片\kai.jpg") ' 显示串口已经打开的图标ImgSwitchoff.Visible = Falsemnuconnect.Caption = "disconnect"ImgSwitchon.Visible = Truetxtstatus.Text = "STATUS:" & cbocom.Text & " OPEND," & cbobaudrate.Text & "," & Left(cboparitybit.Text, 1) & "," & cbodatabit.Text & "," & cbostopbit.TextElsecmdswitch.Caption = "打开串口"'ImgSwitch.Picture = LoadPicture("f:\我的VB\串口调试软件\图片\guan.jpg") ' 显示串口已经关闭的图标ImgSwitchon.Visible = FalseImgSwitchoff.Visible = Truetxtstatus.Text = "STATUS:COM Port Cloced"End IfExit SubErrorHint: ' 错误处理Select Case Err.NumberCase comPortAlreadyOpen ' 如果串口已经打开,则提示MsgBox "没有成功,请重试", vbExclamation, "串口调试助手"CloseCom ' 调用关闭串口函数Case ElseMsgBox "没有成功,请重试", vbExclamation, "串口调试助手"CloseCom ' 调用关闭串口函数End SelectErr.Clear ' 清除Err 对象的属性End SubPrivate Sub Command1_Click()End SubPrivate Sub cbobaudrate_Change()Call Comm_reSet(Val(Mid(cbocom.Text, 4, 2)), cbobaudrate.Text, Left(cboparitybit.Text, 1),cbodatabit.Text, cbostopbit.Text) '串口设置End SubPrivate Sub cbocom_Change()Call Comm_reSet(Val(Mid(cbocom.Text, 4, 2)), cbobaudrate.Text, Left(cboparitybit.Text, 1), cbodatabit.Text, cbostopbit.Text) '串口设置End SubPrivate Sub cbodatabit_Change()Call Comm_reSet(Val(Mid(cbocom.Text, 4, 2)), cbobaudrate.Text, Left(cboparitybit.Text, 1), cbodatabit.Text, cbostopbit.Text) '串口设置End SubPrivate Sub cboparitybit_Change()Call Comm_reSet(Val(Mid(cbocom.Text, 4, 2)), cbobaudrate.Text, Left(cboparitybit.Text, 1), cbodatabit.Text, cbostopbit.Text) '串口设置End SubPrivate Sub cbostopbit_Change()Call Comm_reSet(Val(Mid(cbocom.Text, 4, 2)), cbobaudrate.Text, Left(cboparitybit.Text, 1), cbodatabit.Text, cbostopbit.Text) '串口设置End SubPrivate Sub chkautosend_Click()On Error GoTo ErrIf ChkAutoSend.Value = 1 Then ' 如果有效则,自动发送If MSComm.PortOpen = True Then ' 串口状态判断mnuautosend.Caption = "取消自动发送"TmrAutoSend.Interval = Val(TxtAutoSendTime) ' 设置自动发送时间TmrAutoSend.Enabled = True ' 打开自动发送定时器ElseChkAutoSend.Value = 0 ' 串口没有打开去掉自动发送MsgBox "串口没有打开,请打开串口", 48, "串口调试助手" ' 如果串口没有被打开,提示打开串口End IfElseIf ChkAutoSend.Value = 0 Then ' 如果无效,不发送mnuautosend.Caption = "自动发送数据"TmrAutoSend.Enabled = False ' 关闭自动发送定时器End IfErr:End SubPrivate Sub cmdamend_Click()Dim spShell As Object ' 定义存放引用对象的变量Dim spFolder As Object ' 定义存放引用对象的变量Dim spFolderItem As Object ' 定义存放引用对象的变量Dim spPath As String ' 定义存放的变量On Error GoTo Err ' 错误处理,防止取消打开文件夹时报错Const WINDOW_HANDLE = 0Const NO_OPTIONS = 0Set spShell = CreateObject("Shell.Application")Set spFolder = spShell.BrowseForFolder(WINDOW_HANDLE, "选择目录:", NO_OPTIONS,"C:\Scripts")Set spFolderItem = spFolder.SelfspPath = spFolderItem.PathspPath = Replace(spPath, "\", "\") ' Replace函数的返回值是一个字符串txtsavepath.Text = spPath ' 把文件夹路径显示在标签上SaveTextPath = txtsavepath.Text ' 路径暂存Err:End SubPrivate Sub CmdClearCounter_Click()On Error GoTo ErrSendCount = 0 ' 发送计数器清零ReceiveCount = 0 ' 接收计数器清零txtRXcount.Text = "RX:" & 0 ' 接收计数txtTXcount.Text = "TX:" & 0 ' 发送计数Err:End SubPrivate Sub cmdclearrecieve_Click()TxtReceive.Text = ""End SubPrivate Sub cmdclearsend_Click()txtsend.Text = ""End SubPrivate Sub CmdHelp_Click()FrmHelp.ShowEnd SubPrivate Sub CmdQuit_Click()If MSComm.PortOpen = True Then MSComm.PortOpen = False ' 先判断串口是否打开,如果打开则先关闭Unload Me ' 卸载窗体,并退出程序EndEnd SubPrivate Sub cmdsavedisp_Click()On Error GoTo Err ' 错误处理SaveTextPath = txtsavepath ' 路径暂存Open txtsavepath & "\1.txt" For Output As #1 ' 打开文件' 不存在的话会创建文件,如已存在会覆盖' output 改为append 为追加' 改为input 则只读Print #1, Year(Date) & "年" & Month(Date) & "月" & Day(Date) & _"日" & Hour(Time) & "时" & Minute(Time) & "分" & Second(Time) & _"秒" & vbCrLf & TxtReceive.Text + vbCrLf ' 把接收区的文本保存文本前加上保存时间(0000年00月00日00时00分00秒)' vbcrlf 为回车换行Close #1 ' 关闭文件txtsavepath = "OK,1.txt Save" ' 提示保存成功cmdsavedisp.Enabled = FalseSavetime = Timer ' 记下开始的时间While Timer < Savetime + 5 ' 循环等待5 - 要延时的时间DoEvents ' 转让控制权,以便让操作系统处理其它的事件。
串口精灵源代码
for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
lstr=str[i];
//功能:若是在0-F之间的字符,则转换为相应的十六进制字符,否则返回-1
char CSCommTestDlg::ConvertHexChar(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
写出详细的编程过程,姑且叫串口调试助手源程序V1.0或VC串口通讯源程序吧,
我相信,如果你用VC编程,那么有了这个代码,就可以轻而易举地完成串口编
程任务了。(也许本文过于详细,高手就不用看)
开始吧:
串口调试助手代码分析42
第5章串口调试助手代码分析1、建立基于对话框的工程SCOMM2、绘制界面,如下图:接收区串口组合框:IDC_COMBO_COMSELECT,m_Com波特率组合框:IDC_COMBO_SPEED,m_Speed停止位组合框:IDC_COMBO_STOPBITS,m_StopBits数据位组合框:IDC_COMBO_DATABITS,m_DataBits校验位组合框:IDC_COMBO_PARITY,m_Parity十六进制显示(接收):IDC_CHECK_HEXRECIEVE,m_ctrlHexReceieve接收编辑框:IDC_EDIT_RECIVE ,m_ReceiveData m_ctrlReceiveData Style:Vertical Scroll MultiLine打开串口IDC_BUTTON_OPENPORT,m_ctrlOpenPort串口开关标志图标IDC _STATIC_OPENOFF,m_ctrlIconOpenoff数据文件保存路径IDC _EDIT_SA VEPATH,m_strCurPath保存显示数据文件路径IDC _EDIT_SA VEPATH, m_ctrlSavePath接收计数IDC_STATIC_RXCOUNT ,m_ctrlRXCOUNT发送区…。
3、添加CSeraiPort类文件将类文件SerialPort.h SerialPort.cpp 复制到工程所在文件夹中(选择改进后的类),然后单击VC 6.0菜单Projrct -> Add to Projrct ->Files… ,再在打开的文件选择对话框中选择SerialPort.h 和SerialPort.cpp ,点击OK,就把类文件加入当前工程,并在SCOMMDlg.h 中加入头文件,#include "SerialPort.h",通过上述步骤就在当前工程中加入了CSeraiPort类。
C#串口调试助手
//serialPort1.Parity = (Parity)Enum.Parse(typeof(Parityset), comboBox4.SelectedItem.ToString());
} public enum Parityset {
None, odd, Even, Mark, Space } private void comboBox5_SelectedIndexChanged(object sender, EventArgs e) //停止位 { serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), boBox5.Text); }
serialPort1.BaudRate = int.Parse(comboBox2.Text); serialPort1.DataBits = int.Parse(comboBox3.Text); serialPort1.Parity = (Parity)Enum.Parse(typeof(Parityset),
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
textBox3.Text = serialPort.ReadExisting();
label10.Text = (int.Parse(label10.Text.Trim()) + 1).ToString(); //统计接收区 数据 组数
串口调试软件代码
Private Sub Form_Load()MSComm1.Settings = "9600,n,8,1" ' 波特率9600bit/s,无校验,8位数据,1位停止位mPort = 4 '设定串口,1为com1,这里根据自己对应的com序号MSComm1.InBufferSize = 8 '设定返回接收缓冲区的大小,以字符为单位MSComm1.OutBufferSize = 2If MSComm1.PortOpen = True Then MSComm1.PortOpen = False '关串口MSComm1.RThreshold = 4 '设置并返回产生oncomm 事件的字符数,以字符为单位MSComm1.SThreshold = 1 '为1,传输缓冲区(发送)的每一个字符都会使MSCOMM 控件产生OnComm事件MSComm1.InputLen = 0 '设置从接收缓冲区读取的字数,为0读取整个缓冲区MSComm1.InputMode = comInputModeText '以文本方度接收If MSComm1.PortOpen = False Then MSComm1.PortOpen = TrueMSComm1.InBufferCount = 0Me.Caption = "温度"Text1.Text = "00.0"End SubPrivate Sub MSComm1_OnComm()Dim rec As StringSelect Case mEventCase comEvReceiverec = MSComm1.InputText1.Text = recMSComm1.InBufferCount = 0 '清空接收缓存区End SelectEnd SubOption ExplicitDim a As IntegerDim BytReceived() As ByteDim strData As StringDim lenInput As IntegerDim bytSendByte() As Byte '发送二进制数据Dim strSendText As String '发送文本数据Dim blnAutoSendFlag As BooleanDim openFlag As BooleanPrivate Sub cmdOpen_Click() '打开关闭端口If openFlag ThencmdOpen.Caption = "打开串口"MSComm1.PortOpen = False '打开端口Shape1.FillColor = vbRedLabel1 = "COM" & a & " 关闭"ElseOpenPortNumcmdOpen.Caption = "关闭串口"Shape1.FillColor = vbGreenLabel1 = "COM" & a & " 打开"End IfopenFlag = Not openFlagEnd SubPrivate Sub cmdSendASc_Click() '文本发送Dim sj_Txt As Stringsj_Txt = TxtSendIf MSComm1.PortOpen = True ThenMSComm1.Output = sj_TxtEnd IfEnd SubPrivate Sub cmdSendHex_Click() '16进制发送Dim sj() As ByteDim sj_Txt As StringDim i As Integersj_Txt = TxtSendIf Len(TxtSend) Mod 2 = 0 And Len(TxtSend) <> 0 Then '检验16进制字符串长ReDim sj(Len(sj_Txt) / 2 - 1)For i = 0 To Len(sj_Txt) - 1 Step 2sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))NextIf MSComm1.PortOpen = True ThenMSComm1.Output = sjElseMSComm1.PortOpen = TrueShape1.FillColor = vbGreenMSComm1.Output = sjEnd IfElseMsgBox ("格式不对!")End IfEnd Sub'字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数Private Sub OpenPortNum()On Error GoTo uerrorFor a = 1 To 16mPort = aMSComm1.PortOpen = TrueIf MSComm1.PortOpen = True Then'Print "可用Com号= "; aExit SubElseEnd IfNextExit Subuerror:'Print "出错或占用Com号= "; aResume NextEnd SubPrivate Sub Form_Load()MSComm1.Settings = "9600,n,8,1"MSComm1.InputMode = comInputModeBinary '采用二进制传输MSComm1.InBufferCount = 0 '清空接受缓冲区MSComm1.OutBufferCount = 0 '清空传输缓冲区MSComm1.RThreshold = 1 '产生MSComm事件MSComm1.InBufferSize = 1024TxtSend = ""TxtSend = "" '800A00113135323634389794"txtReceive = ""Text2 = ""Call cmdOpen_ClickEnd SubPrivate Sub MSComm1_OnComm() '接收数据Dim strBuff As StringSelect Case mEventCase 2MSComm1.InputLen = 0strBuff = MSComm1.InputBytReceived() = strBuffjieshoulenInput = Len(strData)Text2 = lenInput \ 2'数据处理代码End SelectEnd SubPublic Function jieshou() '接收数据处理为16进制Dim i As IntegerFor i = 0 To UBound(BytReceived)If Len(Hex(BytReceived(i))) = 1 ThenstrData = strData & "0" & Hex(BytReceived(i)) ElsestrData = strData & Hex(BytReceived(i))End IfNexttxtReceive = strDataEnd Function。
c#上位机串口通信助手源代码详解
c#上位机串口通信助手源代码实例详解一、功能1软件打开时,自动检测有效COM端口2 软件打开时,自动复原到上次关闭时的状态3 不必关闭串口,即可直接进行更改初始化设置内容(串口号、波特率、数据位、停止位、校验位),可按更改后的信息自动将串口重新打开4 可统计接收字节和发送字节的个数5 接收数据可按16进制数据和非16进制数据进行整体转换6 可将接收到数据进行保存7 可设置自动发送,发送时间可进行实时更改8可按字符串、16进制字节、文件方式进行发送,字符串和16进制字节可分别进行存储,内容互不干扰9 按16进制发送时,可自动校验格式,不会输错10 可清空发送或接收区域的数据二、使用工具Visual Studio2015三、程序详解1 界面创建图1用winform创建如图1所示界面,控件名字分别为:端口号:cbxCOMPort 波特率:cbxBaudRate数据位:cbxDataBits 停止位:cbxStopBits校验位:label5 打开串口按钮:btnOpenCom发送(byte):tbSendCount 接收(byte):tbReceivedCount 清空计数按钮:btnClearCount 按16进制显示:cb16Display接收区清空内容按钮:btnClearReceived 保存数据按钮:btnSaveFile接收数据框:tbReceivedData 发送数据框:tbSendData自动发送:cbAutomaticSend 间隔时间:tbSpaceTime按16进制发送:cb16Send 发送区清空内容按钮:btnClearSend 读入文件按钮:btnReadFile 发送按钮:btnSend2 创建一个方法类按Ctrl+shift+A快捷键创建一个类,名字叫Methods,代码为:using System;using System.Collections;using System.Collections.Generic;using System.IO.Ports;using System.Linq;using System.Text;using System.Threading.Tasks;namespace串口助手sdd{class Methods{//获取有效的COM口public static string[] ActivePorts(){ArrayList activePorts = new ArrayList();foreach (string pname in SerialPort.GetPortNames()){activePorts.Add(Convert.ToInt32(pname.Substring(3)));}activePorts.Sort();string[] mystr = new string[activePorts.Count];int i = 0;foreach (int num in activePorts){mystr[i++] = "COM" + num.ToString();}return mystr;}//16进制字符串转换为byte字符数组public static Byte[] _16strToHex(string strValues){string[] hexValuesSplit = strValues.Split(' ');Byte[] hexValues = new Byte[hexValuesSplit.Length];Console.WriteLine(hexValuesSplit.Length);for (int i = 0; i < hexValuesSplit.Length; i++){hexValues[i] = Convert.ToByte(hexValuesSplit[i], 16);}return hexValues;}//byte数组以16进制形式转字符串public static string ByteTo16Str(byte[] bytes){string recData = null;//创建接收数据的字符串foreach (byte outByte in bytes)//将字节数组以16进制形式遍历到一个字符串内 {recData += outByte.ToString("X2") + " ";}return recData;}//16进制字符串转换字符串public static string _16strToStr(string _16str){string outStr = null;byte[] streamByte = _16strToHex(_16str);outStr = Encoding.Default.GetString(streamByte);return outStr;}}}2 Form1.cs的代码为:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.IO.Ports;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;using System.Collections;namespace串口助手sdd{public partial class Form1 : Form{//声明变量SerialPort sp = new SerialPort();bool isSetProperty = false;//串口属性设置标志位private enum PortState//声明接口显示状态,枚举型{打开,关闭}string path = AppDomain.CurrentDomain.BaseDirectory + "confing.ini";//声明配置文件路径string tbSendDataStr = "";//发送窗口字符串存储string tbSendData16 = "";//发送窗口16进制存储List<byte> receivedDatas = new List<byte>();//接收数据泛型数组//接收串口数据private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) {byte[] ReceivedData = new byte[sp.BytesToRead];//创建接收字节数组sp.Read(ReceivedData, 0, ReceivedData.Length);//读取所接收到的数据receivedDatas.AddRange(ReceivedData);tbReceivedCount.Text = (Convert.ToInt32(tbReceivedCount.Text) + ReceivedData.Length).ToString();if (cb16Display.Checked)tbReceivedData.Text = Methods.ByteTo16Str(receivedDatas.ToArray());elsetbReceivedData.Text =Encoding.Default.GetString(receivedDatas.ToArray());sp.DiscardInBuffer();//丢弃接收缓冲区数据}//发送串口数据private void DataSend(){try{if (cb16Send.Checked){byte[] hexBytes = Methods._16strToHex(tbSendData16);sp.Write(hexBytes, 0, hexBytes.Length);tbSendCount.Text = (Convert.ToInt32(tbSendCount.Text) + hexBytes.Length).ToString();}else{sp.WriteLine(tbSendDataStr);tbSendCount.Text = (Convert.ToInt32(tbSendCount.Text) + tbSendDataStr.Length).ToString();}}catch (Exception ex){MessageBox.Show(ex.Message.ToString());return;}}//设置串口属性private void SetPortProperty(){sp.PortName = cbxCOMPort.Text.Trim();//设置串口名sp.BaudRate = Convert.ToInt32(cbxBaudRate.Text.Trim());//设置波特率switch (cbxStopBits.Text.Trim())//设置停止位{case"1": sp.StopBits = StopBits.One; break;case"1.5": sp.StopBits = StopBits.OnePointFive; break;case"2": sp.StopBits = StopBits.Two; break;default: sp.StopBits = StopBits.None; break;}sp.DataBits = Convert.ToInt32(cbxDataBits.Text.Trim());//设置数据位switch (cbxParity.Text.Trim())//设置奇偶校验位{case"无": sp.Parity = Parity.None; break;case"奇校验": sp.Parity = Parity.Odd; break;case"偶校验": sp.Parity = Parity.Even; break;default: sp.Parity = Parity.None; break;}sp.ReadTimeout = 5000;//设置超时时间为5sControl.CheckForIllegalCrossThreadCalls = false;//这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性)//定义DataReceived事件的委托,当串口收到数据后出发事件sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);}//设置端口显示状态private void DisplayPortState(PortState portState){toolStripStatusLabel1.Text = cbxCOMPort.Text + "端口处于" + portState + "状态 " + cbxBaudRate.Text + " " + cbxDataBits.Text + " " + cbxStopBits.Text + " " + cbxParity.Text;}//重新打开串口private void AgainOpenPort(){if (sp.IsOpen){try{sp.Close();SetPortProperty();isSetProperty = true;sp.Open();}catch (Exception){isSetProperty = false;btnOpenCom.Text = "打开串口";DisplayPortState(PortState.关闭);MessageBox.Show("串口无效或已被占用!", "错误提示");return;}DisplayPortState(PortState.打开);}else{DisplayPortState(PortState.关闭);}}public Form1(){InitializeComponent();}//软件启动时加载事件private void Form1_Load(object sender, EventArgs e){#region加载配置文件Hashtable ht = new Hashtable();if (File.Exists(path)){try{string myline = "";string[] str = new string[2];using (StreamReader sr = new StreamReader(path)) {myline = sr.ReadLine();while (myline != null){str = myline.Split('=');ht.Add(str[0], str[1]);myline = sr.ReadLine();}}}catch(Exception ex){MessageBox.Show(ex.Message.ToString());}}#endregion#region设置窗口为固定大小且不可最大化this.MaximumSize = this.Size;this.MinimumSize = this.Size;this.MaximizeBox = false;#endregion#region列出常用的波特率cbxBaudRate.Items.Add("1200");cbxBaudRate.Items.Add("2400");cbxBaudRate.Items.Add("4800");cbxBaudRate.Items.Add("9600");cbxBaudRate.Items.Add("19200");cbxBaudRate.Items.Add("38400");cbxBaudRate.Items.Add("43000");cbxBaudRate.Items.Add("56000");cbxBaudRate.Items.Add("57600");cbxBaudRate.Items.Add("115200");if (ht.ContainsKey("cbxBaudRate"))cbxBaudRate.SelectedIndex =cbxBaudRate.Items.IndexOf(ht["cbxBaudRate"].ToString());elsecbxBaudRate.SelectedIndex = 3;cbxBaudRate.DropDownStyle = ComboBoxStyle.DropDownList; #endregion#region列出停止位cbxStopBits.Items.Add("1");cbxStopBits.Items.Add("1.5");cbxStopBits.Items.Add("2");if (ht.ContainsKey("cbxStopBits"))cbxStopBits.SelectedIndex =cbxStopBits.Items.IndexOf(ht["cbxStopBits"].ToString());elsecbxStopBits.SelectedIndex = 0;cbxStopBits.DropDownStyle = ComboBoxStyle.DropDownList; #endregion#region列出数据位cbxDataBits.Items.Add("8");cbxDataBits.Items.Add("7");cbxDataBits.Items.Add("6");cbxDataBits.Items.Add("5");if (ht.ContainsKey("cbxDataBits"))cbxDataBits.SelectedIndex =cbxDataBits.Items.IndexOf(ht["cbxDataBits"].ToString());elsecbxDataBits.SelectedIndex = 0;cbxDataBits.DropDownStyle = ComboBoxStyle.DropDownList; #endregion#region列出奇偶校验位cbxParity.Items.Add("无");cbxParity.Items.Add("奇校验");cbxParity.Items.Add("偶校验");if (ht.ContainsKey("cbxParity"))cbxParity.SelectedIndex =cbxParity.Items.IndexOf(ht["cbxParity"].ToString());elsecbxParity.SelectedIndex = 0;cbxParity.DropDownStyle = ComboBoxStyle.DropDownList; #endregion#region COM口重新加载cbxCOMPort.Items.Clear();//清除当前串口号中的所有串口名称 cbxCOMPort.Items.AddRange(Methods.ActivePorts());if (ht.ContainsKey("cbxCOMPort") &&cbxCOMPort.Items.Contains(ht["cbxCOMPort"].ToString()))cbxCOMPort.SelectedIndex =cbxCOMPort.Items.IndexOf(ht["cbxCOMPort"].ToString());elsecbxCOMPort.SelectedIndex = 0;cbxCOMPort.DropDownStyle = ComboBoxStyle.DropDownList; #endregion#region初始化计数器tbSendCount.Text = "0";tbSendCount.ReadOnly = true;tbReceivedCount.Text = "0";tbReceivedCount.ReadOnly = true;#endregion#region初始化当前时间toolStripStatusLabel3.Text = DateTime.Now.ToString();#endregion#region初始化串口状态toolStripStatusLabel1.ForeColor = Color.Blue;if (!isSetProperty)//串口未设置则设置串口属性{SetPortProperty();isSetProperty = true;}try{sp.Open();btnOpenCom.Text = "关闭串口";DisplayPortState(PortState.打开);}catch (Exception){//串口打开失败后,串口属性设置标志位设为falseisSetProperty = false;MessageBox.Show("串口无效或已被占用!", "错误提示"); }#endregion#region初始化间隔时间if (ht.ContainsKey("tbSpaceTime"))tbSpaceTime.Text = ht["tbSpaceTime"].ToString();elsetbSpaceTime.Text = "1000";#endregion#region初始化按16进制显示状态if (ht.ContainsKey("cb16Display") && ht["cb16Display"].ToString() == "True") cb16Display.Checked = true;elsecb16Display.Checked = false;#endregion#region初始化按16进制发送状态if (ht.ContainsKey("cb16Send") && ht["cb16Send"].ToString() == "True")cb16Send.Checked = true;elsecb16Send.Checked = false;#endregion#region初始化发送区文本if(ht.ContainsKey("tbSendData16") && ht.ContainsKey("tbSendDataStr")){tbSendData16 = ht["tbSendData16"].ToString();tbSendDataStr = ht["tbSendDataStr"].ToString();if (cb16Send.Checked)tbSendData.Text = ht["tbSendData16"].ToString();elsetbSendData.Text = ht["tbSendDataStr"].ToString();}#endregiontbSendData.Focus();}//显示当前时间private void timer1_Tick(object sender, EventArgs e){toolStripStatusLabel3.Text = DateTime.Now.ToString();}//点击打开串口按钮private void btnOpenCom_Click(object sender, EventArgs e){if (!sp.IsOpen)//串口没有打开时{if (!isSetProperty)//串口未设置则设置串口属性{SetPortProperty();isSetProperty = true;}try{sp.Open();btnOpenCom.Text = "关闭串口";DisplayPortState(PortState.打开);}catch (Exception){//串口打开失败后,串口属性设置标志位设为falseisSetProperty = false;MessageBox.Show("串口无效或已被占用!", "错误提示"); }}else//串口已经打开{try{sp.Close();isSetProperty = false;btnOpenCom.Text = "打开串口";DisplayPortState(PortState.关闭);}catch (Exception){MessageBox.Show("关闭串口时发生错误", "错误提示"); }}}//发送串口数据private void btnSend_Click(object sender, EventArgs e){if (tbSendData.Text.Trim() == "")//检测发送数据是否为空{MessageBox.Show("请输入要发送的数据!", "错误提示");return;}if (sp.IsOpen){DataSend();}else{MessageBox.Show("串口未打开!", "错误提示");}}//点击端口号选择下拉框按钮private void cbxCOMPort_SelectedIndexChanged(object sender, EventArgs e){AgainOpenPort();}//点击波特率选择下拉框按钮private void cbxBaudRate_SelectedIndexChanged(object sender, EventArgs e) {AgainOpenPort();}//点击数据位选择下拉框按钮private void cbxDataBits_SelectedIndexChanged(object sender, EventArgs e) {AgainOpenPort();}//点击停止位选择下拉框按钮private void cbxStopBits_SelectedIndexChanged(object sender, EventArgs e) {AgainOpenPort();}//点击校验位选择下拉框按钮private void cbxParity_SelectedIndexChanged(object sender, EventArgs e){AgainOpenPort();}//点击数据接收区清空按钮private void btnClearReceived_Click(object sender, EventArgs e){receivedDatas.Clear();tbReceivedData.Text = "";}//点击是否按16进制显示接收数据private void cb16Display_CheckedChanged(object sender, EventArgs e){if (cb16Display.Checked)tbReceivedData.Text = Methods.ByteTo16Str(receivedDatas.ToArray());elsetbReceivedData.Text =Encoding.Default.GetString(receivedDatas.ToArray());//点击是否按16进制发送数据private void cb16Send_CheckedChanged(object sender, EventArgs e){if (cb16Send.Checked){tbSendData.Text = tbSendData16;}else{tbSendData.Text = tbSendDataStr;}}//发送文本框键盘按键检测private void tbSendData_KeyPress(object sender, KeyPressEventArgs e){if (cb16Send.Checked){//正则匹配string pattern = "[0-9a-fA-F]|\b";//\b:退格键Match m = Regex.Match(e.KeyChar.ToString(), pattern);if (m.Success){if(e.KeyChar != '\b'){if (tbSendData.Text.Length % 3 == 2){tbSendData.Text += " ";tbSendData.SelectionStart = tbSendData.Text.Length;}e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper()); }e.Handled = false;}else{e.Handled = true;}}else{e.Handled = false;}}//点击清空发送内容private void btnClearSend_Click(object sender, EventArgs e){tbSendData.Text = "";if (cb16Send.Checked)tbSendData16 = "";elsetbSendDataStr = "";//点击清空计数器数据private void btnClearCount_Click(object sender, EventArgs e){tbReceivedCount.Text = "0";tbSendCount.Text = "0";}//点击是否设置自动发送private void cbAutomaticSend_CheckedChanged(object sender, EventArgs e) {if (cbAutomaticSend.Checked){timer2.Enabled = true;timer2.Interval = Convert.ToInt32(tbSpaceTime.Text);}else{timer2.Enabled = false;}}//自动发送时间文本框键盘按键检测private void tbSpaceTime_KeyPress(object sender, KeyPressEventArgs e) {//正则匹配string pattern = "[0-9]|\b";Match m = Regex.Match(e.KeyChar.ToString(),pattern);if (m.Success){timer2.Interval = Convert.ToInt32(tbSpaceTime.Text);e.Handled = false;}else{e.Handled = true;}}//串口显示状态private void timer2_Tick(object sender, EventArgs e){if (sp.IsOpen){DataSend();}else{timer2.Enabled = false;cbAutomaticSend.Checked = false;MessageBox.Show("串口未打开!", "错误提示");return;}if (tbSendData.Text.Trim() == "")//检测发送数据是否为空{timer2.Enabled = false;cbAutomaticSend.Checked = false;MessageBox.Show("请输入要发送的数据!", "错误提示");return;}}//关闭窗口时出发的事件private void Form1_FormClosed(object sender, FormClosedEventArgs e){try{using (StreamWriter sw = new StreamWriter(path, false)){sw.WriteLine("cbxCOMPort=" + cbxCOMPort.Text);sw.WriteLine("cbxBaudRate=" + cbxBaudRate.Text);sw.WriteLine("cbxDataBits=" + cbxDataBits.Text);sw.WriteLine("cbxStopBits=" + cbxStopBits.Text);sw.WriteLine("cbxParity=" + cbxParity.Text);sw.WriteLine("cb16Display="+ cb16Display.Checked.ToString()); sw.WriteLine("tbSpaceTime=" + tbSpaceTime.Text);sw.WriteLine("cb16Send=" + cb16Send.Checked.ToString());sw.WriteLine("tbSendDataStr=" + tbSendDataStr);sw.WriteLine("tbSendData16=" + tbSendData16);sp.Close();}}catch(Exception ex){MessageBox.Show(ex.Message.ToString());}}//发送文本框按键抬起时出发的事件private void tbSendData_KeyUp(object sender, KeyEventArgs e){if (cb16Send.Checked){tbSendData16 = tbSendData.Text.Trim();}else{tbSendDataStr = tbSendData.Text;}}//点击读入文件按钮private void btnReadFile_Click(object sender, EventArgs e){openFileDialog1.Filter = "所有文件(*.*)|*.*";//文件筛选器的设定openFileDialog1.FilterIndex = 1;openFileDialog1.Title = "选择文件";openFileDialog1.FileName = "";openFileDialog1.ShowHelp = true;if(openFileDialog1.ShowDialog() == DialogResult.OK){using (FileStream fs = newFileStream(openFileDialog1.FileName,FileMode.Open)){byte[] bufferByte = new byte[fs.Length];fs.Read(bufferByte, 0, Convert.ToInt32(fs.Length));sp.Write(bufferByte, 0, bufferByte.Length);tbSendCount.Text = (Convert.ToInt32(tbSendCount.Text) + bufferByte.Length).ToString();}}}//点击保存数据按钮private void btnSaveFile_Click(object sender, EventArgs e){saveFileDialog1.Filter = "所有文件(*.*)|*.*";if(saveFileDialog1.ShowDialog() == DialogResult.OK){string fName = saveFileDialog1.FileName;using(FileStream fs = File.Open(fName, FileMode.Append)){fs.Write(receivedDatas.ToArray(), 0, receivedDatas.Count);}}}}}需要源代码或有疑问的c#爱好者们,欢迎加入c#技术交流群(33647125),附加信息为”我以下载此文档”,进群后找群主索要源代码或进行技术交流。
基于WIN32API的串口调试助手
基于WIN32 API的串口通讯软件的设计收藏串口调试助手源代码:/user/kissyfish1、串口的应用随着计算机技术的发展及工业自动化水平的提高,在许多场合采用单机控制已不能满足现场要求,因而必须采用多机控制的形式。
串行通信作为计算机之间常用的通信方法之一,由于其通信编程灵活、硬件简洁并遵循统一的标准,而在工业控制领域得到了广泛的应用。
2、串口的属性2.1波特率波特率即数据传送速率,表示每秒钟传送二进制代码的位数,他的单位是bit/s。
波特率对于CPU与外界的通信是很重要的。
通讯过程中,必须保证上位机和下位机的波特率一致。
如果不一致的话,就有可能出现乱码甚至出现丢包。
这个就好如一个蓄水池的进水管道和出水管道,如果进水管道流入水的速度太快而出水管道的出水太慢,这样时间长了水就会溢出,反映在串口通讯上就是丢包。
2.2数据帧在异步通信中,数据是一帧一帧(包括一个字符或一个字节数据)传送的,每一帧数据的格式如下表所示。
起始位数据位奇偶校验位停止位5-8位可省1在帧格式中,一个字符由四个部分组成:起始位、数据位、奇偶校验位和停止位。
首先是一个起始位(0),然后是5-8位数据(规定低位在前,高位在后),接下来是奇偶校验位(可省略),最后是停止位(1)。
起始位(0)信号只占用一位,用来通知设备一个待接收的数据准备到达。
线路上在不传送字符时应保持为1。
接收端不断检测线路的状态,若连续为以后又测得一个0,就知道后来一个新字符,应该马上接收。
字符的起始位还被用作同步接收端的时钟,以保证以后的接收能正确进行。
奇偶校验位只占一位,但在字符中也可以规定不用奇偶校验位,这一位可以省去。
也可以用这一位来确定这一帧中的字符所代表信息的性质(地址数据等)。
停止位用来表示数据的结束,它一定四高电位(1)。
停止位可以是1位、1.5位或2位。
接收端收到停止位后,知道上一字符已传送完毕,同时为准备接收下字符作好准备。
只要接收到0,就是新的字符的起始位。
(串口通信编程)开源串口调试助手Common(ComMonitor)
(串⼝通信编程)开源串⼝调试助⼿Common(ComMonitor) 最新的⽂档见这⾥: 我写的串⼝调试助⼿程序以及源代码. 警告:请勿将本软件源代码⽤于任何商业⽤途。
其实吧, 我写的这个串⼝软件绝对⽐⽹上流传的⼀些串⼝调试软件好⽤很多, 对于软件,我本⾝并没有作 太多推⼴, ⼀些功能我由于懒也没有⽂档化; 如果您正在使⽤, 也觉得我说得没错, 还望您帮忙推⼴⼀下. 感激!本程序最近正在使⽤C++完全重构, github上⾯的源代码⽆法通过编译~~ 敬请期待最新版本!微云你就⼀坑货, 说了的链接永久有效呢?源代码: 声明:本软件为开源软件,绝对不包含任何对系统不利的代码,对于360的那种⾏为,我不想说什么. 软件界⾯:-------------------------------------------------------------------------------------------------------------------------说明: 更新信息可能不会再更新到这⾥, 请到上⾯查看. 更新:2014-07-07:①优化:接收数据时,如果最后⼀个字符是'\r'或'\n',则会在⼀定时间内等待接收所有'\r','\n'字符,避免导致读取'\r\n'可能造成的两次换⾏②更改:除已经提到的那三种情况, 还有使⽤'\r\n\r'三个字符作为换⾏符使⽤的, 已增加⽀持2014-07-07:①修复:回车换⾏字符'\r','\n','\r\n'均能达到换⾏效果②修复:⼀开始"忽略回车"等⽆法点击问题③优化:简化对数据中包含'\0'的数据的处理④增加:简洁界⾯模式时把⼯具栏放到左边⑤更改:只显⽰接收区按下的"回车"字符为"<Enter>",其它不显⽰⑥增加:接收区增加⼀个"清空数据"菜单2014-07-06 1.17:①增加:允许从接收区输⼊字符并发送(更友好的类交互模式) 会显⽰的字符包括: ' ' <Space> '\b' <Backspace> '\t' <Tab> '\r' <Enter> 感觉输⼊效果还不错:②更改:更改了字符接收区/发送区的字体为Consolas等宽字体,不再使⽤原来的Courier字体③增加:简洁模式 - 此模式下⼤部分界⾯元素会被隐藏, 有时候这样更舒服 见上图所⽰, 左下⾓为"简洁模式"开关, 开启简洁模式后就像上图, 是不是很简洁?④增加:主窗⼝的⾃动界⾯布局(允许拖动改变窗⼝⼤⼩,控件坐标⾃动调整)⑤更改:默认使⽤的模式改为:字符接收+字符发送2014-07-05 1.16 (未对外发布):①字符接收数据时,增加对控制字符Backspace的⽀持(即'\b'),效果就是向前删除⼀个字符②修复⼀处中⽂检测错误(原来是对的, 不知道什么时候改错了③删除了窗体⼤⼩调整(下个版本即将使⽤⾃动布局)2014-03-03 1.15: 更新:⽀持⼿动输⼊⾮标准波特率 改进:增加了⼏个功能快捷键,⽐如Alt+S为发送,.... 优化:修正:如果接收缓冲区有未显⽰的数据,则会在按下继续显⽰时进⾏提⽰,⽽不是原来的在接收到下⼀次的数据时进⾏提⽰; 2013-11-02 1.14: 更新:修改了原来的命令发送模式的界⾯,现在变得更加友好了 如下图, 先选择要发送的命令列表⽂件: 点击打开, 将会打开如下界⾯: 1.上⾯的代码是我测试蓝⽛芯⽚⽤到的命令列表⽂件, 点击"发送命令"就会发送对应的⼀条命令 2.16进制还是原来的要求, 2个⼀组即可; 对于字符数据:默认开启转义字符功能, ⽐如 '\n' 就代表换⾏, ⽀持字符型转义字符有6个, \n,\r,\\,\t,\a,\v,\b, 那些可见的字符不需要转义, ⽐如引号,问号... ⽀持的16进制转义字符格式:\x?? - 其中, ⼀个?代表⼀个16进制字符, 以上4个字符组成⼀个16进制值,缺⼀不可 3.⽂本框中的数据可以在发送时随时修改,回车键可看到效果, (但修改的内容⽬前不能更新到原来的本地⽂件上) 4.注意到数据类型后⾯的"双击改变"⼏个字样, 也就是说通过双击, 可以改变那条命令的解析格式 5.数据⼤⼩是指最终要被发送的数据的⼤⼩, ⽽不是转义前的⼤⼩ 命令⽂件的格式举例: C:初始化SPP库:at+init\r\n C:查询任意访问码的蓝⽛设备:at+iac=9e8b33\r\n C:查询设备类为0x1f1f的蓝⽛设备:at+class=1f1f\r\n C:设置查询模式:at+inqm=1,9,48\r\n C:过滤,查询周边蓝⽛设备:at+inq\r\n H:16进制测试:12 45 67 4f 分为3部分组成, 两者之间⽤冒号隔开: 第1部分:C或者H(⼤写), 代表数据是16进制还是字符数据 第2部分:命令的名字 第3部分:数据对应的数据 BUG修复: 上⼀个版本在发送数据前忘记检查串⼝是否已经打开了, 已修复~----------------------------------------------------------------------------------------------------------2012-12-24 1.0.0.0:发布第1个版本2012-12-26:⾃动识别当前存在,插⼊,移除的串⼝号2013-01-11 1.0.0.1:增加保存接收区数据到⽂件(16进制/⽂本形式)增加从⽂件读数据到发送区(16进制/⽂本形式)增加暂停显⽰功能增加复制发送/接收区数据到剪贴板2013-01-18 1.0.0.2:修复:⽂本⽂件,16⼆进制⽂件读取错误修复:程序内部缓冲区满后使程序进⼊死循环修复:⽂本字符⽅式显⽰接收的数据时产⽣不正确的换⾏符的错误,若要产⽣换⾏符, 请使⽤"\n"2013-02-08 1.0.0.3:内部程序作了许多的优化⼯作,包含数据的发送⽅式等修复接收数据时⿏标在接收区的⽂本选择造成的⼲扰2013-02-14 1.0.0.4:增加显⽰出0~127号ASCII对应8,10,16进制功能2013-02-24 1.0.0.5,今天元宵节:更改原来的1~64串⼝列表到⾃动检测计算机上的可⽤串⼝2013-02-27 1.0.0.6:若发送⽂本,则⾃动发送被⾃动取消(若⾃动发送选项已打开)在显⽰模式下不允许对接收区数据进⾏选择操作提供硬件⽀持的串⼝设备设置为⽤户提供串⼝超时设置提供⼿动设置DTR/RTS引脚电平2013-03-01 1.0.0.7:修改原计算器(系统)为表达式求值计算器(简单版本)2013-03-03:添加:<其它>菜单添加<设备管理器>修改:在关闭串⼝后⾃动发送前⾯的钩不再⾃动取消(如果已经选中)修改:串⼝被关闭/移除后串⼝列表回到第⼀个串⼝设备的BUG2013-03-04:修改:现在在串⼝列表中可以显⽰串⼝在设备管理器中的名字了修正:⽆法显⽰ MSP430-FETUIF Debugger 的串⼝号(现在调⽤SetupApi更新列表)2013-03-05:为了⽅便数据的统计与显⽰,16进制内容与字符内容被显⽰到不同的编辑框中2013-03-09 1.0.0.8:修正在使⽤SetupApi枚举串⼝设备时未检测并⼝设备⽽造成的内存异常访问错误减少在某些波特率(如:19200bps)下丢包严重的情况(如:MSP430串⼝),有时候还是会发⽣,等待修复.某些软件(如:SComAssistant采⽤每次只读⼀个字节的办法效果还⾏, 就是速度有点慢. 我改成了WaitCommEvent函数调⽤了(原来是Pending ReadFile),减少了CPU占⽤(有些串⼝驱动并不总是⽀持同步操作.以前只管ReadFile+输出nRead字节,这⾥错误,ReadFile并不保证读取到要求的数据量后才返回,这⾥会导致严重丢包,WriteFile亦然.速度减慢,但数据更完整2013-03-10 1.0.0.9:修正:因为在格式化字符串的最后少写了⼀句 *pb = '\0',导致接收区数据显⽰错误! 修复:对utils.hex2chs和add_text作了⼤量修改,⼤⼤减少数据丢包,貌似没有丢包?,细节处理参见源程序 1.0.0.8版本因为内部原因速度严重减慢, 1.0.0.9回到原来的快速!2013-03-18:更正:若为字符显⽰⽅式,16进制⽅式保存不被允许,因为格式基本上不满⾜!2013-03-23 1.10:添加:⼯作模式中,右键点击接收区字符⽂本框可以使能中⽂显⽰模式(不推荐),由于中⽂字符由两个字节构成,所以:⼀旦在某⼀次接收过程中只接收到了中⽂字符的⼀个字节,那么数据就会显⽰出错, 这个⽆法避免, 所以建议尽量不使能中⽂显⽰模式.修正:⽤C语⾔的⼈们都习惯使⽤'\n'作为换⾏符,我也这样使⽤,但偏偏Windows的编辑框以'\r\n'作为换⾏符,没有办法,我不得不把所有的'\n'换成'\r\n',效率必然会下降,⽽且我不得不计算出\n的个数先 --> 为了计算所需缓冲区的⼤⼩.添加:现在可以显⽰出还未被发送出去的数据计数.添加:新增计时器,打开串⼝后开始计时,关闭后停⽌计时.2013-03-25:修正:⼤⼤减少中⽂乱码的问题.细节处理见代码.增加:字符串转16进制数组功能,⼯具菜单⾥⾯.2013-04-04:修正:⽆法复制接收区字符⽂本的BUG.⼩提⽰:在选择串⼝时,如果没有任何可⽤的串⼝,则进⾏提⽰更新.2013-04-07:修改:完全修改了utils.str2hex的实现,⼤⼤增加了16进制输⼊的灵活性.现在的要求:每个16进制值由两个相邻字符组成,⽆其它限制.(以前是2个相邻字符+⼀个空格)2013-04-11:发送字符数据时,对于换⾏,只发送'\n',不再发送'\r\n',注意:结尾的'\0\'不被发送!2013-04-13:修正:更改发送与接收⽅式为异步⽅式.添加:简单波形显⽰.2013-04-23:修正:发送与接收⽅式改回同步⽅式!坑~修正:当发送操作达到100次时⽆法继续发送的BUG!修改:优化内部线程同步机制,避免程序停⽌⼯作(失去响应)!优化:优化⾃动发送数据的⽅式,提⾼精度,减⼩内存/CPU占⽤!⼩提⽰:在加载/保存⽂件时,若不清楚打开/保存⽅式,可以查看简单的帮助信息!2013-05-11:明天母亲节修正:终于找到⼀个⽐较好的办法来处理⾃动发送⽤到的重复数据了,呵呵,时间下限减少到10ms2013-07-05:临时修正:选择从⽂件加载并取消后, 串⼝号选择的ComboBox会消失不见,不知道原因,临时已解决2013-07-14:改进:程序内部改进内存分配算法,避免因程序错误造成内存泄漏2013-07-20 1.12:2013-09-10 1.13 今天教师节:增加:现在可以⼿动编写待发送的命令⽂件,并发送命令了 - 在发送⽂件时选择命令⽂件, 格式见博客后⾯的介绍增加:字符发送模式下,可以选择取消回车换⾏符的发送,可以选择插⼊转义字符 1.⽀持的字符型转义字符:\r,\n,\t,\v,\a,\b,\\2.⽀持的16进制转义字符格式:\x?? - 其中⼀个问号代表⼀个16进制字符, 不可省略其⼀,必需保证4个字符的格式3.'?',''','"', 等print-able字符不需要转义 ⼥孩不哭 @ 2012-12-27 14:47:59 @。
串口调试助手VC源程序及详细编程过程
据数留残除清以区冲缓读预先//;)(tupnIteG.mmoClrtc_m 0为度长据数区收接前当置设// ;)0(neLtupnIteS.mmoClrtc_m 件事mmoCnO的据数收接个一发引将时符字个1于等或于多有中区冲缓收接口串当每示表1数参// ; ) 1 ( d l o h s er h T R t e S . m m oCl r t c _ m ; ) 1 ( e d o M tu p n I t e S . m m oCl r t c _ m 据数取检式方制进二以 // 位止停个1�位据数个8�验校无�0069率特波// ;)"1,8,n,0069"(sgnitteSteS.mmoClrtc_m ; )" t r o p l a i r es n e p o t o nn a c " ( x o B e g ass e M x f A esle 口串开打//;)EURT(nepOtroPteS.mmoClrtc_m ) )( n e p O t r o P te G . m m o C l r t c_m ! ( f i 1moc择选// ;)1(troPmmoCteS.mmoClrtc_m ; )E S L A F ( n e p Ot r o P t e S . m m oCl r t c _ m ) ) ( n e p O t r oP t e G . m m o C l rtc _ m ( f i e r e h n o i t az i l a i t i n i a r t x e d d A :OD O T / / �码代下如入加�口串开打)(golaiDtinInO::glDtseTmmoCSC的框话对主在们我在现。口串开打中数函 理处的钮按该在�钮按始开个一做中序程在如例�口串开打候时的要需你在以可你 数 参 口 串 置 设 和 口 串 开 打 . 6
VC编写串口调试助手(含VC6工程源文件)
纯业余者用VC(MFC)编写串口调试助手1.序毕业到现在,转眼就做射频开发10年了,一直从事直放站、干放等通信边缘行业,从低噪放、锁相源、选频、功放到整机,射频就那么点东西,而且越来越集成化,软件无线电是必然趋势。
做射频从业面会越来越窄,我知道所有人都会说,当你成为专家的时候,一切就都不是问题,可有几个真正的专家,再者说,射频需要经验的积累,只有实际项目做的越多越广,经验也就积累得越多,并不是一朝一夕能达到的。
前不久突然觉得,我的射频模块控制要是也是自己来编程控制多好啊!那就得学单片机编程,学习上位机编程了,可我都不会啊,要不就先来整整上位机,windows方面的。
大学唯一学的编程语言是C语言,可根本不能理解用C做什么,还都还给老师10多年了,这怎么办?从BASIC,C/C++,JAVA,PASCAL搜索了一圈,还是选C++吧,毕竟是C 语言发展而来,用VC环境,身边有可以请教的人。
在网上转悠了很久,发现《windows程序设计》是必看,《MFC Windows程序设计》是学MFC最经典的书籍。
OMG,这些书啊,都是上千页的,白天都在上班,哪有时间看哦,只有先下载下来作为参考资料了。
扯远了啊。
这也不行那也不行,怎么办呢?干脆硬着头皮上吧,找几个实例照搬,再修修改改加深理解吧。
第一个目标,自己编写个串口调试助手,掌握串口通信编程,这样上位机的编写就有希望了。
好了,目标定下来了。
网上下载了个串口调试助手,确定基本功能:1.自动寻找串口,并自动添加到下拉框中共选择;2.有波特率、数据位、停止位、校验位的选择设置;3.串口打开控制按钮;4.发送、清除按钮;5.接收是自动实现的;6.有定时自动发送功能;7.有传送文件功能;8.有状态栏显示,指示串口状态,设置参数和发送接收显示。
下面就一步步实现,本人纯业余,只是记录下来这个学习过程,请勿拍砖。
开发平台Visual C++6.0英文版,电脑是i7-2670Q四核8G内存1G独显的笔记本,装的win764位旗舰版,因此VC6兼容不是太好,有些小毛病,不过不影响编写。
串口调试助手源码
串口调试助手程序框架(一)【截图】【源码】main.cpp#include<windows.h>#include<commctrl.h>#include<iostream>#include<stdio.h>#include"resource.h"using namespace std;HINSTANCE hInst;//窗口实例HWND hRDCLEAR, hSDCLEAR;HMENU hRDCHAR;void Init(HWND hwndDlg){SetWindowText(GetDlgItem(hwndDlg, SETXONC), "0x13");SetWindowText(GetDlgItem(hwndDlg, SETXOFFC), "0x19");SetWindowText(GetDlgItem(hwndDlg, SETTO), "0");SendDlgItemMessage(hwndDlg, RDCHAR, BM_SETCHECK, 1, 0);//把RadioBox RDCHAR设置为选中SendDlgItemMessage(hwndDlg, SDCHAR, BM_SETCHECK, 1, 0);//默认发送字符SendDlgItemMessage(hwndDlg, CTASY, BM_SETCHECK, 1, 0);//默认异步通信//在一个groupbox中必须有一个radiobox的属性:组为真//串口号SetWindowText(GetDlgItem(hwndDlg, SETCOMNO), "COM4");//默认值SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM1"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM2"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM3"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM4"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM5"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM6"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM7"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM8"));SendDlgItemMessage(hwndDlg, SETCOMNO, CB_ADDSTRING, 0, (LPARAM)("COM9"));//波特率SetWindowText(GetDlgItem(hwndDlg, SETBR), "9600");//默认值SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("1200"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("2400"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("4800"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("9600"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("14400"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("28800"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("38400"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("57600"));SendDlgItemMessage(hwndDlg, SETBR, CB_ADDSTRING, 0, (LPARAM)("115200"));//数据位SetWindowText(GetDlgItem(hwndDlg, SETBS), "8");//默认值SendDlgItemMessage(hwndDlg, SETBS, CB_ADDSTRING, 0, (LPARAM)("4"));SendDlgItemMessage(hwndDlg, SETBS, CB_ADDSTRING, 0, (LPARAM)("5"));SendDlgItemMessage(hwndDlg, SETBS, CB_ADDSTRING, 0, (LPARAM)("6"));SendDlgItemMessage(hwndDlg, SETBS, CB_ADDSTRING, 0, (LPARAM)("7"));SendDlgItemMessage(hwndDlg, SETBS, CB_ADDSTRING, 0, (LPARAM)("8"));//校验位SetWindowText(GetDlgItem(hwndDlg, SETPAR), "N");//默认值SendDlgItemMessage(hwndDlg, SETPAR, CB_ADDSTRING, 0, (LPARAM)("N"));SendDlgItemMessage(hwndDlg, SETPAR, CB_ADDSTRING, 0, (LPARAM)("odd"));SendDlgItemMessage(hwndDlg, SETPAR, CB_ADDSTRING, 0, (LPARAM)("even"));//停止位SetWindowText(GetDlgItem(hwndDlg, SETSB), "1");//默认值SendDlgItemMessage(hwndDlg, SETSB, CB_ADDSTRING, 0, (LPARAM)("1"));SendDlgItemMessage(hwndDlg, SETSB, CB_ADDSTRING, 0, (LPARAM)("2"));}BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {//窗口回调函数switch (uMsg)//消息处理{case WM_INITDIALOG://初始化{Init(hwndDlg);}return TRUE;case WM_CLOSE://关闭窗口{EndDialog(hwndDlg, 0);}return TRUE;case WM_COMMAND://菜单消息{switch (LOWORD(wParam)){case RDCLEAR://清空接收框数据{SetWindowText(hRDCLEAR, "");}return TRUE;case SDCLEAR://清空发送框数据{SetWindowText(GetDlgItem(hwndDlg, SDTEXT), "");}return TRUE;case SDSEND://发送数据{TCHAR szBuf[80];//缓存区string temp;GetWindowText(GetDlgItem(hwndDlg, SDTEXT), szBuf, 80);//获得接收框的文本temp = szBuf;//拼接字符串//cout<<temp;SetWindowText(GetDlgItem(hwndDlg, RDTEXT), temp.c_str());}return TRUE;case SETOP:{//打开串口}return TRUE;case SETCP:{//关闭串口}return TRUE;case SETSO:{}return TRUE;}}return TRUE;}return FALSE;}int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {hInst = hInstance;InitCommonControls();return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);}【源码】resource.h#ifndef IDC_STATIC#define IDC_STATIC (-1)#endif#define DLG_MAIN 100#define RDBOX 40000#define RDTEXT 40001#define RDCHAR 40002#define RDHEX 40003#define RDCLEAR 40004#define SDCHAR 40005#define SDHEX 40006#define SDCLEAR 40007#define SDSEND 40008#define SDAS 40009#define SDASTIME 40010#define SETCOMNO 40011#define CTSYN 40012#define CTASY 40013#define SETBR 40014#define SDTEXT 40015#define SETXONC 40016#define SETXOFFC 40017#define SETTO 40018#define SETPAR 40019#define SETSB 40020#define SETBS 40021#define SETOP 40022#define SETCP 40023#define SETSO 40024【源码】resource.rc#include<windows.h>#include<commctrl.h>#include<richedit.h>#include"resource.h"//// Dialog resources//LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRALDLG_MAIN DIALOG 0, 0, 635, 304STYLE DS_3DLOOK| DS_CENTER| DS_MODALFRAME| DS_SHELLFONT| WS_CAPTION| WS_VISIBLE| WS_POPUP| WS_SYSMENU CAPTION "SerialPortDebug"FONT 8, "Ms Shell Dlg"{GROUPBOX "Setting", 0, 257, 7, 365, 290, 0, WS_EX_LEFTLTEXT "Select Port", 0, 284, 30, 36, 8, SS_LEFT, WS_EX_LEFTGROUPBOX "Communication Type", 0, 278, 62, 295, 35, 0, WS_EX_LEFTLTEXT "ByteSize", 0, 469, 120, 28, 8, SS_LEFT, WS_EX_LEFTLTEXT "BaudRate", 0, 297, 122, 33, 8, SS_LEFT, WS_EX_LEFTGROUPBOX "Send Data", 0, 12, 135, 240, 162, 0, WS_EX_LEFTLTEXT "StopBits", 0, 471, 161, 27, 8, SS_LEFT, WS_EX_LEFTLTEXT "Parity", 0, 299, 162, 18, 8, SS_LEFT, WS_EX_LEFTAUTOCHECKBOX "fParity", 0, 401, 162, 35, 8, 0, WS_EX_LEFTAUTOCHECKBOX "CTS", 0, 296, 202, 30, 8, 0, WS_EX_LEFTAUTOCHECKBOX "DSR", 0, 365, 202, 31, 8, 0, WS_EX_LEFTAUTOCHECKBOX "DTR", 0, 436, 202, 31, 8, 0, WS_EX_LEFTAUTOCHECKBOX "XON/XOFF", 0, 507, 202, 53, 8, 0, WS_EX_LEFTLTEXT "ms", 0, 131, 228, 10, 8, SS_LEFT, WS_EX_LEFTLTEXT "ms", 0, 562, 237, 10, 8, SS_LEFT, WS_EX_LEFTLTEXT "XonChar", 0, 293, 238, 28, 8, SS_LEFT, WS_EX_LEFTLTEXT "XoffChar", 0, 383, 238, 28, 8, SS_LEFT, WS_EX_LEFTLTEXT "Timeouts", 0, 478, 238, 30, 8, SS_LEFT, WS_EX_LEFTGROUPBOX "Receive Data", RDBOX, 15, 10, 235, 120, 0, WS_EX_LEFTEDITTEXT RDTEXT, 20, 25, 225, 75, ES_AUTOHSCROLL | ES_MULTILINE | ES_READONLY, WS_EX_LEFTAUTORADIOBUTTON "Char", RDCHAR, 26, 112, 31, 8, WS_GROUP, WS_EX_LEFTAUTORADIOBUTTON "Hex", RDHEX, 80, 112, 29, 8, 0, WS_EX_LEFTPUSHBUTTON "Clear", RDCLEAR, 196, 108, 45, 14, 0, WS_EX_LEFTAUTORADIOBUTTON "Char", SDCHAR, 26, 208, 31, 8, WS_GROUP, WS_EX_LEFTAUTORADIOBUTTON "Hex", SDHEX, 82, 208, 29, 8, 0, WS_EX_LEFTPUSHBUTTON "Clear", SDCLEAR, 132, 204, 45, 14, 0, WS_EX_LEFTPUSHBUTTON "Send", SDSEND, 196, 204, 45, 14, 0, WS_EX_LEFTAUTOCHECKBOX "Auto send", SDAS, 26, 228, 48, 8, 0, WS_EX_LEFTEDITTEXT SDASTIME, 86, 225, 40, 14, ES_RIGHT | ES_AUTOHSCROLL, WS_EX_LEFTCOMBOBOX SETCOMNO, 337, 27, 95, 30, CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_LEFTAUTORADIOBUTTON "Synchronous ", CTSYN, 326, 75, 59, 8, WS_GROUP, WS_EX_LEFTAUTORADIOBUTTON "Asynchronous ", CTASY, 472, 77, 63, 8, 0, WS_EX_LEFTCOMBOBOX SETBR, 337, 120, 48, 30, CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_LEFTEDITTEXT SDTEXT, 20, 147, 227, 45, ES_AUTOHSCROLL, WS_EX_LEFTEDITTEXT SETXONC, 325, 235, 40, 14, ES_AUTOHSCROLL, WS_EX_LEFTEDITTEXT SETXOFFC, 417, 235, 40, 14, ES_AUTOHSCROLL, WS_EX_LEFTEDITTEXT SETTO, 512, 235, 40, 14, ES_AUTOHSCROLL, WS_EX_RIGHTCOMBOBOX SETPAR, 338, 160, 48, 30, CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_LEFTCOMBOBOX SETSB, 512, 159, 48, 30, CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_LEFTCOMBOBOX SETBS, 512, 117, 48, 30, CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_LEFTPUSHBUTTON "Open Port", SETOP, 297, 272, 45, 14, 0, WS_EX_LEFTPUSHBUTTON "Close Port", SETCP, 387, 272, 45, 14, 0, WS_EX_LEFTDEFPUSHBUTTON "Stop I/O", SETSO, 473, 271, 45, 14, 0, WS_EX_LEFT}//// Manifest resources//LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL1 RT_MANIFEST".\\manifest.xml"【源码】manifest.xml< ? xml version = "1.0" encoding = "UTF-8" standalone = "yes" ? ><assembly xmlns = "urn:schemas-microsoft-com:asm.v1" manifestVersion = "1.0"><dependency><dependentAssembly><assemblyIdentity type = "win32" name = "mon-Controls" version = "6.0.0.0" processorArchitecture = "*" publicKeyToken = "6595b64144ccf1df" language = "*" / >< / dependentAssembly>< / dependency><trustInfo xmlns = "urn:schemas-microsoft-com:asm.v3"><security><requestedPrivileges><requestedExecutionLevel level = "asInvoker" uiAccess = "false" / > < / requestedPrivileges> < / security>< / trustInfo>< / assembly>。
经典串口调试助手源程序及串口通信设置
经典串口调试助手源程序及串口通信设置串口调试助手是一种用于串口通信调试的工具,它可以通过串口与外部设备进行数据的读写和处理,常用于单片机、嵌入式系统、电子设备等领域的开发调试中。
经典串口调试助手源程序经典串口调试助手是一款经典的串口调试工具,其源程序使用C语言编写,提供了丰富的功能和易于使用的界面,被广泛应用于各种领域的开发调试中。
该工具可以在Windows操作系统中运行,并支持多种串口通信协议,包括RS232、RS485、RS422等。
以下是经典串口调试助手部分源程序的示例:```c// 打开串口HANDLE OpenSerialPort(const char* portName){ HANDLE hCom; hCom = CreateFile(portName, // 串口名称GENERIC_READ | GENERIC_WRITE, // 读写访问0,// 无共享NULL,// 安全描述符OPEN_EXISTING,// 打开方式FILE_ATTRIBUTE_NORMAL, // 文件属性NULL); // 文件模板if (hCom ==INVALID_HANDLE_VALUE) // 判断是否打开成功{ return NULL; } return hCom;}// 发送数据int SendData(HANDLE hCom, const char* data, int len){ DWORD writeSize; if (!WriteFile(hCom, data, len, &writeSize, NULL)) // 写入数据{ return -1; } return writeSize;}// 接收数据int ReceiveData(HANDLE hCom, char* data, int len){ DWORD readSize; if (!ReadFile(hCom, data, len,&readSize, NULL)) // 读取数据{ return -1; } return readSize;}// 关闭串口int CloseSerialPort(HANDLE hCom){ if(!CloseHandle(hCom)) // 关闭句柄{ return-1; } return 0;}```上述函数实现了打开串口、发送数据、接收数据和关闭串口等基本功能,可以作为串口通信的底层驱动实现。
串口调试助手源代码
串口调试助手预源代码using System;using System.Collect ion s.Ge neric;using p onen tModel;using System.Data;using System.Draw ing;using System.Linq;using System.Text;using System.Thread in g.Tasks;using System.Wi ndows.Forms;using Syste m.IO;using Syste m.IO .Ports;using System.Thread ing;using DevExpress.XtraEditors;using System.Text.RegularExpressi ons;n amespace Win dowsFormsApplicatio n3 {public partial class Form1 : XtraForm {SerialPort sp1 = new SerialPort(); int Flag = 0;int StateCha nged = 0;int StateCha nged_Ascii = 1; int StateCha nged_16 = 0;string Temp = null;stri ng Temp_memoSe nd = n ull;private void memoSe nd_EditValueCha nged(object sen der, Even tArgs e) { - }public Form1(){In itializeComp onen t();sp1.DataReceived += sp1_DataReceived; }private void Form1_Load(object sen der, Even tArgs e) { -this.MaximizeBox = false;// 检查是否有串口stri ng[] str = SerialPort.GetPortNames();if (str == null) __________ |{MessageBox.Show(”本机没有串口!", "error");return;}// 添加串口项目foreach (stri ng port in Syste m.IO.P orts.SerialPort.GetPortNames()) { cbSerial.Properties .I tems.Add(port);}// 串口设置默认选择项cbSerial.Selectedl ndex = 0;Con trol.CheckForlllegalCrossThreadCalls = false; spl.DataReceived += new SerialDataReceivedEve ntHa ndler(sp1_DataReceived); //订阅委托e)//SerialPort sp1 = new SerialPort();if (!sp1.lsOpe n)〃串口是关闭的,设置参数,打开串口{try{// 获取串口号string serialName = cbSerial.Selectedltem.ToString(); sp1.PortName = serialName;// 设置各参数stri ng strBaudRate = cbBaudRate.Text;string strDateBits = cbDataBits.Text;string strStopBits = cbStop.Text;sp1.BaudRate = Con vert.ToI nt32(strBaudRate);〃波特率sp1.DataBits = Con vert.ToI nt32(strDateBi ts);// 数据位switch (cbStop.Text) // 停止位{case "1":sp1.StopBits = StopBits. On e;break;case "1.5":sp1.StopBits = StopBits. On e;break;case "2":sp1.StopBits = StopBits.Two;break; default:case " 奇校验":sp1.Parity = Parity.Odd; break;精品文档", "Error");case "偶校验":spl.Parity = Parity.Eve n; break; default:MessageBox.Show("Error: 参数不正确!break;labSerial.Text =" 串口号:” + sp1 .P ortName; labState.Text ="状 态:打开";}catch (System.Excepti on ex) {MessageBox.Show("Error:" + ex.Message, "Error"); return; } } }//private void CloseSerial_Click(object sen der, Even tArgs e) 〃{// if (sp1.lsOpe n) // { // sp1.Close(); // labSerial.Text =" 串口号:” + sp1.PortName; // labState.Text ="状 态:关闭";// } 〃}private void Sen d_Click(object sen der, Even tArgs e) { - stri ng StrSe nd = memoSe nd.Text; if (!sp1.lsOpe n) {MessageBox.Show (” 请先打开串口! ", "Error");return; } else {if (rbt nSen dStr.Checked)//〃bt nOpe n.En abled =false; 字符串发送{if (ckTimeSe nd.Checked&& !(stri ng.lsNullOrEmpty(textTime.Text)))//判断是否定时发送数据{string Time = textTime.Text;tmSe nd.l nterval = int. Parse(Time) * 1000;//时间单位是秒,化成毫秒单位后的空格//stri ng StrSe nd_2 = StrSe nd_1.Replace (” ", ""); //stri ng StrSe nd_3 = stri ng.J oi n (” ",Regex.Matches(StrSend_2, @"..|.").Cast<Match>().ToList());stri ng[] StrArray = StrSe nd_1.Split(' '); // 用空格符隔开字符串数组var Str_Se nd = new List<stri ng>();// i nt byteBufferLe ngth = StrArray.Le ngth; 数组StrArray 的长度for (int i = 0; i < StrArray.Le ngth; i++){if (StrArray[i].Trim()=="") {//byteBufferLe ngth--; con ti nue; }else} else //{if (ckTimeSe nd.Checked&& !(string.IsNullOrEmpty(textTime.Text))){卜六进制发送//tmSe nd.Start();//开启定时器{if (StrArray[i].Le ngth < 3){Str_Se nd.Add(StrArray[i]);}else{Str_Se nd.AddRa nge(Devide(StrArray[i]));}}}int byteBufferLe ngth = Str_Se nd.Cou nt; byte[] byteBuffer = new byte[byteBufferLe ngth];in t ii = 0;//decNum = Con vert.ToI nt32(Str_Se nd[i], 16); // 把字符串转成16进制数〃byteBuffer[ii] = Co nvert.ToByte(decNum);〃}try{decNum = Con vert.ToI nt32(Str_Se nd[i], 16); // 把字符串转成16进制数byteBuffer[ii] = Co nvert.ToByte(decNum);}catch (System.Exceptio n ex){MessageBox.Show(”输入错误!","提示");return;}ii++;}sp1.Write(byteBuffer, 0, byteBuffer.Length);// 从第0个开始写入byteBuffer ,长度bytebuffer 的长度。
Comassistant串口助手详解和源代码分析
到目前为止还不能在接收编辑框中看到数据,因为我们还没有打开串口,但运行程序不应该有任何错误, 不然,你肯定哪儿没看仔细,因为我是打开 VC6 对照着做一步写一行的,运行试试。没错吧?那么做下一 步: 6.打开串口和设置串口参数 码: // TODO: Add extra initialization here if(m_ctrlComm.GetPortOpen()) m_ctrlComm.SetPortOpen(FALSE); m_ctrlComm.SetCommPort(1); //选择 com1 if( !m_ctrlComm.GetPortOpen()) m_ctrlComm.SetPortOpen(TRUE);//打开串口 else AfxMessageBox("cannot open serial port"); m_ctrlComm.SetSettings("9600,n,8,1"); //波特率 9600,无校验,8 个数据位,1 个停止位 m_ctrlComm.SetInputModel(1); //1:表示以二进制方式检取数据 m_ctrlComm.SetRThreshold(1); //参数 1 表示每当串口接收缓冲区中有多于或等于 1 个字符时将引发一个接收数据的 OnComm 事件 m_ctrlComm.SetInputLen(0); //设置当前接收区数据长度为 0 m_ctrlComm.GetInput();//先预读缓冲区以清除残留数据 现在你可以试试程序了,将串口线接好后(不会接?去看看我写的串口接线基本方法),打开串口调试助 手,并将串口设在 com2,选上自动发送,也可以等会手动发送。再执行你编写的程序,接收框里应该有数 据显示了。 7.发送数据 先为发送按钮添加一个单击消息即 BN_CLICKED 处理函数,打开 ClassWizard->Message 你可以在你需要的时候打开串口, 例如在程序中做一个开始按钮, 在该按钮
经典的串口调试工具源代码(二)
经典的串口调试工具源代码(二)Private Sub cmdswitch_Click()On Error GoTo ErrIf MSComm.PortOpen = True ThenComSwitch = TrueElseComSwitch = FalseEnd IfIf ComSwitch = False ThenStatusBar1.Panels(1).Text = "Connected"mnuconnect.Caption = "Dis&connect"OpenCom ' 打开串口ComSwitch = TrueElseCloseCom ' 关闭串口ComSwitch = FalseStatusBar1.Panels(1).Text = "Disconnected"mnuconnect.Caption = "&Connect"StatusBar1.Panels(2).Text = "COM" & mPort StatusBar1.Panels(3).Text = MSComm.SettingsIf (OutputAscii) ThenStatusBar1.Panels(4) = "ASCII"ElseStatusBar1.Panels(4) = "HEX"End IfEnd IfErr:End SubPrivate Sub Form_Load()On Error GoTo ErrlblWEB.FontUnderline = True ' WEB上加下划线lblWEB.ForeColor = vbBlue ' 蓝色显示WEBtxtsend.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 "8"cbodatabit.AddItem "7"cbodatabit.AddItem "6"' 停止位载入cbostopbit.AddItem "1"cbostopbit.AddItem "1.5"cbostopbit.AddItem "2"Err:End SubPrivate Sub hexReceive()On Error GoTo ErrDim ReceiveArr() As Byte ' 接收数据数组Dim receiveData As String ' 数据暂存Dim Counter As Integer ' 接收数据个数计数器Dim i As Integer ' 循环变量If (MSComm.InBufferCount > 0) ThenCounter = MSComm.InBufferCount ' 读取接收数据个数receiveData = "" ' 清缓冲ReceiveArr = MSComm.Input ' 数据放入数组For i = 0 To (Counter - 1) Step 1 ' 数据格式处理If (ReceiveArr(i) < 16) ThenreceiveData = receiveData & "0" + Hex(ReceiveArr(i)) & Space(1) ' 小于16,前面加0ElsereceiveData = receiveData & Hex(ReceiveArr(i)) & Space(1) ' 加空格显示End IfNext iTxtReceive.Text = TxtReceive.Text + receiveData ' 显示接收的十六进制数据TxtReceive.SelStart = Len(TxtReceive.Text) ' 显示光标位置End IfReceiveCount = ReceiveCount + Counter ' 接收计数txtRXcount.Text = "RX:" & ReceiveCount ' 接收字节数显示If chkautoclear.Value = 1 Then ' 自动清空判断If ReceiveCount >= 65535 ThenTxtReceive.Text = ""End IfEnd IfErr:End SubPrivate Sub hexSend()On Error Resume NextDim 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 outputLenTemporarySave = Mid(outData, i + 1, 1) ' 取一位数据If (Asc(TemporarySave) >= 48 And Asc(TemporarySave) <= 57) Or (Asc(TemporarySave) >= 65And Asc(TemporarySave) <= 70) ThendataCount = dataCount + 1ElseExit ForExit SubEnd IfNextIf dataCount Mod 2 <> 0 Then ' 判断十六进制数据是否为双数dataCount = dataCount - 1 ' 不是双数,则减1End IfoutData = Left(outData, dataCount) ' 取出有效的十六进制数据ReDim SendArr(dataCount / 2 - 1) ' 重新定义数组长度For i = 0 To dataCount / 2 - 1SendArr(i) = Val("&H" + Mid(outData, i * 2 + 1, 2)) ' 取出数据转换成十六进制并放入数组中NextSendCount = SendCount + (dataCount / 2) ' 计算总发送数txtTXcount.Text = "TX:" & SendCountMSComm.Output = SendArr ' 发送数据End SubPrivate Sub OpenCom() '打开串口On Error GoTo ErrIf 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 Thentxtstatus.Text = "STATUS:" & cbocom.Text & " OPEND," & cbobaudrate.Text & "," & Left(cboparitybit.Text, 1) & "," & cbodatabit.Text & "," & cbostopbit.Textcmdswitch.Caption = "关闭串口"mnuconnect.Caption = "disconnect"'ImgSwitch.Picture = LoadPicture("f:\我的VB\串口调试软件\图片\kai.jpg") ' 显示串口已经打开的图标ImgSwitchon.Visible = TrueImgSwitchoff.Visible = FalseElsetxtstatus.Text = "STATUS:COM Port Cloced" ' 串口状态显示cmdswitch.Caption = "打开串口"mnuconnect.Caption = "connect"'ImgSwitch.Picture = LoadPicture("f:\我的VB\串口调试软件\图片\guan.jpg") ' 显示串口已经关闭的图标ImgSwitchoff.Visible = TrueImgSwitchon.Visible = FalseEnd IfErr:End SubPrivate Sub textReceive()On Error GoTo ErrInputSignal = MSComm.InputReceiveCount = ReceiveCount + LenB(StrConv(InputSignal, vbFromUnicode)) ' 计算总接收数据If DisplaySwitch = False Then ' 显示接收文本TxtReceive.Text = TxtReceive.Text & InputSignal ' 单片机内存的值用TextReceive显示出TxtReceive.SelStart = Len(TxtReceive.Text) ' 显示光标位置End IftxtRXcount.Text = "RX:" & ReceiveCount ' 接收字节数显示If chkautoclear.Value = 1 Then ' 自动清空判断If ReceiveCount >= 65535 ThenTxtReceive.Text = ""End IfEnd IfErr:End SubPrivate Sub textSend()On Error GoTo ErrIf ModeSend = True ThenOutputSignal = FileData ' 发送文件ElseOutputSignal = txtsend.Text ' 发送文本End IfSendCount = SendCount + LenB(StrConv(OutputSignal, vbFromUnicode)) ' 计算总发送数txtTXcount.Text = "TX:" & SendCount ' 发送字节数显示Err:End SubPrivate Sub Image1_Click()End SubPrivate Sub mnuautosend_Click()On Error GoTo Err'If TmrAutoSend.Enabled = True Then ' 如果有效则,自动发送If MSComm.PortOpen = True Then ' 串口状态判断ChkAutoSend.Value = 1TmrAutoSend.Interval = Val(TxtAutoSendTime) ' 设置自动发送时间mnuautosend.Caption = "取消自动发送"TmrAutoSend.Enabled = True ' 打开自动发送定时器Elsemnuautosend.Caption = "自动发送"ChkAutoSend.Value = 0 ' 串口没有打开去掉自动发送MsgBox "串口没有打开,请打开串口", 48, "串口调试助手" ' 如果串口没有被打开,提示打开串口End If'ElseIf TmrAutoSend.Enabled = False Then ' 如果无效,不发送' mnuautosend.Caption = "autosend"' TmrAutoSend.Enabled = False ' 关闭自动发送定时器'End IfErr:End SubPrivate Sub mnucom_Click(Index As Integer)Dim i As IntegerDim OldPort As LongOn Error Resume NextWith MSCommOldPort = .CommPortIf MSComm.PortOpen Then.PortOpen = False.CommPort = Index.PortOpen = TrueIf Err.Number <> 0 Then ' This should not happen...MsgBox "Com" & Index & " is not available." & _vbCrLf & Err.DescriptionErr.Clear.CommPort = OldPortElseFor i = 1 To 4mnucom(i).Checked = FalseNext imnucom(Index).Checked = TrueEnd IfElse.CommPort = IndexFor i = 1 To 4mnucom(i).Checked = FalseNext imnucom(Index).Checked = TrueEnd IfEnd WithUpdateStatusEnd SubPrivate Sub mnuconnect_Click()On Error Resume NextIf MSComm.PortOpen = True ThenComSwitch = TrueElseComSwitch = FalseEnd IfWith MSCommIf .PortOpen = True Then.PortOpen = Falsetxtstatus.Text = "STATUS:COM Port Cloced" ' 串口状态显示cmdswitch.Caption = "打开串口"'ImgSwitch.Picture = LoadPicture("f:\我的VB\串口调试软件\图片\guan.jpg") ' 显示串口已经关闭的图标ImgSwitchoff.Visible = TrueImgSwitchon.Visible = FalseElse.PortOpen = TrueComSwitch = Truetxtstatus.Text = "STATUS:" & cbocom.Text & " OPEND," & cbobaudrate.Text & "," & Left(cboparitybit.Text, 1) & "," & cbodatabit.Text & "," & cbostopbit.Textcmdswitch.Caption = "关闭串口"'ImgSwitch.Picture = LoadPicture("f:\我的VB\串口调试软件\图片\kai.jpg") ' 显示串口已经打开的图标ImgSwitchon.Visible = TrueImgSwitchoff.Visible = FalseIf Err.Number <> 0 ThenMsgBox "Com" & .CommPort & " is not available." & vbCrLf & _Err.DescriptionErr.ClearEnd IfEnd IfEnd WithUpdateStatusEnd SubPrivate Sub mnusave_Click()On Error GoTo Err ' 错误处理SaveTextPath = txtsavepath ' 路径暂存Open txtsavepath & "\1.txt" For Output As #1 ' 打开文件' 不存在的话会创建文件,如已存在会覆盖' output 改为append 为追加' 改为input 则只读Print #1, Year(Date) & "年" & Month(Date) & "月" & Day(Date) & _"日" & Hour(Time) & "时" & Minute(Time) & "分" & Second(Time) & _"秒" & vbCrLf & TxtReceive.Text + vbCrLf ' 把接收区的文本保存文本前加上保存时间(0000年00月00日00时00分00秒)' vbcrlf 为回车换行Close #1 ' 关闭文件txtsavepath = "OK,1.txt Save" ' 提示保存成功cmdsavedisp.Enabled = FalseSavetime = Timer ' 记下开始的时间While Timer < Savetime + 5 ' 循环等待5 - 要延时的时间DoEvents ' 转让控制权,以便让操作系统处理其它的事件。
3550调试编程代码
一、启动初始化:按蓝色按钮大于五分钟(在内存卡上方)输入中国代码:*95 31994 √,31994 √31994 √31994 √。
29-5/*26 √自动停电3分钟进入编程:按*95 31994 √,31994 √系统进入编程,挂机退出改时间:1913 日期:1914二、编程代码1、改出局码:234 系统有16个路由就有16个出局码(初始拨0出局)例:改为拨9出局输入23显示23Trunk Acces Codes,输入4显示Routel. Posl:0,按*显示Routel Posl;输入新的出局码9,显示Routel.Posl:9=comfirm? 按√确认,嘟一声2、改呼叫总台代码:2352 (初始9到总台)例:改为拨0到总台输入23显示23 Trunk Access Codes;输入5显示235 Attendant code;输入2显示Call No:9=change按*显示Call No: ,输入总机代码0显示Call No:0= comfirm? 按√确认,嘟一声3、改分机号:161012输入16显示16 Call Managementdnc,输入10显示1610 internal number1=分机2=组(寻线组16102):输入1显示16101 Station 1板子,2=修改号码输入2显示pos:100+(下一个)*(修改)#分机按+翻动分机到110显示posll:110,按*显示posll: ,输入新的分机号888显示posll:888=comfirm? 按√确认,嘟一声4、改寻线组的组号:16102输入16102显示Groupl:350,按*显示Groupl: ,输入新的组号887显示:Groupl:887=comfirm? 按√确认,嘟一声5、寻线组的振铃模式:16152;寻线组成员分机的输入:16151输入1615Group setup (1=组成员,2=组振铃模式),输入2显示Group:350:Ringing按*Guoup:350(1=循环振铃,2=线性振铃,3=同时振铃),输入2显示Group:350:Linear Hant=comfirm? 按√确认,嘟一声6、遇忙转移:1416(初始所有分机关闭)输入1416显示stn100:off ;按#显示stn: 输入分机号如:108,显示stn:108按√确认,嘟一声显示stn108:off;按*显示stn108:0= off/1=on输入1显示stn108: on=comfirm? 按√确认,嘟一声7、语音信箱Vm接口的设定:14112(初始所有分机端口默认为Auto-select)输入1411显示Stn100:Auto-select按#显示stn:输入分机号如:108,显示stn:108=comfirm? 按√显stn:108Auto-select 按*修改,显示stn:108 输入编程代码2显示stn:108:Phone Mail5=comfirm? 按√,108端口类型改为vm口8、遇忙强插:1413 Denied/不允许Allowed/允许*62(初始所有分机不允许)输入1413显示stn100: Deniedr按*显示stn100:(0=Denied 1=Allowed)按1显stn100: Allowed=comfirm? 按√确认,嘟一声9、代接组的设定:1418(初始所有分机不属于任何代接组)输入1418显示stn100: —,按*显示stn100: ,输入代接组号:1(可分1-16组,例分机100属1组)显示stn100: 1 =comfirm?按√确认显示stn100: 1(按+翻动目标分机)10、数字话机的DTMF音频拨号(初始不能自动激活音频拨号)输入2220显示Mode:No按*显示Mode:(0=No,1=Yes)输入1显示Mode: Yes=comfirm? 按√确认,嘟一声11、长途限制等级:日/夜151/152(初始全部COS-等级14无限制日/夜)以108分机为样板机为例:输入151显示:stn100:Retel:full,按#显示stn100: 输入108显示stn100:108按√确认显示stn108: ax-ex-108(1=复制,2=制样板分机)输入2显示stn108:Retel:full,按*显示stn108、Retel,输入服务等级8(禁止表1)显stn108、Retel:Barl = comfirm? 按√确认显stn108:Retel:full按+ stn108、R ete2:Barl 复制分机:普通分机以109为例输入151按#输入普通分机号109,显示stn100:109 =comfirm?按√确认显stn109:as extn109 按*After for ext 109(1=复制,2=制样板分机)输入显Template for 109: 输入样板分机108显Template for 109:108 =comfirm? 按√确认12、禁止表154(初始没有存储)输入1541按*修改显示posl: ,输入长途号码例:00显示posl:00=comfirm? 按√确认,嘟一声13、允许表153输入1531*显示posl: ,输入允许号码如:010显示posl:010=comfirm? 按√确认,嘟一声14、路由分配1712(初始所有外线都属于外线组1,外线组可分1-16组)例:槽5插了一个外线板,要求该板第二条外线为第二路由输入1712显示Slt/Tr501:1,按+选择外线槽位显示Slt/T502:1按*显示Slt/T502:输入外线组号2显示Slt/Tr502:2 =comfirm? 按√确认,嘟一声15、代接提示音关闭22172*0,按√确认,嘟一声16、电话重新激活(即提机有电流声,但没有拨号音)1.14292.按*选择拨号方式,按#选择分机;例:200分机有此故障,则输入1429#200*2√*1√17、电脑话务员1.交换机部分(初始为350是指分机组号)1616/1617 显示sh/tr0x0x:100 指定外线寻线到哪个分机或端口(每条外线都要改)16151 显示grp350:mbrl: 寻线组成员的输入16152 显示grp350:ring 寻线组振铃方式16102 寻线组号修改,以上均为:+/下一个;*/修改;#/选择分机2.话务员部分(外线打入话务员组号,输入#*+password)#693 软复位#101您好;#105占线;#106无人接#341 转接方式及能力#107空号;#114转接;#115已下班#500+前台分机号+## 前台设置听录音#2XX#63X+# 分机字头设置#63X+功能代码#67XY# 分机位数设置#691 存储参数18、计费软件设置216关反极性计费*0 +(下一个)每一个都关,显示slt/tr0x0x:on22131 传输速度选择*0、2、3,显示jv.24-porl:96002213211 打开计费输出* ,显示port:none(设v.24)2213212 v.24输出口设定*1 ,显示v.24-port:1放光盘,我的电脑,setup完成。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
sp1.Close();
//SerialPort sp1 = new SerialPort();
if (!sp1.lsOpe n)〃串口是关闭的,设置参数,打开串口
{
try
{
//获取串口号
string serialName=cbSerial.SelectedItem.ToString();
sp1.PortName = serialName;
{
case "1":
sp1.StopBits =
break;
case "1.5":
sp1.StopBits =
break;
case "2":
sp1.StopBits =
break;
default:
MessageBox.Show("Error:
break;
}
switch (cbParity.Text)
}
public Form1()
{
In itializeComp onen t();
sp1.DataReceived += sp1_DataReceived; }
private void Form1_Load(object sen der, Even tArgs e) {-
this.MaximizeBox = false;
{
case "
sp1.Parity =
break;
case "
sp1.Parity = break;
case":
波特率
数据位
停止位
", "Error");
校验位
//private void CloseSerial_Click(object sen der, Even tArgs e)
using System.Text.RegularExpressi ons;
amespace Win dowsFormsApplicati on3
public partial class Form1:XtraForm {
SerialPort sp1 = new SerialPort(); int Flag = 0;
串口调试助手预源代码
using System;
using System.Collect ion s.Ge neric; using p onen tModel;
using System.Data;
using System.Draw ing;
using System.Linq;
using System.Text;
using System.Thread in g.Tasks; using System.Wi ndows.Forms;
using Syste m.IO;
using Syste m.IO .Ports;
using System.Thread ing;
using DevExpress.XtraEditors;
成毫秒单位
tmSe nd.Start();//开启定时器
}
else
{
sp1.Write(StrSe nd);//写入数据
SerialDataReceivedEve ntHa ndler(sp1 DataReceived); //
private void comboBoxEdit1 Selectedl ndexCha nged(object sen der, Even tArgs e)
private void Open Serial_Click(object sen der. Even tArgs e)
//检查是否有串口
stri ng[] str = SerialPort.GetPortNames();
if (str == n ull)
{
MessageBox.Show(”本机没有串口!", "error");
return;
}
//添加串口项目
foreach (stri ng port in Syste m.IO.P orts.SerialPort.GetPortNames()) {
int StateCha nged = 0;
int StateCha nged_Ascii = 1; int StateCha nged_16 = 0;
string Temp = null;
stri ng Temp memoSe nd = n ull;
private void memoSe nd_EditValueCha nged(object sen der, Even tArgs e) {-
stri ng StrSe nd = memoSe nd.Text;
if (!sp1.lsOpe n)
{
if (ckTimeSe nd.Checked
&& !(stri ng.lsNullOrEmpty(textTime.Text)))//
{
string Time = textTime.Text;
tmSe nd.l nterval = int.P arse(Time) * 1000;//时间单位是秒,化
//设置各参数
stri ng strBaudRate = cbBaudRate.Text; string strDateBits = cbDataBits.Text;
string strStopBits = cbStop.Text;
sp1.BaudRate=Con vert.ToI nt32(strBaudRate);〃sp1.DataBits = Con vert.ToI nt32(strDateBi ts);〃switch (cbStop.Text)//
cbSerial.Properties .I tems.Add(port);
}
//串口设置默认选择项
cbSerial.Selectedl ndex = 0;
Con trol.CheckForlllegalCrossThreadCalls = false;
spl.DataReceived += new
〃{
// if (sp1.lsOpe n)
// {
//sp1.Close();
//labSerial.Text ="串口号:” +sp1.PortName;
//labState.Text ="状 态:关闭";
//}
〃}
private void Sen d_Click(object sen der, Even tArgs e) {