前言
![在这里插入图片描述](https://img-blog.csdnimg.cn/1bd1ad42e01d449891d076045c407c96.png)
一、代码
public partial class Form1 : Form
{
SerialPort serialPort1 = new SerialPort();
ConcurrentQueue<byte> receiveBytesQueue = new ConcurrentQueue<byte>();
public Form1()
{
InitializeComponent();
serialPort1.DataReceived += SerialPort1_DataReceived;
FindPortNameList();//寻找串口列表
}
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//Thread.Sleep(1);//一般加一个延时,这样能让缓冲区多储存一些数据
int num = serialPort1.BytesToRead;//获取接收缓冲区中的字节数
if (num > 0)
{
byte[] bytes = new byte[num];
serialPort1.Read(bytes, 0, num);//将指定字节数数据存到数组中
DisplayData(bytes);//显示数据
}
}
/// <summary>
/// 寻找串口列表
/// </summary>
priv