private SerialPort LEDPt = new SerialPort();
if (!LEDPt.IsOpen)
{
LEDPt.PortName = SysConfig.comNum; //设置端口号
LEDPt.BaudRate = int.Parse(SysConfig.btl); //设置波特率
LEDPt.Open(); //打开串口
}
if (LEDPt.IsOpen)
{
LEDPt.Write(sendMsgData, 0, sendMsgData.Length); //发送数据
}
if (LEDPt != null)//发送并接受数据
{
if (LEDPt .IsOpen)
{
LEDPt .Write(info, 0, info.Length);
while (LEDPt .BytesToRead == 0)
{
Thread.Sleep(1);
}
Thread.Sleep(10);
Byte[] receivedData = new Byte[LEDPt .BytesToRead]; //创建接收字节数组
LEDPt .Read(receivedData, 0, receivedData.Length); //读取包含速度的数据
LEDPt .DiscardInBuffer();
}
}
打开串口发送、接收数据
最新推荐文章于 2024-09-17 10:02:37 发布