Before reading, be sure to review part 1 and download the Coding 4 Fun GPS Program.
After opening the GPS program in Visual C# Express Edition, you will note that the Coding for Fun project is actually composed of two solutions named GPS and GPSClient. The GPS solution contains 4 files where all the C# code needed to connect and read from a GPS is stored. The second solution is named GPSClient and is just a GUI for displaying the information being transmitted from the GPS device.

Since we are focused on building a GPS solution that can be added to other mapping programs, we will work on building our client application similar to the GPSClient solution. The difference being our result will be a single Dynamically-Linked Library (DLL) that can easily be added to other programs when only GPS location information is needed.
Before creating this DLL though, we will need to understand how the GPS input process works. Let’s briefly discuss the importance of the 4 files in the GPS solution.
GPSStructs.cs
This contains enumerations, structures, and classes used in this application. Look over this code here to get a sense for what type of information will be received from the GPS device.
Location.cs
A very simple class to store latitude, longitude, and altitude.
Satellite.cs
This file contains two classes for storing data about the location of the GPS satellites.
For more information on GPS satellites, see http://en.wikipedia.org/wiki/GPS.
NMEAProtocol.cs
A more complicated class used to interpret the input streams from a GPS device. This class will parse NMEA data and store it into variables that can be easily accessed by the programmer.
I would strongly encourage anyone interested in using GPS to learn at least the basics of NMEA. NMEA is the standard protocol for exporting data from a GPS. Most GPS devices can export data in this format which is basically ASCII text strings in a comma-delimited format (“sentences”).
To learn about the basics of NMEA, see http://en.wikipedia.org/wiki/NMEA
For a detailed description of NMEA data transmissions, see http://www.gpsinformation.org/dale/nmea.htm
The easiest way to explain how this works is using an example. When GPS devices transmit NMEA sentences, each one needs to be interpreted differently. Our focus is on the GPS’s location so one sentence is the GPGGA type:
$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M, ,*47 |
(see example from http://www.gpsinformation.org/dale/nmea.htm)
NMEA protocol tells us that the latitude is 48 degrees 7.038 minutes north and longitude is 11 degrees 31.00 minutes east. It should be noted that the accuracy of a GPS location varies based on a number of factors.
Fortunately, we don’t have to worry about any of the NMEA sentence translation! In the NMEAProtocol class, look for the ProcessGPGGA method. This is where all the parsing, translation, and math to generate latitude, longitude, altitude, etc. are performed automatically.
Unfortunately, there is an error in the way the Coding 4 Fun solution calculates the longitude and latitude from a GPGGA sentence. Take a look and see if you can figure out what they are doing wrong. The only reason I noticed it is because I did some testing with a GPS device and it didn’t quite match where I was actually standing on planet Earth.
Next time, I’ll go over the math error in some detail and then we will begin building our own GPS solution.