Sample Code for Reading?

Dec 9, 2013 at 6:47 PM
Hi,

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.

TIA
Coordinator
Dec 9, 2013 at 6:59 PM
Edited Dec 9, 2013 at 6: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.

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

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?

TIA
Coordinator
Dec 9, 2013 at 7: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?

Regards,
Jason.
Dec 9, 2013 at 7:41 PM
Edited Dec 10, 2013 at 12:34 PM
Again thanks for the quick feedback.

Here is the parser I am using at the moment:

http://code.google.com/p/nmeasharp/

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?

TIA

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