找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7677|回复: 16
收起左侧

C#制作串口助手源码

  [复制链接]
ID:473315 发表于 2019-1-27 11:37 | 显示全部楼层 |阅读模式
用C#做的一个简易串口助手,可以作为练手使用,再也不用别人的串口助手了HHHH
基本功能都有,代码也有很多注释,欢迎下载。

程序运行界面:
QQ截图20190127113359.png 0.png

程序源码如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;

  11. namespace WinFormSeriport
  12. {
  13.     public partial class FORM1 : Form
  14.     {
  15.         SerialPort sp = new SerialPort();
  16.         
  17.         public FORM1()
  18.         {
  19.             Control.CheckForIllegalCrossThreadCalls = false;
  20.             InitializeComponent();
  21.             sp.Close();
  22.             lbl_TxRx.Text = "Tx:" +0+" " + "Rx:" + 0;
  23.         }
  24.         private void Form1_Load_1(object sender, EventArgs e)
  25.         {
  26.             Timer_SendDate.Interval = 1000;
  27.             Timer_SendDate.Stop();
  28.         }

  29.         
  30.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  31.         {
  32.             lbl_Move.Text = e.X.ToString() + " " + e.Y.ToString();
  33.         }

  34.       

  35.         private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
  36.         {
  37.             MessageBox.Show("接收数据时发生未知错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
  38.         }

  39.         private void btn_OpenSerial_Click(object sender, EventArgs e)
  40.         {
  41.             if (sp.IsOpen == false)
  42.             {
  43.                 btn_OpenSerial.Text = "开启串口";
  44.                 try
  45.                 {
  46.                     sp.PortName = comboBox_Com.Text;
  47.                     sp.BaudRate = Convert.ToInt32(comboBox_Bot.Text);
  48.                     sp.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), comboBox_Parity.Text);
  49.                     sp.DataBits = Convert.ToInt16(comboBox_DateBit.Text);
  50.                     sp.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), comboBox_StopBit.Text);
  51.                     sp.Encoding = Encoding.Default;    //设置串口编码为default:获取操作系统的当前 ANSI 代码页的编码。
  52.                     sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);

  53.                     sp.Open();    //开启串口
  54.                     if (checkBox_ClearShow.Checked)   //如果开启了自动清空功能
  55.                         Timer_SendDate.Start();

  56.                     btn_OpenSerial.Text = "关闭串口";
  57.                 }
  58.                 catch
  59.                 {
  60.                     MessageBox.Show("配置串口出错,检查各项参数是否设置!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 btn_OpenSerial.Text = "关闭串口";
  66.                 try
  67.                 {
  68.                     sp.Close();

  69.                     btn_OpenSerial.Text = "开启串口";
  70.                     MessageBox.Show("串口已关闭!","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
  71.                 }
  72.                 catch
  73.                 {
  74.                     MessageBox.Show("出现未知错误,串口关闭失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);      
  75.                 }
  76.             }

  77.         }
  78.         string receiveData;
  79.         UInt32 receiveBytesCount;
  80.         string sendData;
  81.         UInt32 sendBytesCount;
  82.         private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
  83.         {

  84.             receiveData = sp.ReadExisting();
  85.             receiveBytesCount += (UInt32)receiveData.Length;


  86.             //字符串显示
  87.             if (checkBox_ClearShow.Checked == false)
  88.             {
  89.                 if (checkBox_16Show.Checked == false)   //hex 方式显示
  90.                 {
  91.                     richtxtbox_ShowGetNumber.AppendText(receiveData);
  92.                     richtxtbox_ShowGetNumber.Focus();   //让光标到这来
  93.                     richtxtbox_ShowGetNumber.Select(richtxtbox_ShowGetNumber.TextLength, 0);
  94.                     this.richtxtbox_ShowGetNumber.ScrollToCaret(); //设置光标到最后
  95.                 }
  96.                 //16进制显示
  97.                 else
  98.                 {
  99.                     byte[] recData = System.Text.Encoding.Default.GetBytes(receiveData);// 将接受到的字符串据转化成数组;  

  100.                     foreach (byte str in recData)
  101.                     {
  102.                         richtxtbox_ShowGetNumber.AppendText(string.Format("{0:X2} ", str));
  103.                     }
  104.                 }
  105.             }
  106.             lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();
  107.         }


  108.         private void btn_GetCom_Click(object sender, EventArgs e)  //自动获取串口号
  109.         {
  110.             string[] serialPortName = System.IO.Ports.SerialPort.GetPortNames();

  111.             foreach (string name in serialPortName)
  112.             {
  113.                 comboBox_Com.Text = name;
  114.             }
  115.         }

  116.         private void btn_Send_Click(object sender, EventArgs e)
  117.         {
  118.             if (sp.IsOpen == false)
  119.             {
  120.                 btn_OpenSerial.Text = "开启串口";
  121.                 MessageBox.Show("请打开串口!", "错误",MessageBoxButtons.OK, MessageBoxIcon.Information);
  122.             }
  123.             else
  124.             {
  125.                 btn_OpenSerial.Text = "关闭串口";
  126.                 if (checkBox_SendByItself.Checked)
  127.                 {
  128.                     Timer_SendDate.Interval = Convert.ToInt32(textBox_SendTime.Text);
  129.                     Timer_SendDate.Start();
  130.                 }
  131.                 sendData = richTextBox_Send.Text;
  132.                 if (checkBox_SendIn16.Checked == false)
  133.                 {
  134.                     sp.Write(sendData);
  135.                 }
  136.                 else
  137.                 {
  138.                     try
  139.                     {
  140.                         sendData.Replace("0x", "");   //去掉0x
  141.                         sendData.Replace("0X", "");   //去掉0X
  142.                         //  sendData.

  143.                         string[] strArray = sendData.Split(new char[] { ',', ',', '\r', '\n', ' ', '\t' });
  144.                         int decNum = 0;
  145.                         int i = 0;
  146.                         byte[] sendBuffer = new byte[strArray.Length];  //发送数据缓冲区

  147.                         foreach (string str in strArray)
  148.                         {
  149.                             try
  150.                             {
  151.                                 decNum = Convert.ToInt16(str, 16);
  152.                                 sendBuffer[i] = Convert.ToByte(decNum);
  153.                                 i++;
  154.                             }
  155.                             catch
  156.                             {

  157.                             }
  158.                         }
  159.                         sp.Write(sendBuffer, 0, sendBuffer.Length);

  160.                         //更新发送数据计数
  161.                         sendBytesCount += (UInt32)sendBuffer.Length;

  162.                         lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();

  163.                     }
  164.                     catch
  165.                     {

  166.                     }
  167.                 }
  168.             }
  169.         }

  170.         private void Timer_SendDate_Tick(object sender, EventArgs e)
  171.         {

  172.             if (checkBox_ClearShow.Checked)
  173.                 richtxtbox_ShowGetNumber.Text = null;

  174.             sendData = richTextBox_Send.Text;
  175.    
  176.             if (sp.IsOpen)
  177.             {
  178.                 btn_OpenSerial.Text = "关闭串口";
  179.                 if (checkBox_SendByItself.Checked)
  180.                 {
  181.                     if (checkBox_SendIn16.Checked == false)
  182.                     {
  183.                         sp.Write(sendData);
  184.                     }
  185.                     else
  186.                     {
  187.                         try
  188.                         {
  189.                             sendData.Replace("0x", "");   //去掉0x
  190.                             sendData.Replace("0X", "");   //去掉0X
  191.                             //  sendData.

  192.                             string[] strArray = sendData.Split(new char[] { ',', ',', '\r', '\n', ' ', '\t' });
  193.                             int decNum = 0;
  194.                             int i = 0;
  195.                             byte[] sendBuffer = new byte[strArray.Length];  //发送数据缓冲区

  196.       

  197.                             foreach (string str in strArray)
  198.                             {
  199.                                 try
  200.                                 {
  201.                                     decNum = Convert.ToInt16(str, 16);
  202.                                     sendBuffer[i] = Convert.ToByte(decNum);
  203.                                     i++;
  204.                                 }
  205.                                 catch
  206.                                 {

  207.                                 }
  208.                             }
  209.                             sp.Write(sendBuffer, 0, sendBuffer.Length);

  210.                         }
  211.                         catch
  212.                         {

  213.                         }
  214.                     }
  215.                     //更新发送数据计数
  216.                     sendBytesCount += (UInt32)sendData.Length;

  217.                     lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();
  218.                 }
  219.                 else
  220.                 {
  221.                  if (checkBox_ClearShow.Checked)
  222.                     checkBox_SendByItself.CheckState=CheckState .Unchecked ;
  223.                 else
  224.                     Timer_SendDate.Stop();
  225.                 }
  226.             }

  227.             else
  228.             {
  229.                     btn_OpenSerial.Text = "开启串口";
  230.                     Timer_SendDate.Stop();
  231.                     MessageBox.Show("串口已经关闭!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);      
  232.             }
  233.         
  234.         }

  235.         private void btn_ClearAll_Click(object sender, EventArgs e)
  236.         {
  237.             richtxtbox_ShowGetNumber.Text = null;
  238.             richTextBox_Send.Text = null;
  239.         }
  240.     }
  241. }
复制代码

所有资料51hei提供下载:
WinFormSeriport.zip (88.05 KB, 下载次数: 281)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:18591 发表于 2019-3-3 19:25 | 显示全部楼层
看看~~~~~~~~~~~~~~
回复

使用道具 举报

ID:337813 发表于 2019-8-6 08:46 | 显示全部楼层
非常感谢您的无私分享,谢谢!
回复

使用道具 举报

ID:99124 发表于 2019-12-7 21:21 | 显示全部楼层
学习一下,要是界面能全屏就好了。
回复

使用道具 举报

ID:67573 发表于 2019-12-13 12:23 | 显示全部楼层
非常感谢您的无私分享,谢谢!
回复

使用道具 举报

ID:491495 发表于 2020-1-17 20:53 | 显示全部楼层
非常好的资料,学习一下
回复

使用道具 举报

ID:687464 发表于 2020-1-19 23:30 | 显示全部楼层
挺好~学习一下
回复

使用道具 举报

ID:101466 发表于 2020-2-17 22:33 | 显示全部楼层
还是这里好,下载方便,谢谢
回复

使用道具 举报

ID:694712 发表于 2020-2-18 15:24 | 显示全部楼层
感谢分享。。。
回复

使用道具 举报

ID:59830 发表于 2020-2-20 18:11 | 显示全部楼层
看了一下,有些帮助。谢谢分享。
回复

使用道具 举报

ID:734421 发表于 2020-5-3 21:02 | 显示全部楼层
感谢,内容可以
回复

使用道具 举报

ID:195703 发表于 2020-5-6 11:58 | 显示全部楼层
谢谢分享
回复

使用道具 举报

ID:749484 发表于 2020-5-13 09:58 | 显示全部楼层
支持一下原创,我也有打算写一个类似的,学习一下。
回复

使用道具 举报

ID:807693 发表于 2022-11-6 16:12 | 显示全部楼层
感谢,想参考下
回复

使用道具 举报

ID:121968 发表于 2022-11-23 14:22 | 显示全部楼层
非常好的资料,学习一下
回复

使用道具 举报

ID:1016815 发表于 2023-2-14 22:27 | 显示全部楼层
非常感谢您的无私分享,谢谢!
回复

使用道具 举报

ID:1051997 发表于 2023-3-15 15:46 | 显示全部楼层
感谢分享,对初学者来说是个很好的例子
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表