C#桌面应用--简单的串口助手

今天看视频学习,自己做了一个串口助手,来实现对串口数据的收发。

下边是具体的界面设计:


具体功能:设置端口号和波特率,实现对串口数据的收发;后边会增加具体的功能。

代码如下,

using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 上位机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)     //窗口的创建及初始化
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; 
            for (int i = 1; i < 20; i++)
            {
                comboBox1.Items.Add("COM"+i.ToString());
            }
            comboBox1.Text = "COM8";    
            comboBox2.Text = "9600";    //下拉列表的填充和初始值
            
            //手动添加串口的事件处理函数
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)        //定义串口数据的接收事件
        {
            if (!radioButton3.Checked)   //如果接收模式是字符模式
            {
                string str = serialPort1.ReadExisting(); //使用字符串方式读数据
                textBox1.AppendText(str);           //在当前的文本框添加内容
                //textBox1.Text += str;
            }
            else             //如果接收模式是数据接收
            {
                byte data;
                int n = serialPort1.BytesToRead;        //获取输入缓存区中的数据个数
                while (n-- != 0)
                {
                    data = (byte)serialPort1.ReadByte();        //读取一个字节数据,将int型数据转换为byte型
                    string str = Convert.ToString(data, 16).ToUpper();   //将其转换为16进制的大写字符串
                    textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");  //空位补0
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)          //打开串口按键
        {
            try
            {
                serialPort1.PortName = comboBox1.Text;                  //设置串口号
                serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text); //设置波特率
                serialPort1.Open();                                     //打开串口
                button2.Enabled = false;                                //打开串口按键不可按
                button3.Enabled = true;                                 //关闭串口按键可以按
            }
            catch
            {
                MessageBox.Show("端口打开错误,请检查串口","错误");     //弹出警告信息
            }
        }

        private void button3_Click(object sender, EventArgs e)           //关闭串口按键
        {
            serialPort1.Close();                                     //关闭串口
            button2.Enabled = true;                                  //打开串口按键可按
            button3.Enabled = false;                                 //关闭串口按键不可以按
        }

        private void button1_Click(object sender, EventArgs e)          //数据发送按键
        {
            byte[] data = new byte[1];              //定义一个1个字节的字符型的变量
            if (serialPort1.IsOpen)      //判断串口是否打开
            {
                if (textBox2.Text != " ")      //如果发送区有数据
                {
                    if (!radioButton1.Checked)      //如果不是数值模式,即为字符模式
                    {
                        try
                        {
                            serialPort1.WriteLine(textBox2.Text);   //写数据
                        }
                        catch
                        {

                        }
                    }
                    else//数值模式发送数据
                    {
                        for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)
                        {
                            try
                            {
                                data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
                                serialPort1.Write(data, 0, 1);
                            }
                            catch
                            {
                                MessageBox.Show("请确保输入的字符在0~F之间!","警告!");
                            }
                        }
                        try
                        {
                            if (textBox2.Text.Length % 2 != 0)
                            {
                                data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);
                                serialPort1.Write(data, 0, 1);
                            }
                        }
                        catch
                        {
                            MessageBox.Show("请确保输入的字符在0~F之间!", "警告!");
                        }
                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text= "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }


    }
}
注意事项:

panel 实现对radioButton的分组;

serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);函数添加串口事件处理函数

数值方式发送时,应确保字符数据在0~F之间。

下边是具体测试,接收来自STM32的串口数据,



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值