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

相关推荐
坐井观老天4 小时前
在C#中使用资源保存图像和文本和其他数据并在运行时加载
开发语言·c#
pchmi6 小时前
C# OpenCV机器视觉:模板匹配
opencv·c#·机器视觉
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭8 小时前
C#都可以找哪些工作?
开发语言·c#
boligongzhu10 小时前
Dalsa线阵CCD相机使用开发手册
c#
向宇it1 天前
【从零开始入门unity游戏开发之——C#篇23】C#面向对象继承——`as`类型转化和`is`类型检查、向上转型和向下转型、里氏替换原则(LSP)
java·开发语言·unity·c#·游戏引擎·里氏替换原则
sukalot1 天前
windows C#-命名实参和可选实参(下)
windows·c#
小码编匠1 天前
.NET 下 RabbitMQ 队列、死信队列、延时队列及小应用
后端·c#·.net
我不是程序猿儿1 天前
【C#】Debug和Release的区别和使用
开发语言·c#
lzhdim1 天前
2、C#基于.net framework的应用开发实战编程 - 设计(二、二) - 编程手把手系列文章...
开发语言·c#·.net