c#上位机源码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO.Ports;
- using System.Text.RegularExpressions;
- namespace COM
- {
- public partial class Form1 : Form
- {
- //声明了一个delegate 类型
- public delegate void Displaydelegate(byte[] InputBuf);
- //声明了一个delegate 对象
- public Displaydelegate disp_delegate;
- //将串口接收到的数据显示到textBox1上
- public void DispUI(byte[] InputBuf)
- {
- ASCIIEncoding encoding = new ASCIIEncoding();
- if (radioButton3.Checked)
- {
- foreach (byte b in InputBuf)
- {
- //将数值转换成16进制数并追加一个空格并显示到textBox1上
- textBox1.AppendText(b.ToString("X2") + " ");
- }
- }
- if (radioButton4.Checked)
- {
- //直接将数值转换成字符串并显示并显示到textBox1上
- textBox1.AppendText(encoding.GetString(InputBuf));
- }
- }
- public Form1()
- {
- //创建一个delegate 对象
- disp_delegate = new Displaydelegate(DispUI);
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //获取端口名字 使用前需要添加 using System.IO.Ports;
- string[] PortName = SerialPort.GetPortNames();
- Array.Sort(PortName);//给端口名称排序
- for (int i = 0; i < PortName.Length; i++)
- {
- comboBox1.Items.Add(PortName[i]);//给comboBox1添加选项
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.PortName = comboBox1.Text;//更改端口名称
- //因为存储在comboBox2中的数值都为字符串,所以需要将端口号转换为10进制数值
- serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
- serialPort1.Open();//打开串口
- button1.Enabled = false;//"打开串口"按键失效
- button2.Enabled = true;//"关闭串口"按键使能
- }
- catch
- {
- MessageBox.Show("端口错误,请检查端口", "错误");
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Close();//关闭串口
- button1.Enabled = true;//"打开串口"按键使能
- button2.Enabled = false;//"关闭串口"按键失效
- }
- catch
- {
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen)//如果没有打开串口就报错并返回
- {
- MessageBox.Show("串口写入失败", "错误");
- serialPort1.Close();
- button1.Enabled = true;
- button2.Enabled = false;
- return;
- }
- if (radioButton1.Checked)//如果选择数值发送模式
- {
- List<byte> buf = new List<byte>();//填充到这个临时列表中
- //使用正则表达式获取textBox2中的有效数据
- MatchCollection mc = Regex.Matches(textBox2.Text, @"(?i)[\da-f]{2}");
- //将mc转换为16进制数据并添加到buf列表中
- foreach (Match m in mc)
- {
- byte data = Convert.ToByte(m.Value, 16);
- buf.Add(data);
- }
- //将buf列表转换为数组并通过串口发送出去
- serialPort1.Write(buf.ToArray(), 0, buf.Count);
- }
- else//如果选择字符发送模式
- {
- serialPort1.WriteLine(textBox2.Text);
- }
- }
- private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- int n = serialPort1.BytesToRead;//串口缓存中数据的个数
- Byte[] InputBuf = new Byte[n];
- try
- {
- //读取串口缓存中的数据并存放到InputBuf数组中
- serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);
- //将当前线程挂起50ms
- System.Threading.Thread.Sleep(50);
- //执行委托
- this.Invoke(disp_delegate, InputBuf);
- }
- catch (TimeoutException ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- }
- }
复制代码
全部资料51hei下载地址:
串口助手上位机(C#).zip
(255.13 KB, 下载次数: 107)
|