{%Pragmatic Coding%} Tips and tricks for software developers.

30Nov/120

Working with GPS in WinCE using C#

In this article I want to share with You, my dear reader, the implementation of the GPS data access in devices based on WindowsCE.When I had to create a product Shturman Navigator, it was necessary to develop an application that runs in WinSE 5.0, and in WinCE 6.0, which is able to receive data from the NMEA navigation receiver, and record them to log.

To work with GPS in a WinCE 5.0, and 6.0 is easier to use to work with COM port. So, first of all you need to find on your device, which COM-port provides GPS data. For determining that you can use DeviceManager.

Often manufacturers of a firmware already made sure that COM port of GPS was two. This allows you to separate software that requires a GPS and navigation, so that they were not fighting for access to the COM-port. Suppose that the COM port will be used in exclusive access.
To get NMEA data (http://ru.wikipedia.org/wiki/NMEA_0183), we need just to open the COM port, read data from it, and then close the COM port.

Sample code:

Despite the fact that it looks very trivial, the code often does not work because of errors accessing COM-port.(For example, often there is an error: «UnauthorizedAccessException: Access to the port is denied»). But we will not be upset, there is another approach that works.
Searching via internet I have found wonderful project OpenNetCf that provides the source code of its own SerialPort. http://serial.codeplex.com/

Implementation

First, you need to add reference to OpenNetCf.IO.Serial assembly in to the project.

Than you can use following class for working with GPS via COM-port.

In SerialPortDataReceived method you need actually parsing NMEA strings.
You can do this by:

  1. Writing your own parser of NMEA;
  2. Using SharpGps ;
  3. Use the NMEA-0183-2-0-sentense-parser-builder
  4. Any other parser.

In the NMEA 0183 protocol ( http://www.tronico.fi/OH6NT/docs/NMEA0183.pdf ) checksum is described as a 2-digit decimal number and control XOR-sum of all bytes is  in the string between the "$" and "* ".

SharpGps has a function to validate the checksum in the package:

This function works great if the navigation receiver transmits the checksum in the form of two numbers (0x01, 0x02, etc.), as stated in the protocol. But any perfect code break on the reality in which navigation receivers transmit packets with checksum without adding a leading zero (0x1, 0x2).

In the running application, it turns out that part of the package is eliminated. In this case, the feeling that everything seems to be working. But the track, even though it is there, but very poor quality.

To make it work correctly, we can change the last row to this:

To store navigation data, you can use SqlServer Compact Edition. Its very easy to integrate into an application, and use it in development. Or you can just simply use data without saving it as I had to do in my application.

Thank you for your attention. Hope this article was helpful.

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Tagged as: .NET, C#, GPS, WinCE Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.