串行端口数据写入最终进入接收缓冲区
我有一个包装类,它包装 .Net 的 SerialPort 类来执行调制解调器操作和数据流。当我从 SerialPort 对象读取数据时,我之前写入端口的数据可以在接收缓冲区中找到。我可以使用 SerialPort 对象更改这种行为吗?我还没有找到该类的属性来阻止我的传出数据最终进入接收缓冲区。
我使用 SerialPort.Write() 写入数据,这就是我读取数据的方法
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Task.Factory.Startnew(() => WriteToBuffer());
}
// ...
private void WriteToBuffer()
{
string data = string.Empty;
lock(_serialPort)
{
StringBuilder sb = new StringBuilder();
while(_serialPort.BytesToRead > 0)
{
sb.Append(_serialPort.ReadExisting());
}
data = sb.ToString();
}
// .. then do some additional processing
}
我以非阻塞方式读取数据,因为我宁愿偏执,也不要错过任何数据接收事件,并让客户端代码认为那里是某种类型的超时。
我可以通过发送 ATE0 来阻止 Hayes AT 命令复制到接收缓冲区,但在调制解调器连接到远程调制解调器后发送的任何数据仍然会进入接收缓冲区。
有什么我可以做的吗?很难仅检查并查看我最后写入缓冲区的内容并忽略它,因为有时我一次读取一个字节并且是返回数据的一部分。例如,如果我发送类似“ID 123”的内容,我可能会使用 while 循环读取“ID”,并发现有 0 个字节要读取,因此它会停在那里。我知道我可以每次调用 Thread.Sleep() 但我认为这不是一个好的做法。
我可以预期所有调制解调器都会出现这种回声行为吗?如果是这样,那么我可以添加代码来过滤我写入的数据。
I have a wrapper class that wraps .Net's SerialPort class to perform modem operations and data flow. When I'm reading from the SerialPort object, the data that I previously wrote to the port is found within the receive buffer. Is that a behavior that I can change with the SerialPort object? I haven't found a property for the class that prevents my outgoing data from ending up in the receive buffer.
I use SerialPort.Write() to write the data and this is what I do to read data
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Task.Factory.Startnew(() => WriteToBuffer());
}
// ...
private void WriteToBuffer()
{
string data = string.Empty;
lock(_serialPort)
{
StringBuilder sb = new StringBuilder();
while(_serialPort.BytesToRead > 0)
{
sb.Append(_serialPort.ReadExisting());
}
data = sb.ToString();
}
// .. then do some additional processing
}
I read the data in a non-blocking manner because I'd rather be paranoid and not miss any data received events and have client code think there was some type of timeout.
I can prevent Hayes AT commands from being copied into the receive buffer by sending ATE0
but any data I send after the modem is connected to a remote modem is still creeping into the receive buffer.
Is there anything I can do? It's hard to just check and see what I wrote last to the buffer and ignore it because sometimes I read one byte at a time and is part of the data returned. For instance if I send something like "ID 123", I might read "ID " with the while loop and find that there are 0 bytes to read so it stops there. I know that I could just call a Thread.Sleep() each time but I think that would not be good practice.
Can I expect this echoing behavior in ALL modems? If so, then I can add code to filter my written data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了一些其他调制解调器,并且输入缓冲区中返回的写入数据就像我尝试的原始调制解调器模型一样。我的解决方案是过滤掉写入的数据,以正确读取来自远程调制解调器的其他数据。
I tried out a handful of other modems and the write data returned in the input buffer just like the original modem model that I tried. My solution was to filter out the written data to correctly read other data coming from the remote modem.