This project is read-only.

Getting Started

Please not that the code here has been forked by Nerzhul and is available as a NuGet package here.

The GPS Library is a single .Net Assembly designed to provide a simple object based interface to both the Garmin and Magellan range of GPS devices. In addition the GPS Library will support NMEA devices.

In order to make use of the library, simply install the product and reference the appropriate assembly within a Visual Studio project. In order to use the source code simply add the project to your solution within Visual Studio and add a reference to the project.

If a full installation option is selected, the help documentation will be integrated into the Visual Studio help collection and will be visible in the contents as a separate section. This is the recomended way of accessing all documentation. However a simple set of examples are shown below.

The examples shown here demonstrate how to make use of the various methods properties and events to transfer data to and from the supported devices. For further details and example code, please see the help documentation.

Note: For simplicity, all examples use the default port settings i.e. USB for Garmin devices and Com1 for Magellan. Devices.

  • Retrieving Waypoints from a Garmin or Magellan GPS device
  • Retrieving Routes from a Garmin or Magellan GPS device
  • Retrieving Tracklogs from a Garmin or Magellan GPS device
  • Sending a single Waypoint to a Garmin or Magellan GPS device
  • Sending multiple Waypoints to a Garmin or Magellan GPS device
  • Sending a single Route to a Garmin or Magellan GPS device
  • Sending multiple Routes to a Garmin or Magellan GPS device
  • Sending a Tracklog to a Garmin or Magellan GPS device
  • Using the Progress Event
  • Receiving NMEA Data

Retrieving Waypoints from a Garmin or Magellan GPS device

This C# example shows how to download waypoints from a Magellan or Garmin GPS device. The example shows how the waypoints can be enumerated and it also shows how to assign the retrieved data to a DataGridView control. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

private void GetWaypoints(bool garmin)
{

DeviceBase gps;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();

//Retrieve the Waypoints
WaypointCollection wpts = gps.GetWaypoints();

//Now we have a collection of Waypoints
foreach (Waypoint wpt in wpts)
{
Console.WriteLine(wpt.Latitude);
Console.WriteLine(wpt.Longitude);
}

//Alternatively display the results in a DataGridView
dataGridView1.DataSource = wpts.ToDataSet();
dataGridView1.DataMember = "Waypoints";

}

 
Retrieving Routes from a Garmin or Magellan GPS device

This example shows how Routes can be retrieved from the GPS device. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

private void GetRoutes(bool garmin)
{
DeviceBase gps;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();

//Retrieve the Routes
RouteCollection rtes = gps.GetRoutes();
//Now we have a collection of Routes
foreach (Route rte in rtes)
{
Console.WriteLine(rte.Identifier);
Console.WriteLine(rte.Comment);

//Each Route has a collection of Waypoints
foreach (Waypoint wpt in rte.Waypoints)
{
Console.WriteLine(wpt.Latitude);
Console.WriteLine(wpt.Longitude);
}
}

//Alternatively display the results in a DataGridView
dataGridView1.DataSource = rtes.ToDataSet();
dataGridView1.DataMember = "Routes";
}

 
Retrieving Tracklogs from a Garmin or Magellan GPS device

This example shows how Tracklogs can be retrieved from the GPS device. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

private void GetTracklogs(bool garmin)
{
DeviceBase gps;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();

//Retrieve the Tracklogs
TracklogCollection trks = gps.GetTracklogs();

//Now we have a collection of Tracklogs
foreach (Tracklog trk in trks)
{
Console.WriteLine(trk.Identifier);

//Each Tracklog has a collection of Trackpoints
foreach (Trackpoint tpt in trk.Trackpoints)
{
Console.WriteLine(tpt.Latitude);
Console.WriteLine(tpt.Longitude);
}
}

//Alternatively display the results in a DataGridView
dataGridView1.DataSource = trks.ToDataSet();
dataGridView1.DataMember = "Tracks";

}


Sending a single Waypoint to a Garmin or Magellan GPS device

This example creates a very simple waypoint and sends it to the GPS device. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

