基本信息
源码名称:简单的串口助手开发,初学者必备
源码大小:0.18M
文件格式:.rar
开发语言:C#
更新时间:2019-08-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

串口助手

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());
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}