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

相关推荐
hixiong1231 小时前
C# OpenvinoSharp使用DINOv2模型进行图像相似度计算
开发语言·c#
月昤昽3 小时前
autoCAD二次开发 4.正多边形与collection区分
算法·c#·二次开发·autocad二次开发
Victory_20253 小时前
c#定时器顺序控制写法
开发语言·c#·c#顺序控制+定时器
雪度娃娃6 小时前
基于TCP的网络词典
网络·c++·tcp/ip·c#
xxjj998a7 小时前
PHP vs C#:两大编程语言终极对比
开发语言·c#·php
工程师0077 小时前
C# 泛型:约束、协变逆变、底层模板生成机制
c#·泛型·逆变·协变
bestcxx8 小时前
多个维度对 Java、Python、C#、Go 这四种主流编程语言进行比较
java·python·c#
我是唐青枫8 小时前
内存为什么越来越高?C#.NET GC 详解:分代回收、LOH、终结器与性能优化实战
性能优化·c#·.net
游乐码9 小时前
c#反射笔记二
笔记·c#
工程师0079 小时前
C# 变量:生命周期、作用域、变量逃逸
c#·生命周期·作用域·逃逸