This tutorial is made of two parts.
The first part explains how to use the Serial library to create a connection with a GPS device and how to use the GPS library to retrieve the data.
The second part will explain how to convert coordinates from one datum to another and how to convert between Lat/Lon format and UTM format.
First we need to add a reference to the Serial and GPS libraries.
We will use the Serial2 library which requires .Net 2.0.
You can use SerialDesktop and SerialDevice instead but they are no longer supported by new devices and therefore are less recommended.
Create a new file and save it.
Without saving our file we won't be able to add any library as their is still no project folder.
Choose Tools - Components...
Choose Add Both (as we want to add the same libraries to both the desktop and the device) and add GPS.dll.
Add Serial2 in the same way.
Each library added, exposes one or more objects types that can be added to our program.
There are two ways to add objects; at runtime with AddObject and at design time using Tools - Add Object.
Normally we will use the second option.
Add a GPS object and name it GPS1 and a Serial object and name it Serial1.
Create a new form and add a Timer, a ListBox and one MenuItem named mnuConnect.
Now for the real part...
GPS devices whether external or internal send the data via a serial port.
The port could be COM1 to COM9.
See this thread if you want to find the available ports from the registry:
http://www.b4x.com/forum/showthread.php?t=314
The GPS continuously sends NMEA strings.
Using a Serial object and a Timer we check each second if data is waiting in the input buffer.
If there is data we read the data and pass it to a GPS object.
GPS1 collects the partial strings and when there is enough data it parses the data and raises a GPSDecoded event.
Inside the GPSDecoded event we can read the GPS data (lstData is a ListBox):
To make our application less error prone we've added a global variable named counter.
The GPS continuously sends data even when there is no satellites reception.
On each tick event (each second), if there is no data we increase counter by one.
If counter equals 5 (which means that no data was sent in the last five seconds), we assume that the connection is broken, we close the serial port and ask the user if he wants to reconnect.
Some notes:
* Most of the GPS object properties are directly parsed from the NMEA strings.
If there is no reception properties like SpeedOverGround, Latitude / Longitude and CourseOverGround will not be zero but rather be an empty string.
Which means that you need to check that the data is not empty before using it in a calculation:
* The attached code uses port 8. You should change the global variable 'port' to match the right port.
* When the user closes Form1 we check if the serial port is still open and close it if necessary.
* You will need to install .Net 2.0 to run the attached code (or remove Serial2 library and add SerialDesktop and SerialDevice).
The first part explains how to use the Serial library to create a connection with a GPS device and how to use the GPS library to retrieve the data.
The second part will explain how to convert coordinates from one datum to another and how to convert between Lat/Lon format and UTM format.
First we need to add a reference to the Serial and GPS libraries.
We will use the Serial2 library which requires .Net 2.0.
You can use SerialDesktop and SerialDevice instead but they are no longer supported by new devices and therefore are less recommended.
Create a new file and save it.
Without saving our file we won't be able to add any library as their is still no project folder.
Choose Tools - Components...
Choose Add Both (as we want to add the same libraries to both the desktop and the device) and add GPS.dll.
Add Serial2 in the same way.
Each library added, exposes one or more objects types that can be added to our program.
There are two ways to add objects; at runtime with AddObject and at design time using Tools - Add Object.
Normally we will use the second option.
Add a GPS object and name it GPS1 and a Serial object and name it Serial1.
Create a new form and add a Timer, a ListBox and one MenuItem named mnuConnect.
Now for the real part...
GPS devices whether external or internal send the data via a serial port.
The port could be COM1 to COM9.
See this thread if you want to find the available ports from the registry:
http://www.b4x.com/forum/showthread.php?t=314
The GPS continuously sends NMEA strings.
Using a Serial object and a Timer we check each second if data is waiting in the input buffer.
If there is data we read the data and pass it to a GPS object.
B4X:
Sub Timer1_Tick
If Serial1.InBufferCount > 0 Then
GPS1.GPSStream(Serial1.InputString)
End If
End Sub
Inside the GPSDecoded event we can read the GPS data (lstData is a ListBox):
B4X:
Sub GPS1_GPSDecoded
lstData.Clear
lstData.Add("Status: " & GPS1.Status)
lstData.Add("Number of satellites: " & GPS1.NumberOfSatellites)
lstData.Add("Time: " & GPS1.UTCTime)
lstdata.Add("Lat: " & GPS1.Latitude)
lstdata.Add("Lon: " & GPS1.Longitude)
If gps1.SpeedOverGround <> "" Then lstdata.Add("Speed: " & (GPS1.SpeedOverGround * 1.852))
lstdata.Add("Course: " & GPS1.CourseOverGround)
End Sub
To make our application less error prone we've added a global variable named counter.
The GPS continuously sends data even when there is no satellites reception.
On each tick event (each second), if there is no data we increase counter by one.
If counter equals 5 (which means that no data was sent in the last five seconds), we assume that the connection is broken, we close the serial port and ask the user if he wants to reconnect.
B4X:
Sub Timer1_Tick
If Serial1.InBufferCount > 0 Then
Label1.Text = "GPS is connected."
GPS1.GPSStream(Serial1.InputString)
counter = 0
Else
counter = counter + 1
If counter >= 5 Then 'After 5 seconds without any data we will try to make a new connection.
counter = 0
Timer1.Enabled = false
Serial1.PortOpen = false
Label1.Text = "No connection."
GPS1.GPSBuffer = "" 'Clear the yet unparsed data.
If Msgbox("GPS was disconnected." & crlf & "Do you want to reconnect?","",cMsgboxYesNo) = cYes Then
mnuConnect_Click 'Try to reconnect.
End If
End If
End If
End Sub
Some notes:
* Most of the GPS object properties are directly parsed from the NMEA strings.
If there is no reception properties like SpeedOverGround, Latitude / Longitude and CourseOverGround will not be zero but rather be an empty string.
Which means that you need to check that the data is not empty before using it in a calculation:
B4X:
If gps1.SpeedOverGround <> "" Then lstdata.Add("Speed: " & (GPS1.SpeedOverGround * 1.852)) 'Convert to KMH.
* When the user closes Form1 we check if the serial port is still open and close it if necessary.
* You will need to install .Net 2.0 to run the attached code (or remove Serial2 library and add SerialDesktop and SerialDevice).