找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3748|回复: 2
打印 上一主题 下一主题
收起左侧

串口助手上位机(C#源码)

[复制链接]
跳转到指定楼层
楼主
c#上位机源码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;

  10. using System.IO.Ports;
  11. using System.Text.RegularExpressions;

  12. namespace COM
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         //声明了一个delegate 类型
  17.         public delegate void Displaydelegate(byte[] InputBuf);
  18.         //声明了一个delegate 对象
  19.         public Displaydelegate disp_delegate;
  20.         //将串口接收到的数据显示到textBox1上
  21.         public void DispUI(byte[] InputBuf)
  22.         {

  23.             ASCIIEncoding encoding = new ASCIIEncoding();
  24.             if (radioButton3.Checked)
  25.             {
  26.                 foreach (byte b in InputBuf)
  27.                 {
  28.                     //将数值转换成16进制数并追加一个空格并显示到textBox1上
  29.                     textBox1.AppendText(b.ToString("X2") + " ");
  30.                 }
  31.             }
  32.             if (radioButton4.Checked)
  33.             {
  34.                 //直接将数值转换成字符串并显示并显示到textBox1上
  35.                 textBox1.AppendText(encoding.GetString(InputBuf));
  36.             }

  37.         }


  38.         public Form1()
  39.         {
  40.             //创建一个delegate 对象
  41.             disp_delegate = new Displaydelegate(DispUI);
  42.             InitializeComponent();
  43.         }

  44.         private void Form1_Load(object sender, EventArgs e)
  45.         {
  46.             //获取端口名字 使用前需要添加 using System.IO.Ports;
  47.             string[] PortName = SerialPort.GetPortNames();
  48.             Array.Sort(PortName);//给端口名称排序
  49.             for (int i = 0; i < PortName.Length; i++)
  50.             {
  51.                 comboBox1.Items.Add(PortName[i]);//给comboBox1添加选项
  52.             }
  53.         }

  54.         private void button1_Click(object sender, EventArgs e)
  55.         {
  56.             try
  57.             {
  58.                 serialPort1.PortName = comboBox1.Text;//更改端口名称
  59.                 //因为存储在comboBox2中的数值都为字符串,所以需要将端口号转换为10进制数值
  60.                 serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
  61.                 serialPort1.Open();//打开串口
  62.                 button1.Enabled = false;//"打开串口"按键失效
  63.                 button2.Enabled = true;//"关闭串口"按键使能
  64.             }
  65.             catch
  66.             {
  67.                 MessageBox.Show("端口错误,请检查端口", "错误");
  68.             }
  69.         }
  70.         private void button2_Click(object sender, EventArgs e)
  71.         {
  72.             try
  73.             {
  74.                 serialPort1.Close();//关闭串口
  75.                 button1.Enabled = true;//"打开串口"按键使能
  76.                 button2.Enabled = false;//"关闭串口"按键失效
  77.             }
  78.             catch
  79.             {

  80.             }
  81.         }

  82.         private void button3_Click(object sender, EventArgs e)
  83.         {
  84.             if (!serialPort1.IsOpen)//如果没有打开串口就报错并返回
  85.             {
  86.                 MessageBox.Show("串口写入失败", "错误");
  87.                 serialPort1.Close();
  88.                 button1.Enabled = true;
  89.                 button2.Enabled = false;
  90.                 return;
  91.             }
  92.             if (radioButton1.Checked)//如果选择数值发送模式
  93.             {
  94.                 List<byte> buf = new List<byte>();//填充到这个临时列表中
  95.                 //使用正则表达式获取textBox2中的有效数据
  96.                 MatchCollection mc = Regex.Matches(textBox2.Text, @"(?i)[\da-f]{2}");
  97.                 //将mc转换为16进制数据并添加到buf列表中
  98.                 foreach (Match m in mc)
  99.                 {
  100.                     byte data = Convert.ToByte(m.Value, 16);
  101.                     buf.Add(data);
  102.                 }
  103.                 //将buf列表转换为数组并通过串口发送出去
  104.                 serialPort1.Write(buf.ToArray(), 0, buf.Count);
  105.             }
  106.             else//如果选择字符发送模式
  107.             {
  108.                 serialPort1.WriteLine(textBox2.Text);
  109.             }
  110.         }

  111.         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  112.         {
  113.             int n = serialPort1.BytesToRead;//串口缓存中数据的个数
  114.             Byte[] InputBuf = new Byte[n];
  115.             try
  116.             {
  117.                 //读取串口缓存中的数据并存放到InputBuf数组中
  118.                 serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);
  119.                 //将当前线程挂起50ms
  120.                 System.Threading.Thread.Sleep(50);
  121.                 //执行委托
  122.                 this.Invoke(disp_delegate, InputBuf);
  123.             }
  124.             catch (TimeoutException ex)
  125.             {
  126.                 MessageBox.Show(ex.ToString());
  127.             }
  128.         }

  129.     }
  130. }
复制代码


全部资料51hei下载地址:
串口助手上位机(C#).zip (255.13 KB, 下载次数: 107)

评分

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

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶2 踩
回复

使用道具 举报

沙发
ID:544352 发表于 2019-9-17 16:53 | 只看该作者
很有帮助,多谢分享!
回复

使用道具 举报

板凳
ID:664669 发表于 2019-12-15 22:37 | 只看该作者
感谢分享,谢谢
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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