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

相关推荐
向宇it5 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
九鼎科技-Leo5 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
Heaphaestus,RC6 小时前
【Unity3D】获取 GameObject 的完整层级结构
unity·c#
baivfhpwxf20236 小时前
C# 5000 转16进制 字节(激光器串口通讯生成指定格式命令)
开发语言·c#
直裾7 小时前
Scala全文单词统计
开发语言·c#·scala
ZwaterZ8 小时前
vue el-table表格点击某行触发事件&&操作栏点击和row-click冲突问题
前端·vue.js·elementui·c#·vue
ZwaterZ10 小时前
el-table-column自动生成序号&&在序号前插入图标
前端·javascript·c#·vue
SRC_BLUE_1713 小时前
SQLI LABS | Less-55 GET-Challenge-Union-14 Queries Allowed-Variation 2
oracle·c#·less
yngsqq14 小时前
037集——JoinEntities连接多段线polyline和圆弧arc(CAD—C#二次开发入门)
开发语言·c#·swift
Zԅ(¯ㅂ¯ԅ)14 小时前
C#桌面应用制作计算器进阶版01
开发语言·c#