编写串口软件
*在跟着别人写了一遍过后,发现串口软件会把原本一个整体的内容当成两次内容,导致显示出来的时候会发现内容会在中间出现换行*
*通过尝试,在DataReceived事件触发后,不要立即从缓冲区读取发来的内容,设置Sleep等待50ms。然后就可解决问题*
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8fce294db9707e624170b6602c09bb97.png)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace PortHelper
{
public partial class Form1:Form
{
private SerialPort SerialPort = new SerialPort();
public Form1()
{
InitializeComponent();
InitConfig();
}
private void InitConfig()
{
cbSerial.Items.AddRange(SerialPort.GetPortNames());
if(cbSerial.Items.Count>0)
{
cbSerial.SelectedIndex = 0;
}
else
{
cbSerial.Text = "未检测到串口";
}
string[] baudData = new String[] {
"9600", "19200", "38400","57600", "115200","Custom"};
cbBaudRate.Items.AddRange(baudData);
cbBaudRate.SelectedIndex = 0