Page 1
Page 2
Dot Net programming example: Sharing a GPS by using GpsGate
This example shows briefly how to open access to the GPS via GpsGate in a Dot Net Form and read position data from the device, as well as using GpsGate to act as a data source to other applications. This code example is for Windows Mobile devices, but GpsGate works well on any PC running Windows.
The easiest way to start developing using the GpsGate API is to download the GpsGate SDK from Franson Technology (http://www.franson.biz/gpsgate/dev_guide.asp?platform=ppc). There you will find code examples for both Windows and Windows Mobile, in C# .Net, VB/eVB, and VC++/cVC++.
First, what you need to do in order to make the GpsGate API accessible to your .Net Compact Framework application is to reference the GateApiNET.dll found in the GpsGate SDK. All functions in the Dot Net GpsGate API are then available to programmers through an on object called Simple.
From within a form, all you have to do is to set up a variable for the GateApi object and create a new Simple object:
private GateApiNET.Simple m_simple = null;
m_simple = new GateApiNET.Simple();
Whenever GpsGate receives data from the GPS, it will generate an OnRead event, and we set our form to handle it:
m_simple.OnRead += new GateApiNET.OnRead(Form1_OnRead);
…and the button to open the GPS:
this.bOpen.Text = "Open the GPS";
this.bOpen.Click += new System.EventHandler(this.bOpen_Click);
this.Controls.Add(this.bOpen);
…and a label for displaying the read position data:
this.txtRead.Text = "";
this.Controls.Add(this.txtRead);
The event handler for the “open”-button uses StartGpsGate() to make sure GpsGate is up and running, and specifies that we want to read the output channel of GpsGate. There is no need to specify a particular COM port or to have any other knowledge of the GPS:
private void bOpen_Click(object sender, System.EventArgs e)
{
try
{
m_simple.StartGpsGate();
m_simple.Open("_output");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Finally, we add the event handler that will be called each time new data is available from the GPS:
private void Form1_OnRead(string Data)
{
if(Data == null)
{
// Connection is down. GpsGate was probably closed
MessageBox.Show("Connection down");
m_simple.Close();
}
else
{
txtRead.Text = Data;
}
}
Your device should by now be connected to the application, and NMEA strings will appear in the text box as soon as the GPS gets a fix.
Adding more functionality: acting as a position source
GpsGate also provides a feature to act as a source of GPS data to other applications – basically your application can simulate an GPS.
In that case we would change the channel above into “_input” and add an event for writing, to send back an NMEA sentence into GpsGate and to all listening applications:
private void bWrite_Click(object sender, System.EventArgs e)
{
try
{
m_simple.Write("$GPRMC,093339,A,5917.40531,N,01806.38798, E,10.0,147.2,040607,0.0,E*4A\r\n");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Higher-level GPS tools
In addition to offering a clean programmable application interface for accessing and sharing a GPS, users can benefit from the same features by setting up GpsGate manually. Something very handy during both debugging and demonstrations is the ability to log and retransmit NMEA sentences to other applications. You can also define a set of waypoints and let GpsGate simulate travel between those.
For readers that have not already developed a GPS application ready to be shared, Franson Technology also provides several other tools for rapid GPS application development. Most notably GpsTools is a program that includes support for reading GPS data, converting between different coordinate systems and plotting to raster maps. It also integrates directly with GpsGate, making it a low-threshold introduction to GPS programming, with plenty of easy-to-follow examples in VB, .Net and C++.
By using tools such as these, you should be up and running with your GPS application development within a couple of hours, while still allowing for your users to mix and match between their favourite GPS applications!
For further case studies see http://franson.biz/gpsgate/case.asp.
Previous Page