1

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.

Community
  • 1
  • 1
DefenestrationDay
  • 3,712
  • 2
  • 33
  • 61

1 Answers1

2

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.

Community
  • 1
  • 1
Amirshk
  • 8,170
  • 2
  • 35
  • 64
  • Thanks Am, Nice idea, but I'm not too concerned with copying streams at the moment - I can't even set up one stream as the input for nmeasharp. As soon as I'm able to set up a persistant stream that I can write to - sure, your idea seems elegant. – DefenestrationDay Jan 07 '11 at 01:27
  • OK, so i've now got a stream-splitter, that takes a stream and outputs it identically to two (or more) destination streams. works good. however, reading from one of them (as a memorystream) is a nightmare. what do i do the the stream.position? do i reset it to zero for every read? does that screw up the writing thread?.. – DefenestrationDay Jan 15 '11 at 12:23