我需要一个串口程序来读取以4800
波特进入的数据。现在我有一个模拟器每秒发送15行数据。它的输出似乎“后面”,并跟不上数据进入的速度/数量。在.net中读取串行端口的最快方法是什么?
我尝试使用ReadLine()
与DataReceieved
事件,这似乎并不可靠,现在我使用的是异步方法与serialPort.BaseStream.ReadAsync
:
okToReadPort = true;
Task readTask = new Task(startAsyncRead);
readTask.Start();
//this method starts the async read process and the "nmeaList" is what
// is used by the other thread to display data
public async void startAsyncRead()
{
while (okToReadPort)
{
Task<string> task = ReadLineAsync(serialPort);
string line = await task;
NMEAMsg tempMsg = new NMEAMsg(line);
if (tempMsg.sentenceType != null)
{
nmeaList[tempMsg.sentenceType] = tempMsg;
}
}
public static async Task<string> ReadLineAsync(
this SerialPort serialPort)
{
// Console.WriteLine("Entering ReadLineAsync()...");
byte[] buffer = new byte[1];
string ret = string.Empty;
while (true)
{
await serialPort.BaseStream.ReadAsync(buffer, 0, 1);
ret += serialPort.Encoding.GetString(buffer);
if (ret.EndsWith(serialPort.NewLine))
return ret.Substring(0, ret.Length - serialPort.NewLine.Length);
}
}
这似乎仍然是低效的,没有人知道一个更好的方式,以确保每一块数据从端口读取,占?
您是否尝试过'ReadExisting'并解析传入的数据?如果你每秒读一遍,你应该有足够的时间 –
一次读一个字节的串口似乎很奇怪。下面是一个可靠地(根据作者)读取串行端口的方法的快速网络示例。 http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport 通过谷歌搜索发现它。 – PhillipH
@PhillipH:我的博客文章没有介绍如何设置缓冲区大小,因为它对设备和协议非常特殊。通常我会设置一个字符间超时,但.NET并不那么容易。而杰里通过切换到'port.BaseStream.ReadAsync'已经接受了我的建议。 –