C#串口通信的实现

1、实现代码

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialPortDemo
{
    public partial class MainForm : Form
    {
        private readonly SerialPort serialPort=new SerialPort();
        public MainForm()
        {
            InitializeComponent();
            GetSerialPortName();
        }

        private void GetSerialPortName()
        {
            string[] names=SerialPort.GetPortNames();
            cbbName.DataSource = names;
            int[] bauds = new int[] { 4800, 9600, 11520 };
            cbbBaud.DataSource = bauds;
            cbbBaud.SelectedIndex = 1;
            int[] datas = new int[] { 6, 7, 8 };
            cbbData.DataSource = datas;
            cbbData.SelectedIndex = 2;
            Parity[] parities = new Parity[] {Parity.None, Parity.Odd, Parity.Even, Parity.Mark, Parity.Space};
            cbbParity.DataSource = parities;
            StopBits[] stopBits = new StopBits[] {StopBits.One, StopBits.Two, StopBits.OnePointFive };
            cbbStop.DataSource = stopBits;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (btnOpen.Text.Equals("打开"))
            {
                btnOpen.Text = "关闭";
                serialPort.PortName = Convert.ToString(cbbName.SelectedItem);
                serialPort.BaudRate = Convert.ToInt32(cbbBaud.SelectedItem);
                serialPort.DataBits = Convert.ToInt32(cbbData.SelectedItem);
                serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), cbbParity.SelectedItem.ToString());
                serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cbbStop.SelectedItem.ToString());
                //StopBits sb = (StopBits)cbbStop.SelectedIndex;
                //serialPort.StopBits= StopBits.One;
                serialPort.Handshake = Handshake.None;
                serialPort.DataReceived += SerialPort_DataReceived;
                serialPort.Open();
            }
            else
            {
                btnOpen.Text = "打开";
                serialPort.DataReceived -= SerialPort_DataReceived;
                serialPort.Close();
            }
        }

        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            this.Invoke(new Action(() =>
            {
                lbxReceive.Items.Add(indata);
            }));
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if(serialPort.IsOpen)
            {
                serialPort.Write(tbSend.Text.Trim());
            }
        }
    }
}

2、运行结果

相关推荐
就是有点傻2 小时前
使用PaddleOCRSharp大模型精选文字识别
c#
LeonDL1682 小时前
【通用视觉框架】基于C#+Winform+OpencvSharp开发的视觉框架软件,全套源码,开箱即用
人工智能·c#·winform·opencvsharp·机器视觉软件框架·通用视觉框架·机器视觉框架
数据的世界014 小时前
技术变革:为何C#与.NET是未来的开发方向
java·c#·.net
大龄Python青年4 小时前
C#快入教程:Linux安装.NET
linux·c#·.net
我是唐青枫5 小时前
C#.NET Random 深入解析:随机数生成原理与最佳实践
c#·.net
光头闪亮亮5 小时前
电子发票解析工具-c#桌面应用开发案例详解
c#
咕白m6256 小时前
如何通过 C# 提取 PDF 图片?单页与全文档提取
c#·.net
时光追逐者10 小时前
【拾遗补漏】.NET 常见术语集
微软·c#·.net
秋雨雁南飞10 小时前
c# 使用Memory实现Redis入队出队功能
redis·c#
2501_9307077812 小时前
使用C#代码添加或删除PPT页面
开发语言·c#·powerpoint