基于C#的简易串口调试软件(SunnyUI学习二)

目录

一、基于SunnyUI的C#简易串口调试软件界面

二、源代码

三、视频演示


一、基于SunnyUI的C#简易串口调试软件界面

        上篇文章已经学会怎么在线导入SunnyUI并用于Winform程序中,本文利用前面所学的知识编写一个简易串口调试程序,UI界面如下:

该界面简洁美观,只有基本的可编辑发送和接收区域。

二、源代码

        源代码如下,如果需要压缩包文件直接点击绑定的资源进行下载。

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 Sunny.UI;
using System.IO.Ports;
using System.Threading;
using System.Reflection.Emit;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Net.Mime.MediaTypeNames;

namespace SerialPortCommunications
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
            uiCheckBoxStringSend.Checked = true;
            uiCheckBoxStringDisplay.Checked = true;
        }

        SerialPort serialPort = new SerialPort();
        bool autoSendFlag = false;
        
        private void uiComboBoxCOMports_DropDown(object sender, EventArgs e) //扫描并选择本机串口
        {
            uiComboBoxCOMports.Items.Clear();
            string[] Ports = SerialPort.GetPortNames(); //获取当前计算机的所有COM口
            foreach (string Port in Ports)
            {
                if (Ports.Contains(Port))
                {
                    uiComboBoxCOMports.Items.Add(Port); //把所有COM口添加到组合框控件选项中
                }
            }
            uiComboBoxCOMports.Text = Ports[Ports.Count() - 1]; //选中最新获取的COM口
        }

        private void SerialSet() //串口配置设置
        {
            serialPort.PortName = uiComboBoxCOMports.Text; //选择串口号
            serialPort.BaudRate = uiComboBoxbaudRate.Text.ToInt(); //设置波特率
            switch (uiComboBoxParityBits.Text) //设置校验位
            {
                case "无None": serialPort.Parity = Parity.None; break;
                case "偶Even": serialPort.Parity = Parity.Even; break;
                case "奇Odd": serialPort.Parity = Parity.Odd; break;
                case "标志Mark": serialPort.Parity = Parity.Mark; break;
                case "空格Space": serialPort.Parity = Parity.Space; break;
                default: serialPort.Parity = Parity.None; break;
            }
            serialPort.DataBits = uiComboBoxDataBits.Text.ToInt(); //设置数据位
            switch (uiComboBoxStopBits.Text) //设置停止位
            {
                case "1": serialPort.StopBits = StopBits.One; break;
                case "2": serialPort.StopBits = StopBits.Two; break;
                default: serialPort.StopBits = StopBits.One; break;
            }
        }

        private async void uiButtonOpenOrCloseSerialPort_Click(object sender, EventArgs e) //打开或关闭串口
        {

            try
            {
                if (serialPort.IsOpen) //关闭串口
                {
                    serialPort.Close();
                    uiLedBulb1.On = false;
                    uiButtonOpenOrCloseSerialPort.FillColor = Color.Lime;
                    uiButtonOpenOrCloseSerialPort.Text = "打开串口";
                }
                else //打开串口
                {
                    SerialSet();
                    serialPort.Open();
                    uiLedBulb1.On = true;
                    uiButtonOpenOrCloseSerialPort.FillColor = Color.Red;
                    uiButtonOpenOrCloseSerialPort.Text = "关闭串口";
                }
            }
            catch (Exception ex)
            {
                UIMessageBox.Show("串口打开失败,该串口可能已被其他程序占用!!!");
            }
        }

        private void uiButtonManulSend_Click(object sender, EventArgs e) //手动发送
        {
            string stringData = uiRichTextBoxSendArea.Text;
            SendData(stringData);
        }

        private void SendData(string stringData) //发送数据方法
        {
            if (serialPort.IsOpen)
            {
                if (uiCheckBoxStringSend.Checked) //以字符串格式发送
                {
                    if (stringData.Length != 0)
                    {
                        serialPort.Write(stringData);
                    }
                    else
                    {
                        UIMessageBox.Show("请输入要发送的数据!!!");
                    }
                }
                else //以十六进制格式发送
                {
                    // 要发送的十六进制字节数组
                    if (stringData.Length != 0)
                    {
                        byte[] sendData = null;
                        string send = stringData.Trim();
                        //按照指定编码将string编程字节数组
                        byte[] b = Encoding.GetEncoding("GBK").GetBytes(send);
                        string result = string.Empty;
                        //逐字节变为16进制字符
                        for (int i = 0; i < b.Length; i++)
                        {
                            result += Convert.ToString(b[i], 16).ToUpper() + " ";
                        }
                        sendData = Encoding.GetEncoding("GBK").GetBytes(result);
                        serialPort.Write(sendData, 0, sendData.Length);
                    }
                    else
                    {
                        UIMessageBox.Show("请输入要发送的数据!!!");
                    }
                }
            }
            else
            {
                UIMessageBox.Show("串口未打开,请先打开串口!!!");
            }
        }

        private void uiCheckBoxHexSend_Click(object sender, EventArgs e) //选择16进制发送
        {
            uiCheckBoxStringSend.Checked = false;
            uiCheckBoxHexSend.Checked = true;
            return;
        }

        private void uiCheckBoxStringSend_Click(object sender, EventArgs e) //选择字符串发送
        {
            uiCheckBoxHexSend.Checked = false;
            uiCheckBoxStringSend.Checked = true;
            return;
        }

        private void uiButtonClearSendArea_Click(object sender, EventArgs e) //清空发送区
        {
            uiRichTextBoxSendArea.Text = string.Empty;
        }

        private void uiButtonUpDateTime_Click(object sender, EventArgs e) //更新时间
        {
            uiRichTextBoxSendArea.Text = DateTime.Now.ToString();
        }

        private void uiButtonClearRecieveArea_Click(object sender, EventArgs e) //清空接收区
        {
            uiRichTextBoxRecieveArea.Text = string.Empty;
        }

        private async void uiButtonAutoSend_Click(object sender, EventArgs e) //自动发送数据
        {
            string stringData = uiRichTextBoxSendArea.Text;
            int AutoSendCycleMs = 0;
            if (uiTextBoxAutoSendCycleMs.Text != string.Empty)
            {
                AutoSendCycleMs = uiTextBoxAutoSendCycleMs.Text.ToInt();
            }
            else
            {
                AutoSendCycleMs = 0;
            }
            autoSendFlag = !autoSendFlag;
            await Task.Run(() => //更新UI界面
            {
                if (serialPort.IsOpen)
                {
                    while (true)
                    {
                        SendData(stringData);
                        uibuttonTextUpdate(uiButtonAutoSend, "停止发送");
                        Task.Delay(AutoSendCycleMs).Wait();
                        if (!autoSendFlag)
                        {
                            uibuttonTextUpdate(uiButtonAutoSend, "自动发送");
                            break;
                        }
                    }
                }
                else
                {
                    UIMessageBox.Show("串口未打开,请先打开串口!!!");
                }
            });
        }

        private void uibuttonTextUpdate(UIControl uIControl, string text) //委托的方式更新按键文本
        {
            //采用委托的方式更新UI界面label1显示的字符串 
            if (uIControl.InvokeRequired) //label1委托请求
            {
                while (!uIControl.IsHandleCreated)
                {
                    //解决窗体关闭时出现“访问已释放句柄“的异常
                    if (uIControl.Disposing || uIControl.IsDisposed)
                        return;
                }
                Invoke(new Action(() => { uIControl.Text = text; }));
            }
            else
            {
                uIControl.Text = text;
            }
        }

        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) //串口数据接收处理
        {
            //异步委托一个线程,不然接收数据时会出现数据线程错误
            this.Invoke((EventHandler)delegate
            {
                if (serialPort.IsOpen)
                {
                    try
                    {
                        byte[] receivedData = new byte[serialPort.BytesToRead];//创建接收数据数组
                        serialPort.Read(receivedData, 0, receivedData.Length);//读取数据
                        var content = string.Empty;
                        //显示形式
                        if (uiCheckBoxHexDisplay.Checked && !uiCheckBoxStringDisplay.Checked)
                        {
                            for (int i = 0; i < receivedData.Length; i++)
                            {
                                //ToString("X2") 为C#中的字符串格式控制符
                                //X为     十六进制
                                //2为 每次都是两位数
                                content += (receivedData[i].ToString("X2") + " ");
                            }
                        }
                        else if (!uiCheckBoxHexDisplay.Checked && uiCheckBoxStringDisplay.Checked)
                        {
                            content = Encoding.GetEncoding("GB2312").GetString(receivedData);//防止乱码
                        }
                        //接收文本框
                        uiRichTextBoxRecieveArea.AppendText(content); //接收区域追加数据    
                        uiRichTextBoxRecieveArea.SelectionStart = uiRichTextBoxRecieveArea.Text.Length; //将光标设置到末尾位置
                        uiRichTextBoxRecieveArea.ScrollToCaret(); //接收区域更新显示最后一行
                        serialPort.DiscardInBuffer();//丢弃缓存区数据
                    }
                    catch (Exception ex)
                    {
                        UIMessageBox.Show(ex.Message, "Error");
                    }
                }
                else
                {
                    UIMessageBox.Show("请打开串口", "Error");
                }
            });
        }

        private void uiCheckBoxStringDisplay_Click(object sender, EventArgs e) //选择字符串显示
        {
            uiCheckBoxStringDisplay.Checked = true;
            uiCheckBoxHexDisplay.Checked = false;
        }

        private void uiCheckBoxHexDisplay_Click(object sender, EventArgs e) //选择16进制显示
        {
            uiCheckBoxStringDisplay.Checked = false;
            uiCheckBoxHexDisplay.Checked = true;
        }
        private void Form1_Load(object sender, EventArgs e) //采用委托的方式实时接收数据并显示
        {
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
        }
    }
}

