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、运行结果

相关推荐
isyoungboy1 小时前
c#实现鼠标mousemove事件抽稀,避免大数据阻塞网络
c#·计算机外设·远程桌面·deskflow
一枚小小程序员哈5 小时前
基于asp.net 的在线餐饮订餐系统的设计与实现/基于c#的网上订餐系统/餐厅管理系统
后端·c#·asp.net
好望角雾眠5 小时前
第三阶段数据库-7:sql中函数,运算符,常用关键字
数据库·笔记·sql·学习·sqlserver·c#
海绵宝宝汉堡包9 小时前
c# 项目 文件夹
开发语言·c#
曹牧12 小时前
C#:窗体间传值
c#
一个会的不多的人17 小时前
C# NX二次开发:面收集器控件和曲线收集器控件详解
开发语言·c#
创可贴治愈心灵18 小时前
WPF中UI线程频繁操作造成卡顿的处理
ui·c#·wpf
格林威1 天前
Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型和EasyOCR实现汽车牌照动态检测和识别(C#代码,UI界面版)
人工智能·深度学习·数码相机·yolo·c#·汽车·视觉检测
Aczone281 天前
Linux 软件编程(九)网络编程:IP、端口与 UDP 套接字
linux·网络·网络协议·tcp/ip·http·c#
chenglin0161 天前
C#_接口设计:角色与契约的分离
java·前端·c#