基于.Net C# 通信开发-串口调试助手
<p><span style="font-size: 16px"> 基于.Net C# 通信开发-串口调试助手</span></p><p> </p>
<p><span style="font-size: 16px"> 1、</span><span style="font-size: 16px">概述</span></p>
<p><span style="font-size: 16px"> <img src="https://img2022.cnblogs.com/blog/28607/202205/28607-20220503191745467-334005604.png" alt=""></span></p>
<p><span style="font-size: 16px"> 串口调试助手,广泛应用于工控领域的数据监控、数据采集、数据分析等工作,可以帮助串口应用设计、开发、测试人员检查所开发的串口应用软硬件的数据收发状况,提高开发的速度,成为您的串口应用的开发助手。</span><br><span style="font-size: 16px">实全串口调试助手是绿色软件,只有一个执行文件,适用于各版本Windows操作系统,基于C# .Net 4.0 框架开发。可以在一台PC上同时启动多个串口调试助手(使用不同的COM口)。</span><br><span style="font-size: 16px"> 典型应用场合:通过串口调试助手与自行开发的串口程序或者串口设备进行通信联调。</span><br><span style="font-size: 16px"> 支持多串口,自动监测枚举本地可用串口;自由设置串口号、波特率、校验位、数据位和停止位等(支持自定义非标准波特率);</span><br><span style="font-size: 16px"> 支持ASCII/Hex两种模式的数据收发,发送和接收的数据可以UTF-8、16进制和AscII码之间任意转换;</span><br><span style="font-size: 16px"> 支持间隔发送,循环发送,批处理发送,输入数据可以从外部文件导入。</span></p>
<p><span style="font-size: 16px"> 串口调试开发,一般分为读取电脑连接串口信息、选择串口信息进行连接、设置相关发送接收配置、发送命令或消息,接收读取返回结果。</span></p>
<p> </p>
<p><span style="font-size: 16px"> 2、串口开发主要代码</span></p>
<p><span style="font-size: 16px"> 2.1、读取电脑连接串口信息</span></p>
<p><span style="font-size: 16px"> 串口调试,首先需要程序读取电脑连接串口信息。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取连接串口</span>
<span style="color: rgba(0, 0, 255, 1)">string</span>[] mPortNames =<span style="color: rgba(0, 0, 0, 1)"> SerialPort.GetPortNames();
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtPortName.Items.Clear();
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> mPortNames)
{
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtPortName.Items.Add(item);
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtParity.Items.Clear();
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">string</span> item <span style="color: rgba(0, 0, 255, 1)">in</span> Enum.GetNames(<span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(Parity)))
{
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtParity.Items.Add(item);
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.txtParity.DropDownStyle =<span style="color: rgba(0, 0, 0, 1)"> ComboBoxStyle.DropDownList;
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtStopBits.Items.Clear();
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">string</span> item <span style="color: rgba(0, 0, 255, 1)">in</span> Enum.GetNames(<span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(StopBits)))
{
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtStopBits.Items.Add(item);
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.txtStopBits.DropDownStyle =<span style="color: rgba(0, 0, 0, 1)"> ComboBoxStyle.DropDownList;
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtHandshake.Items.Clear();
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">string</span> item <span style="color: rgba(0, 0, 255, 1)">in</span> Enum.GetNames(<span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(Handshake)))
{
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtHandshake.Items.Add(item);
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.txtHandshake.DropDownStyle = ComboBoxStyle.DropDownList;</pre>
</div>
<p> <span style="font-size: 16px">2.2、选择串口信息进行连接</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 获取串口信息
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="setting"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="fail"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><returns></returns></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> SerialPort GetSerialPort(SerialPortSet setting, <span style="color: rgba(0, 0, 255, 1)">out</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> fail)
{
fail </span>= <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
{
_SerialPort </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SerialPort();
_SerialPort.PortName </span>=<span style="color: rgba(0, 0, 0, 1)"> setting.PortName;
_SerialPort.BaudRate </span>=<span style="color: rgba(0, 0, 0, 1)"> setting.BaudRate.ToInt32();
_SerialPort.Parity </span>= (Parity)Enum.Parse(<span style="color: rgba(0, 0, 255, 1)">typeof</span>(Parity), setting.Parity, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
_SerialPort.DataBits </span>=<span style="color: rgba(0, 0, 0, 1)"> setting.DataBits.ToInt32();
_SerialPort.StopBits </span>= (StopBits)Enum.Parse(<span style="color: rgba(0, 0, 255, 1)">typeof</span>(StopBits), setting.StopBits, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
_SerialPort.Handshake </span>= (Handshake)Enum.Parse(<span style="color: rgba(0, 0, 255, 1)">typeof</span>(Handshake), setting.Handshake, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> _SerialPort;
}
</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ex)
{
fail </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">打开串口 异常:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> ex.Message;
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
}</span></pre>
</div>
<p><span style="font-size: 16px"> 2.3、选择相关发送接收配置</span></p>
<p><span style="font-size: 16px"> 支持UTF-8、ASCII、GB2312、16进制内容,收、发选择;支持间隔发送,循环发送,批处理发送,输入数据可以从外部文件导入。</span></p>
<p> </p>
<p><span style="font-size: 16px"> 2.4、发送命令或消息</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 发送命令或消息
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="content"></param></span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> SendByte(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> content)
{
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
{
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.ckbShowSend.Checked)
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.AppendText(content);
</span><span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] buffer;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.txtSendEncoding.Text.Length <= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
buffer </span>= Encoding.Default.GetBytes(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtContent.Text);
</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.txtSendEncoding.Text == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">16进制</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
buffer </span>= <span style="color: rgba(0, 0, 255, 1)">this</span>.HexToByte(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtContent.Text);
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
buffer </span>= Encoding.GetEncoding(<span style="color: rgba(0, 0, 255, 1)">this</span>.txtSendEncoding.Text).GetBytes(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.txtContent.Text);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">向串口发送数据</span>
<span style="color: rgba(0, 0, 255, 1)">this</span>._SerialPort.Write(buffer, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, buffer.Length);
}
</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ex)
{
WinMessageBox.Warning(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">发送失败:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> ex.Message);
}
}</span></pre>
</div>
<p> </p>
<p><span style="font-size: 16px"> 2.5、接收读取返回结果</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 接收读取返回结果
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="sender"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="e"></param></span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> SerialPort_DataReceived(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, SerialDataReceivedEventArgs e)
{
</span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] buffer = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">._SerialPort.ReadBufferSize];
</span><span style="color: rgba(0, 0, 255, 1)">int</span> count = <span style="color: rgba(0, 0, 255, 1)">this</span>._SerialPort.Read(buffer, <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, buffer.Length);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">string str = Encoding.Default.GetString(readBuffer).TrimEnd('\0');</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.txtEncoding.Text.Length <= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.AppendText(System.Text.Encoding.Default.GetString(buffer,<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, count));
</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.txtEncoding.Text == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">16进制</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.AppendText(<span style="color: rgba(0, 0, 255, 1)">this</span>.ByteToHex(buffer,<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,count));
</span><span style="color: rgba(0, 0, 255, 1)">else</span>
<span style="color: rgba(0, 0, 255, 1)">this</span>.AppendText(System.Text.Encoding.GetEncoding(<span style="color: rgba(0, 0, 255, 1)">this</span>.txtEncoding.Text).GetString(buffer,<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,count));
}</span></pre>
</div>
<p> </p>
<p><span style="font-size: 16px"> 3、结语</span></p>
<p><span style="font-size: 16px"> 至此介绍完毕,本项目开源,源码地址:https://gitee.com/ShiQuan25/SerialHelper </span></p>
<p><span style="font-size: 16px"> 安装包下载地址:https://gitee.com/ShiQuan25/SerialHelper/attach_files/1048876/download/ShiQuan.SerialHelper.zip</span></p>
<p><span style="font-size: 16px"> 不当之处,欢迎指正!</span></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
作者:HengXiao
<p>
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:896374871@qq.comQQ:896374871联系我,非常感谢。<br><br>
来源:https://www.cnblogs.com/henxiao25/p/16219156.html
頁:
[1]