Quantcast
Channel: GPS – Spatial Horizons
Viewing all articles
Browse latest Browse all 5

Create Your Own GPS Applications Using C# (5)

$
0
0

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
  {
    _port.Read(bData, 0, 256);
    _protocol.ParseBuffer(bData);
    _validData = true;
    return true;
  }
  catch
  {
    _validData = false;
    return false;
  }
}

 

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.

 

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;
  }
}

 

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.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images