private void SetWaypoint(bool garmin)
{
DeviceBase gps;
Waypoint newWpt = null;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();

//Create a Waypoint
newWpt = new Waypoint();
newWpt.Identifier = "Test01";
newWpt.Latitude = 53.9608166666667;
newWpt.Longitude = -2.02225;
newWpt.Comment = "Test 1";

//Transfer the Waypoint to the GPS device
gps.SetWaypoint(newWpt);
}

 
Sending multiple Waypoints to a Garmin or Magellan GPS device

This example creates a set of Waypoints and sends them to the GPS device. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

private void SetWaypoints(bool garmin)
{
DeviceBase gps;
WaypointCollection newWpts = new WaypointCollection();
Waypoint newWpt = null;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();

//Create some Waypoints

//Waypoint 1
newWpt = new Waypoint();
newWpt.Identifier = "Test01";
newWpt.Latitude = 53.9608166666667;
newWpt.Longitude = -2.02225;
newWpt.Comment = "Test 1";

//Add the new waypoint to a Waypoint collection
newWpts.Add(newWpt);

//Waypoint 2
newWpt = new Waypoint();
newWpt.Identifier = "Test02";
newWpt.Latitude = 190.0;
newWpt.Longitude = 1180.0;
newWpt.Comment = "Test 2";
//Add the new Waypoint to a Waypoint collection
newWpts.Add(newWpt);

//Transfer the Waypoints to the GPS device
gps.SetWaypoints(newWpts);
}


Sending a single Route to a Garmin or Magellan GPS device

This example creates a single Route and sends it to the GPS device. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

private void SetRoute(bool garmin)
{
DeviceBase gps;
Waypoint wpt;
Route rte;
WaypointCollection wpts;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();

//Create the collection of Waypoints for the Route
wpts = new WaypointCollection();

//Waypoint 1
wpt = new Waypoint();
wpt.Identifier = "RT01";
wpt.Latitude = 1.54;
wpt.Longitude = 0.2;

//Add the new Waypoint to the Waypoint Collection
wpts.Add(wpt);

//Waypoint 2
wpt = new Waypoint();
wpt.Identifier = "RT02";
wpt.Latitude = 1.53;
wpt.Longitude = 0.22;

//Add the new Waypoint to the Waypoint Collection
wpts.Add(wpt);

//Create and populate the Route
rte = new Route();
rte.Number = 2;
rte.Comment = "Hello Route";
rte.Waypoints = wpts;

//Transfer the Route to the GPS device
gps.SetRoute(rte);
}

 
Sending multiple Routes to a Garmin or Magellan GPS device

This example creates a set of Routes and sends them to the GPS device. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

private void SetRoutes(bool garmin)
{
DeviceBase gps;
Waypoint wpt;
Route rte;
RouteCollection rtes
WaypointCollection wpts;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();
//Create the collection of Waypoints for the Route
wpts = new WaypointCollection();

//Waypoint 1
wpt = new Waypoint();
wpt.Identifier = "RT01";
wpt.Latitude = 1.54;
wpt.Longitude = 0.2;

//Add the new Waypoint to the Waypoint Collection
wpts.Add(wpt);

//Waypoint 2
wpt = new Waypoint();
wpt.Identifier = "RT02";
wpt.Latitude = 1.53;
wpt.Longitude = 0.22;

//Add the new Waypoint to the Waypoint Collection
wpts.Add(wpt);
//Create and populate the Route
rte = new Route();
rte.Number = 2;
rte.Comment = "Hello Route";
rte.Waypoints = wpts;

//Create a routes collection
//Typically a single Route would be transferred using SetRoute
rtes = new RouteCollection();
rtes.Add(rte);

//Transfer the Routes to the GPS device
gps.SetRoutes(rtes);
}

 
Sending a Tracklog to a Garmin or Magellan GPS device

This example creates a Tracklog and sends it to the GPS device. The code uses the DeviceBase object in order that it can be used for both Magellan and Garmin devices. It is cast as appropriate depending on the value of a boolean value passed as a parameter in to the method.

