在 C# 中创建并保持流活动
我正在使用漂亮的 nmeasharp 项目来破译我收到的 NMEA 流C# 中的串行端口。开箱即用一切正常,但我想将数据镜像到 IP 地址,但我遇到了困难。
nmeasharp 包从任何流中获取数据,因此我过去将其连接到串行端口流,效果很好:
_nmeaParser.Source = serialport.BaseStream;
现在,我想使用串行端口事件来触发我自己的例程,我可以在其中重定向数据,所以我删除上面的分配,并设置:
_serialport.DataReceived += new serialDataReceivedEventHandler(HandleNewSerialPortData);
触发此事件,并调用该方法。此时一切都很好,但 nmeasharp 代码仍在寻找要侦听的流,因为我还没有将其分配给流。
下面的方法是我需要设置一个到 nmeasharp 的流,并将串口刚刚接收到的任何新数据写入该流的地方。
private void HandleNewSerialPortData(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
this.serialDataAvailable(indata); // raise event that writes string to IP
if (_nmeaParser.Source == null) _nmeaParser.Source = new MemoryStream(840);
if (_nmeaParser.Source.CanWrite) _nmeaParser.Source.Write(ASCIIEncoding.UTF8.GetBytes(indata), 0, indata.Length);
// Unsuccessful attempts
// MemoryStream s = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(indata));
// s.CopyTo(_nmeaParser.Source);
// sp.BaseStream.CopyTo(this.NmeaDataStreamFromSerialPort);
}
我尝试了几种尝试写入 nmeasharp 流的变体,但都不起作用。一个有希望的方法是每次都初始化一个新的流,但这意味着该流在每个 DataReceived 事件后关闭,这会截断并错过串行消息。 (不成功的)代码是:
_nmeaParser.Source = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(indata));
我已经阅读了很多教程,阅读了我能找到的所有 msdn 文档,但仍然无法让这个简单的事情发挥作用。这必须很容易,对吧......?
编辑:如果可能的话,我想保留 nmeasharp 代码库存,因为它工作正常,并且由于串行数据并不总是 ASCII,所以希望保留它的二进制(流)而不是发送它数据作为字符串。我可以稍后修复二进制文件的 IP 重定向。
谢谢。
I'm using the pretty nifty nmeasharp project to decipher an NMEA stream I'm receiving on a serial port in c#. It all works fine out of the box, but I want to mirror the data to an IP address, and am getting stuck.
The nmeasharp package gets it's data from any stream, so I used to hook it up to the serial port stream, which worked fine:
_nmeaParser.Source = serialport.BaseStream;
Now, I want to use the serial port event to trigger my own routine, where I can redirect the data, so i remove the assignment above, and set:
_serialport.DataReceived += new serialDataReceivedEventHandler(HandleNewSerialPortData);
This event is triggered, and the method gets called. All good at this point, but the nmeasharp code is still looking for a stream to listen to, since I haven't assigned it to a stream anymore.
The following method is where I need to set up a stream to nmeasharp, and write whatever new data the serial port has just received out to that stream.
private void HandleNewSerialPortData(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
this.serialDataAvailable(indata); // raise event that writes string to IP
if (_nmeaParser.Source == null) _nmeaParser.Source = new MemoryStream(840);
if (_nmeaParser.Source.CanWrite) _nmeaParser.Source.Write(ASCIIEncoding.UTF8.GetBytes(indata), 0, indata.Length);
// Unsuccessful attempts
// MemoryStream s = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(indata));
// s.CopyTo(_nmeaParser.Source);
// sp.BaseStream.CopyTo(this.NmeaDataStreamFromSerialPort);
}
I've tried several variations of trying to write to the nmeasharp stream, but none work. One that showed promise was initialising a new stream every time, but that meant that the stream was closed after every DataReceived
event, which truncated and missed out serial messages. The (unsuccessful) code was:
_nmeaParser.Source = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(indata));
I've read lots of tutorials, read all the msdn documentation I could find, and still can't get this simple thing working. This has to be easy, right...?
Edit: I would like to keep the nmeasharp code stock if possible, as it works fine, and as the serial data isn't always ASCII, would like to keep it binary (streams) rather than sending it the data as a string. I can fix up the IP redirection for binary later.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试创建两个流。手动从串行流中读取,然后复制到两个流。
将 nmeaParser 设置为使用其中之一,并让 IP 处理程序从第二个读取。
您可以在此处查看,了解有关如何进行的良好解决方案复制流。
I would try to create two streams. Read from the serial stream manually, and copy to both streams.
Set the nmeaParser to use one of them, and have the IP handler read from the second one.
You can look here for a good solution on how to copy streams.