三、视频演示

C#简易串口调试软件

  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,下面是一个简单的C#串口调试软件示例: ```csharp using System; using System.IO.Ports; using System.Windows.Forms; namespace SerialPortDebug { public partial class MainForm : Form { private SerialPort serialPort; public MainForm() { InitializeComponent(); serialPort = new SerialPort(); serialPort.BaudRate = 9600; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; serialPort.Parity = Parity.None; serialPort.DataReceived += SerialPort_DataReceived; } private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { string data = serialPort.ReadExisting(); this.Invoke((MethodInvoker)delegate { textBoxReceived.AppendText(data); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void buttonOpen_Click(object sender, EventArgs e) { if (!serialPort.IsOpen) { try { serialPort.PortName = comboBoxPortName.Text; serialPort.Open(); buttonOpen.Text = "Close"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { serialPort.Close(); buttonOpen.Text = "Open"; } } private void buttonSend_Click(object sender, EventArgs e) { string data = textBoxSend.Text; serialPort.Write(data); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort.IsOpen) { serialPort.Close(); } } private void MainForm_Load(object sender, EventArgs e) { string[] portNames = SerialPort.GetPortNames(); comboBoxPortName.Items.AddRange(portNames); if (comboBoxPortName.Items.Count > 0) { comboBoxPortName.SelectedIndex = 0; } } } } ``` 这个示例中,我们创建了一个 `SerialPort` 对象,设置了串口的参数,并在窗体的 `Load` 事件中获取可用串口列表,并将其添加到一个下拉框中。用户可以选择要连接的串口,点击 `Open` 按钮打开或关闭串口连接。当串口接收到数据时,将会触发 `DataReceived` 事件,我们在该事件中读取数据并将其显示在接收文本框中。用户可以输入要发送的数据并点击 `Send` 按钮将数据发送到串口。当窗体关闭时,如果串口连接还未关闭,程序会自动关闭串口连接。 注意:该示例仅供参考,实际应用中可能需要根据具体需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零壹电子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值