如何从串行端口c#接收更多消息

问题描述 投票:0回答:1

我正在使用System.IO.Ports。我将消息发送到设备,并希望等待来自它的所有消息。

我正在尝试这样做:

message = Console.ReadLine();
_serialPort.Write(message);
Console.WriteLine(_serialPort.ReadExisting());

但是它仅返回第一行。我尝试使用port.BytesToRead,但得到的结果相同。当我使用ReadLine()时,它不会返回任何内容。

c# serial-port
1个回答
0
投票

ReadExisting返回确切时间可用的可用字节,在读取缓冲区之前要稍加延迟,解决方法是

message = Console.ReadLine();
_serialPort.Write(message);
Thread.Sleep(200);
Console.WriteLine(_serialPort.ReadExisting());
© www.soinside.com 2019 - 2024. All rights reserved.