<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spatial Horizons &#187; GPS</title>
	<atom:link href="http://spatialhorizons.com/category/gps/feed/" rel="self" type="application/rss+xml" />
	<link>http://spatialhorizons.com</link>
	<description>Exploring Geographic Technologies with GIS, GPS, C#, XNA, and Open Source Tools</description>
	<lastBuildDate>Thu, 18 Sep 2008 19:00:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create Your Own GPS Applications Using C# (5)</title>
		<link>http://spatialhorizons.com/2007/02/04/create-your-own-gps-applications-using-c-5/</link>
		<comments>http://spatialhorizons.com/2007/02/04/create-your-own-gps-applications-using-c-5/#comments</comments>
		<pubDate>Sun, 04 Feb 2007 03:51:33 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GPS]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/02/04/create-your-own-gps-applications-using-c-5/</guid>
		<description><![CDATA[Continuing with our GPS program, we now need to add a method to read from the GPS device. This step will grab the NMEA information, parse it, and store it in the classes provided in from the Coding 4 Fun project.

public bool ReadData()
{
  byte[] bData = new byte[256];
  try
  {
   [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing with our GPS program, we now need to add a method to read from the GPS device. This step will grab the NMEA information, parse it, and store it in the classes provided in from the Coding 4 Fun project.</p>
<p>
<pre lang="csharp">public bool ReadData()
{
  byte[] bData = new byte[256];
  try
  {
    _port.Read(bData, 0, 256);
    _protocol.ParseBuffer(bData);
    _validData = true;
    return true;
  }
  catch
  {
    _validData = false;
    return false;
  }
}</pre>
</p>
<p><span id="more-16"></span></p>
<p>Finally, we need a method to actually return the latitude and longitude from the stored class. If some part of connection process failed, a fake location (-999, -999) is used instead.</p>
<p>
<pre lang="csharp">public Location GetLocation()
{
  Location gpsLocation = new Location();
  if (_validData)
  {
    gpsLocation.Longitude = _protocol.GPGGA.Longitude;
    gpsLocation.Latitude = _protocol.GPGGA.Latitude;
    gpsLocation.Altitude = _protocol.GPGGA.Altitude;
    _validData = false;
    return gpsLocation;
  }
  else
  {
    gpsLocation.Longitude = -999;
    gpsLocation.Latitude = -999;
    gpsLocation.Altitude = -999;
    return gpsLocation;
  }
}</pre>
</p>
<p>This the bare minimum needed for a working solution. Additional enhancements could be added later on but for now just go ahead and compile the program. The resulting DLL can now be used in other programs and we will demonstrate that in a future post.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/02/04/create-your-own-gps-applications-using-c-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create Your Own GPS Applications Using C# (4)</title>
		<link>http://spatialhorizons.com/2007/01/29/create-your-own-gps-applications-using-c-4/</link>
		<comments>http://spatialhorizons.com/2007/01/29/create-your-own-gps-applications-using-c-4/#comments</comments>
		<pubDate>Mon, 29 Jan 2007 01:05:06 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GPS]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/01/29/create-your-own-gps-applications-using-c-4/</guid>
		<description><![CDATA[Using the information in the previous 3 posts, we can now build our own GPS solution based on the Coding 4 Fun project. Our objective is to create a DLL that can be used in future projects that need to read from GPS devices. The DLL will serve as the interface between the program and [...]]]></description>
			<content:encoded><![CDATA[<p>Using the information in the previous 3 posts, we can now build our own GPS solution based on the <a target="_blank" href="http://blogs.msdn.com/coding4fun/archive/2006/10/31/912287.aspx">Coding 4 Fun</a> project. Our objective is to create a DLL that can be used in future projects that need to read from GPS devices. The DLL will serve as the interface between the program and a GPS device.<br />
<span id="more-15"></span><br />
<span style="font-weight: bold">Project Setup</span></p>
<p>First, open Visual C# Express Edition and create a new class library project.</p>
<p class="moreinfo">A class library will compile to a DLL and can be referenced by other projects later on. This is unlike a standard Windows application that produces an executable (EXE) file.</p>
<p>In the Solution Explorer, rename the Class1.cs to something more appropriate. Open the renamed class and change the namespace also.</p>
<p>At the top of code listing, add this line:</p>
<p>
<pre lang="csharp">using System.IO.Ports;</pre>
</p>
<p>Then add into this project the 4 C# code files from the Coding 4 Fun project &#8211; GPSStructs.cs, Location.cs, NMEAProtocol.cs, and Satellite.cs. Remember, we modified the NMEAProtocol.cs file in our <a href="http://spatialhorizons.com/2007/01/21/create-your-own-gps-applications-using-c-3/">previous post</a> so be sure to read that first.</p>
<p><span style="font-weight: bold">Coding</span></p>
<p>Inside our class, we need to add three member variables:</p>
<p>
<pre lang="csharp">private NMEAProtocol _protocol;
private SerialPort _port;
private bool _validData;</pre>
</p>
<p>These variables are initially defined in the constructor:</p>
<p>
<pre lang="csharp">public myGPS()
{
  _protocol = new NMEAProtocol();
  _port = new SerialPort();
  _validData = false;
}</pre>
</p>
<p>Next, we need a method to connect to the GPS device based on a set of parameters. For this project, we will assume the necessary information on port name and baud rate are already known and will be passed in when the function is called:</p>
<p>
<pre lang="csharp">public bool Connect(string portName, int baudRate)
{
  try
  {
    _port.PortName = portName; // "COM1", "COM2", etc.
    _port.Parity = Parity.None;
    _port.BaudRate = baudRate; // 4800, 9600, etc.
    _port.StopBits = StopBits.One;
    _port.DataBits = 8;
    _port.Open();
    return true;
  }
  catch
  {
    return false;
  }
}</pre>
</p>
<p>Conversely, we will need a function to disconnect from the device:</p>
<p>
<pre lang="csharp">public void Disconnect()
{
  if (_port.IsOpen) _port.Close();
}</pre>
</p>
<p>To be continued&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/01/29/create-your-own-gps-applications-using-c-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create Your Own GPS Applications Using C# (3)</title>
		<link>http://spatialhorizons.com/2007/01/21/create-your-own-gps-applications-using-c-3/</link>
		<comments>http://spatialhorizons.com/2007/01/21/create-your-own-gps-applications-using-c-3/#comments</comments>
		<pubDate>Sun, 21 Jan 2007 15:53:55 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GPS]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/01/21/create-your-own-gps-applications-using-c-3/</guid>
		<description><![CDATA[As I mentioned before, Microsoftâ€™s Coding 4 Fun GPS project contains a math error when calculating latitude and longitude.  Iâ€™ll first provide a brief review of the NMEA protocol to better understand the error and how to correct it.
GPS receivers communicate using the NMEA protocol.  Another device, such as a computer, receives the [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned before, <a target="_blank" href="http://blogs.msdn.com/coding4fun/archive/2006/10/31/912287.aspx">Microsoftâ€™s Coding 4 Fun GPS project</a> contains a math error when calculating latitude and longitude.  Iâ€™ll first provide a brief review of the NMEA protocol to better understand the error and how to correct it.</p>
<p>GPS receivers communicate using the NMEA protocol.  Another device, such as a computer, receives the data in the form of NMEA sentences.  There are several different types of NMEA sentences that allow for information on location (GPGGA), accuracy (GPGSA), satellite reception (GPGSV), etc. to be transmitted.<br />
<span id="more-14"></span></p>
<p>Here is an example of a NMEA sentence:</p>
<p>
<pre lang="csharp">$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M, ,*47</pre>
</p>
<p>The sentence type is indicated first ($GPGGA) so we know that this sentence is communicating the current position of the GPS device.  To determine the location, we must look at the following portion of the sentence:</p>
<p>
<pre lang="csharp">4807.038,N,01131.000,E</pre>
</p>
<p>The first value (4807.038) represents the latitude(Y) while the N represents north (of the equator).  To find the actual latitude, the value must be separated after 2 digits to get 48 degrees and 7.038 minutes.  Likewise, the longitude(X) value translates to 11 degrees and 31.000 minutes east.</p>
<p>It is important to note that GPGGA sentences communicate the location in <strong>degrees and decimal minutes</strong>.  If we wanted to convert this to <strong>decimal degrees</strong>, the minutes value would have to be divided by 60 first since there are 60 minutes in a degree:</p>
<p class="moreinfo">Decimal degrees = 48 + (7.038 / 60) = 48 + 0.1173 = 48.1173</p>
<p>Conversely, at first glance one might think to just move the decimal place over 2 positions to get decimal degrees (48.07038) but as we already know, this is incorrect.  Any mapping programs using this formula will show your location offset in both latitude and longitude.</p>
<p>Now letâ€™s take a look at the Coding 4 Fun formula.  In the NMEAProtocol.cs file, find the method named ProcessGPGGA and see how latitude is calculated:</p>
<p>
<pre lang="csharp">GPGGA.Latitude = Convert.ToDouble(fields[1])/100;</pre>
</p>
<p>The variable fields[1] contains the entire latitude value (i.e. 4807.038) from the NMEA sentence.  But instead of first converting the minutes portion to decimal degrees, the code just divides everything by 100.  This produces an incorrect latitude as I discussed above.  The process is also repeated for longitude so the codeâ€™s location is different from the location in the GPS.</p>
<p>My adjusted code looks like this:</p>
<p>
<pre lang="csharp">double degrees = Convert.ToDouble(fields[1].Substring(0, 2));
double minutes = Convert.ToDouble(fields[1].Substring(2, 7));
GPGGA.Latitude = Math.Round(degrees + (minutes / 60.0), 6);</pre>
</p>
<p>I first separate out degrees and minutes and then correctly combine them to produce decimal degrees.  I repeated the process for the longitude calculation.</p>
<p>In reality, the math error only produces a small offset that might not even be noticeable unless you were looking at a finer-scaled map.  I only discovered the error after using the original code in a mapping application that contained high-resolution aerial photography.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/01/21/create-your-own-gps-applications-using-c-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create Your Own GPS Applications Using C# (2)</title>
		<link>http://spatialhorizons.com/2007/01/11/create-your-own-gps-applications-using-c-2/</link>
		<comments>http://spatialhorizons.com/2007/01/11/create-your-own-gps-applications-using-c-2/#comments</comments>
		<pubDate>Thu, 11 Jan 2007 01:12:35 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GPS]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/01/11/create-your-own-gps-applications-using-c-2/</guid>
		<description><![CDATA[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# [...]]]></description>
			<content:encoded><![CDATA[<p class="moreinfo">Before reading, be sure to review <a href="http://spatialhorizons.com/2006/12/18/create-your-own-gps-application-using-c-1/">part 1</a> and <a target="_blank" href="http://blogs.msdn.com/coding4fun/archive/2006/10/31/912287.aspx">download the Coding 4 Fun GPS Program</a>.</p>
<p>After opening the GPS program in <a target="_blank" href="http://msdn.microsoft.com/vstudio/express/visualcsharp/">Visual C# Express Edition</a>, 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.</p>
<p><span id="more-12"></span></p>
<div style="text-align: center"><img width="280" height="310" alt="C#_GPS_Solutions" id="image13" src="http://spatialhorizons.com/wp-content/uploads/2007/01/gps_c__solutions.jpg" /></div>
<p>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.</p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><strong>GPSStructs.cs</strong></p>
<p>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.</p>
<p><strong>Location.cs</strong></p>
<p>A very simple class to store latitude, longitude, and altitude.</p>
<p><strong>Satellite.cs</strong></p>
<p>This file contains two classes for storing data about the location of the GPS satellites.</p>
<p class="moreinfo">For more information on GPS satellites, see <a target="_blank" href="http://en.wikipedia.org/wiki/GPS">http://en.wikipedia.org/wiki/GPS.</a></p>
<p class="MsoNormal"><strong>NMEAProtocol.cs</strong></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--></p>
<p class="MsoNormal">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â€).</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--></p>
<p class="moreinfo">To learn about the basics of NMEA, see <a target="_blank" href="http://en.wikipedia.org/wiki/NMEA">http://en.wikipedia.org/wiki/NMEA</a></p>
<p class="moreinfo">For a detailed description of NMEA data transmissions, see <a target="_blank" href="http://www.gpsinformation.org/dale/nmea.htm">http://www.gpsinformation.org/dale/nmea.htm</a></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--></p>
<p class="MsoNormal">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:</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p>
<pre lang="csharp">$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M, ,*47</pre>
</p>
<p class="MsoNormal">(see example from <a target="_blank" href="http://www.gpsinformation.org/dale/nmea.htm">http://www.gpsinformation.org/dale/nmea.htm</a>)</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Fortunately, we donâ€™t have to worry about any of the NMEA sentence translation!  In the <strong>NMEAProtocol</strong> class, look for the <strong>ProcessGPGGA</strong> method.  This is where all the parsing, translation, and math to generate latitude, longitude, altitude, etc. are performed automatically.</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Next time, Iâ€™ll go over the math error in some detail and then we will begin building our own GPS solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/01/11/create-your-own-gps-applications-using-c-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create Your Own GPS Application Using C# (1)</title>
		<link>http://spatialhorizons.com/2006/12/18/create-your-own-gps-application-using-c-1/</link>
		<comments>http://spatialhorizons.com/2006/12/18/create-your-own-gps-application-using-c-1/#comments</comments>
		<pubDate>Mon, 18 Dec 2006 01:08:36 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GPS]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2006/12/18/create-your-own-gps-application-using-c-1/</guid>
		<description><![CDATA[Have you ever wanted to connect to a GPS device within your own custom application?  In the past, this was a difficult task for the average programmer using languages like C++.  Fortunately, Microsoftâ€™s .NET framework along with the C# programming language has made this task much easier.

Recently I came across a tutorial on [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to connect to a GPS device within your own custom application?  In the past, this was a difficult task for the average programmer using languages like C++.  Fortunately, Microsoftâ€™s .NET framework along with the C# programming language has made this task much easier.</p>
<p class="MsoNormal"><span id="more-9"></span></p>
<p class="MsoNormal">Recently I came across a <a target="_blank" href="http://blogs.msdn.com/coding4fun/archive/2006/10/31/912287.aspx">tutorial</a> on Microsoftâ€™s <a target="_blank" href="http://msdn.microsoft.com/coding4fun/">Coding for Fun</a> site that showed how to connect and read data from a GPS device.  I encourage you to read the article, download the source code, and experiment with their solution.  It works great although I did come across a math error in their code that I will discuss in a future post.</p>
<p class="MsoNormal">In order to test the application, you should connect a GPS device to one of your USB ports.  Any GPS-enabled device that can export data in the NMEA format will work.  Older GPS devices that use a serial port connection will need a Serial to USB adapter.  For example, I have a <a target="_blank" href="http://www.garmin.com/products/etrexVista/">Garmin eTrex Vista</a> along with a Serial to USB adapter that works fine.  Recently though I purchased a <a target="_blank" href="http://www.pharosgps.com/news/press/20060309.htm">Pharos iGPS-500 receiver</a> and that functions even better.  Since it is powered through the computer, you can leave it run for hours while testing the application and not waste batteries.</p>
<p class="MsoNormal">The application should compile error free and display a form with four tabs.  First chose which COM port your GPS device is connected to under the Set Up tab.  If you are unsure, right-click on My Computer and chose Manage.  This brings up the Computer Management menu where you can select the Device Manager and navigate to the Ports item on the right-hand list.  Here your GPS device will be represented by whatever connector you are using (note the COM port in the parenthesis).</p>
<p class="MsoNormal"><img width="332" height="41" id="image10" alt="Port_Finder" src="http://spatialhorizons.com/wp-content/uploads/2006/12/com_port_finder.jpg" /></p>
<p class="MsoNormal">Press the Connect button and wait several seconds.  If everything is working right, your current location should be displayed in the Summary tab.  To see the actual NMEA data, look at the Raw NMEA Data tab.</p>
<p class="MsoNormal"><img width="211" height="150" id="image11" alt="GPSClient_Screenshot" src="http://spatialhorizons.com/wp-content/uploads/2006/12/gpsclient_screenshot.jpg" /></p>
<p class="MsoNormal">
<p class="MsoNormal">As I mentioned above, I believe there is a math error in their code so the latitude location is slightly off.  But for now we just want to make sure the overall setup is functioning properly.</p>
<p class="MsoNormal">Hopefully this introduction will show you how easy it is to read from a GPS device.  In the next post, we will breakdown their code, make some changes, and work on producing a DLL that can used in any project requiring GPS input.</p>
<p class="MsoNormal">
<p class="MsoNormal">
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2006/12/18/create-your-own-gps-application-using-c-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