public void SetTracklog(bool garmin)
{

DeviceBase gps;
Tracklog trk;
Trackpoint tpt;

//Create a new device object depending
//on the type of GPS device
if (garmin)
gps = new GarminDevice();
else
gps = new MagellanDevice();

//Create a new Tracklog object
trk = new Tracklog();
trk.Identifier = "Upload";

//Create the first Trackpoint
tpt = new Trackpoint();
tpt.Longitude = 146.42;
tpt.Latitude = -36.47;
tpt.NewTrack = true;
trk.Trackpoints.Add(tpt);

//Create a second Trackpoint
tpt = new Trackpoint();
tpt.Longitude = 146.43;
tpt.Latitude = -36.48;
trk.Trackpoints.Add(tpt);

//Create the third Trackpoint
tpt = new Trackpoint();
tpt.Longitude = 146.44;
tpt.Latitude = -36.49;
tpt.NewTrack = true;
trk.Trackpoints.Add(tpt);

//Create the fourth Trackpoint
tpt = new Trackpoint();
tpt.Longitude = 146.45;
tpt.Latitude = -36.5;
trk.Trackpoints.Add(tpt);

//Send the Tracklog to the GPS device
gps.SetTrackLog(trk);
}

Using the Progress Event

The progress event provides a place for client code to update a progress indicator etc. The event is fired during data transfers between the library and the GPS Unit. The value passed to this event by the library is in the range 0 to 100 representing the percentage complete. This example shows how the event can be used to update a progress bar (pbProgress) in an application.

// Register the event
gps.Progress +=new EventHandler<ProgressEventArgs>(gps_Progress);

private void gps_Progress(object sender, ProgressEventArgs e)
{
// Update a progress bar. This is OK as this event
// is raised on the thread that created the Device object.
pbProgress.Value = e.Progress;
}


Receiving NMEA Data

This example shows how the GPS Library can be used to received NMEA data from NMEA devices.

// Register the event
gps.ReceiveSentence += new EventHandler<ReceiveSentenceEventArgs>(gpsDevice_ReceiveSentence);

// Use a delegate as Windows Forms Controls are not thread safe
private delegate void DisplayNmeaDelegate(Sentence sentence);

// The Event is then raised by the assembly when data is received from the NMEA device.
private void gpsDevice_ReceiveSentence(object source, ReceiveSentenceEventArgs e)
{
// Call the Display function
DisplayNmea(e.Sentence);
}

private void DisplayNmea(Sentence sentence)
{

// Populate a label called lblSentence

// Do NOT do this, as we may not be on the UI thread.
// lblSentence.Text = sentence.ToString();

// Check if we need to call Invoke.
if (this.InvokeRequired)
{
// Pass the this function to Invoke,
// then the call will come in on the correct
// thread and InvokeRequired will be false.
this.BeginInvoke(new DisplayNmeaDelegate(DisplayNmea), new object[] { sentence });
return;
}

//ok now as we are on the UI thread so this is OK
lblSentence.Text = sentence.ToString();

// All sentence objects are derived from Waymex.Gps.NMEA.Sentence, to get to the
// specific properties of a derived type (e.g. SentenceRmc) simply
// cast the Sentence object to the approriate type as follows;
switch (sentence.TypeId)
{
case "RMC":

// RMC Sentence received so cast to the SentenceRmc type
SentenceRmc sentenceRmc = (SentenceRmc)sentence;

// process the properties as required
lblLatitude.Text = sentenceRmc.Latitude.ToString();
lblLongitude.Text = sentenceRmc.Longitude.ToString();

break;

case "RMB":

// RMB Sentence received so cast to the SentenceRmb type
SentenceRmb sentenceRmb = (SentenceRmb)sentence;

// process the properties as required
lblDestLatitude.Text = sentenceRmb.DestinationLatitude.ToString();

break;
}
}

Last edited Jun 11, 2015 at 10:45 PM by John_Newcombe, version 17

Comments

No comments yet.