C#----串口数据接收发送

本文介绍如何使用C#进行串行端口通信,包括如何创建串口连接、读取和写入数据,并通过示例代码展示了串口数据的接收与发送过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虚拟串口驱动工具,创建俩个虚拟串口,如图:

 

创建两个Console模拟串口的发送接收数据

C#串口数据接收发送,类空间:

using System.IO.Ports;

 

C# 串行端口 接收数据,代码如下:

复制代码
            //遍历串行端口名称数组
            foreach (string port in System.IO.Ports.SerialPort.GetPortNames())
            {
                Console.WriteLine(port);
            }

            byte[] b = new byte[32];
            SerialPort sp = new SerialPort("COM4");

            while (true)
            {
                //打开新的串行端口连接
                sp.Open();
                //丢弃来自串行驱动程序的接受缓冲区的数据
                sp.DiscardInBuffer();
                //丢弃来自串行驱动程序的传输缓冲区的数据
                sp.DiscardOutBuffer();
                //从串口输入缓冲区读取一些字节并将那些字节写入字节数组中指定的偏移量处
                sp.Read(b, 0, b.Length);

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < b.Length;i++ )
                {
                    sb.Append(Convert.ToString(b[i]) + " ");
                }
                
                Console.WriteLine(sb.ToString());
                Console.WriteLine(b.Length.ToString());
                //关闭端口连接
                sp.Close();
                //当前线程挂起500毫秒
                System.Threading.Thread.Sleep(500);
            }
复制代码

C# 串行端口 发送数据,代码如下:

复制代码
            SerialPort sp = new SerialPort("COM2");
            byte[] m = new byte[5];
            int i = 0;

            while (true)
            {
                //打开新的串行端口连接
                sp.Open();
                //丢弃来自串行驱动程序的接受缓冲区的数据
                sp.DiscardInBuffer();
                //丢弃来自串行驱动程序的传输缓冲区的数据
                sp.DiscardOutBuffer();

                m[0] = Convert.ToByte("1");
                m[1] = Convert.ToByte("2");
                m[2] = Convert.ToByte("3");
                m[3] = Convert.ToByte("4");
                m[4] = Convert.ToByte("5");

                //使用缓冲区的数据将指定数量的字节写入串行端口
                sp.Write(m, 0, m.Length);
                //关闭端口连接
                sp.Close();

                Console.WriteLine(i.ToString());
                i = i + 1;
                //当前线程挂起500毫秒
                System.Threading.Thread.Sleep(500);
            }
public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1 = new SerialPort(); string InputData = String.Empty; delegate void SetTextCallback(string text); private void Port_Select() {//获取机器中的串口地址 string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { comboBox1.Items.Add(port); } } private void Form1_Load_1(object sender, EventArgs e) { Port_Select(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "关闭串口") //当要关闭串口的时候 { port1.DiscardOutBuffer(); port1.DiscardInBuffer(); port1.Close(); button1.Text = "打开串口"; label3.Text = "串口当前状况:未打开"; comboBox1.Enabled = true; comboBox2.Enabled = true; } else if (button1.Text == "打开串口") //当要打开串口的时候 { try { port1.PortName = comboBox1.SelectedItem.ToString(); port1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem); port1.DataBits = 8; port1.RtsEnable = true; port1.Open(); port1.DiscardOutBuffer(); port1.DiscardInBuffer(); button1.Text = "关闭串口"; comboBox1.Enabled = false; comboBox2.Enabled = false; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + " 波特率:" + comboBox2.SelectedItem.ToString() + " 数据位:8 "; } catch { button1.Text = "打开串口"; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + "打开失败"; MessageBox.Show("该串口无法打开"); } } } 资源中部分代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值