Sample Code for Reading?

Dec 9, 2013 at 7:47 PM

I am new to serial port programming in general. From the documentation, it seems as though you have solved a lot of the difficulties with Serial port programming.

I would like to write a .net 4.0 console program that reads continually from a serial port on my GPS unit. Do you have any samples that I can use to get me going.

Dec 9, 2013 at 7:59 PM
Edited Dec 9, 2013 at 7:59 PM
Hi, the code is designed to be relatively simple to use. Take head (there's been a few bugfixes since the last change), compile it using VS2012 (Desktop Express works well).

The best source of working code comes from the unit tests, SerialPortStreamTest.cs. A good test case is SerialPortStream_WriteReadLine_Multilines.

There you'll learn how to send data, receive data.

I assume you're talking about NMEA, so you'll have to read each individual line of text, then implement your parser on top. Ensure you set the encoding to something other than UTF8 (the default), e.g. iso-8859-15.

Marked as answer by jmcurl on 12/14/2014 at 3:50 AM
Dec 9, 2013 at 8:30 PM

Thanks for the rapid response. Yes I am trying to read NMEA from the serial port and I have an nmea parser library which today handles input by using a bufferedstream to read from a file. So I am expecting to be able to use SerialPortStream to hand this nmea parser the bufferedstream object it is expecting. Does that sound like a good plan or am I likely to encounter problems?

I am using VS2010 and I am not able to move to VS2012 at this time. Is that an issue?

I have been able to compile the DLL.

Because I am only reading and not that interested in writing to a serial port at this time I created my own test project within the SerialPortStream solution.

Thanks for the pointer to SerialPortStream_WriteReadLine_Multilines. I just adapted it for my test code and was able to see that it successfully connected to the serial port and read 2 lines! So now in order to test more fully with my GPS, should I put some kind of while loop to read continuously from the port or should I use an event?

Dec 9, 2013 at 8:35 PM
Hi, If you have an interface that expects a Stream, this shouldn't be an issue, as it also supports reading the bytes direct from the serial port. In this case, you don't need to worry about encoding/decoding when converting to chars. You might have to check the Timeout parameters, to ensure that no timeout occurs, so that the library for parsing NMEA simply blocks.

There are two things you can do about VS2010. You can create your own project and import the files, it's just that VS2010 and VS2012 project solution files are incompatible. I haven't tried to load the csproj file within a new project of VS2010. In any case, my library is targeting .NET 4.0, so this shouldn't be an issue. Otherwise, you can compile using VS2012 Express, and import the assembly directly into VS2010.

To get more data, just continuously read in a loop and decide when you want to stop. btw, do you have a link to the NMEA parser you're using?

Dec 9, 2013 at 8:41 PM
Edited Dec 10, 2013 at 1:34 PM
Again thanks for the quick feedback.

Here is the parser I am using at the moment:

No doubt you could adapt it to use SerialPortStream a lot faster than I. If you do, would you mind sharing more of your work?


OK, I have it going. Meaning that I have successfully opened the serial port and am reading a continuous feed of NMEA 'sentences' using a loop. You were right about the timeout. Initially it was set to 300ms. The NMEA feed is sending a new set of sentences about once per second, so I bumped the timeout up to 1200ms and now I am not seeing any timeout exceptions.

Thanks Again