Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Take Your Apps Far and Wide with a GPS Tracking System

You already know that GPS is used extensively in a wide variety of mobile devices in order to track delivery and service vehicles—or any other mobile fleet. This kind of application is not as difficult as you may at first think. Find out how to use Visual Studio to create a GPS tracking app, with maps, that runs on Windows Mobile Pocket PC devices.


advertisement
lobal Positioning System (GPS) is a world-wide navigational system that can tell you with pinpoint accuracy your exact current location. GPS has been around for many years and has many applications both in the commercial sector as well as in the military. The best part of GPS is that it is free to use—simply purchase a GPS receiver (prices range from $100 to $10,000+ for complex devices) and equip it with the necessary software and you will soon be using it to help you navigate unfamiliar territory.

While route navigation is one of the most popular uses of GPS, another good use is helping you track the whereabouts of your inventory, such as delivery trucks and goods. In this article, I will build a GPS tracking system that allows you to track the whereabouts of your delivery trucks, using a Windows Mobile 5.0 Pocket PC device and a Bluetooth-enabled GPS receiver.

The tracking system comprises:

  • A Windows Mobile 5.0 Pocket PC mounted in a truck; it is also connected to the Internet via GPRS (General Packet Radio Services)
  • A Bluetooth-enabled GPS receiver that is paired with the Pocket PC
  • A Pocket PC application that displays the latitude and longitude of its current position; it also shows the speed at which the truck is moving
  • A back end server hosted on a Web server that collects the positional data
  • A server-side application that receives the data and displays the map of the location the truck is currently at, thereby allowing real-time monitoring.
Figure 1 shows what the server application will look like. As you can see, it can monitor two trucks at the same time and it uses Microsoft Virtual Earth for mapping.




Figure 1. The server application you will build in this article monitors two trucks at the same time in side-by-side maps.
 
Figure 2. I chose the iMate JasJar as my Pocket PC device and the Holux GPSlim 236 as my GPS receiver.

There are two main components you will build in this article:
  • A Pocket PC application that retrieves and displays GPS data from the GPS receiver
  • A server that receives the positional data sent by the Pocket PC application and then displays the map of the corresponding location
What you need
To create this application you'll need:
  • A Windows Mobile 5.0 Pocket PC (Phone Edition) that supports Bluetooth connectivity; in addition, the device must be connected to the Internet via GPRS
  • A Bluetooth-enabled GPS receiver
Figure 2 shows the hardware I selected for my implementation: The iMate JasJar Pocket PC and the Holux GPSlim 236 GPS receiver.

You need to pair up the Windows Mobile device with the GPS receiver using Bluetooth. Once they are paired, remember to establish a serial connection between the two devices. For my device, I selected COM4 as the serial port.

Author's Note: Due to the different setup procedures for different devices, I will not attempt to show the steps to set up the serial port. Check the documentation of your device for more information.

Creating the Pocket PC Application
Using Visual Studio 2005, create a Windows Mobile 5.0 Pocket PC application and name it C:\GPSTracking (see Figure 3).

Author's Note: In order to select the Windows Mobile 5.0 Pocket PC item in the Project types listview, you need to make sure you've downloaded the Windows Mobile 5.0 SDK for Pocket PC ().

Populate the default Form1 with the following controls (see Figure 4):

  • Label
  • TextBox (set the Multiline property to True and the ScrollBars property to Both)
  • MainMenu


Figure 3. Create a Windows Mobile 5.0 Pocket PC project using Visual Studio.
 
Figure 4. Populate the default Form1 with the various controls as shown.

Switch to the code-behind of Form1 and declare the following member variables:
  • serialPort to connect to the GPS receiver via the serial connection
  • ServerIP to store the IP address of the server
  • ID to store the ID of the user

Public Class Form1 '---serial port to connect to the GPS receiver--- Private WithEvents serialPort As New IO.Ports.SerialPort '---IP address of the server--- Private ServerIP As String = "10.0.1.4" '---the ID of the user--- Private ID As String = "1"

In the event handler for the "Connect GPS" menu item, configure the serial port variable with the necessary parameters and then open the connection:

'---Connect GPS--- Private Sub MenuItem1_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MenuItem1.Click '---close the port if it is already open--- If serialPort.IsOpen Then serialPort.Close() End If '---set the parameters and open the port--- Try With serialPort .PortName = "COM4" .BaudRate = 9600 .Parity = IO.Ports.Parity.None .DataBits = 8 .StopBits = IO.Ports.StopBits.One End With serialPort.Open() '---disable the Connect GPS menu item--- MenuItem1.Enabled = False '---enable the Disconnect menu item--- MenuItem3.Enabled = True Catch ex As Exception MsgBox(ex.ToString) End Try End Sub

To receive incoming data from the GPS receiver, you need to service the DataReceived event of the SerialPort class:

Private Sub DataReceived( _ ByVal sender As Object, _ ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _ Handles serialPort.DataReceived TextBox1.BeginInvoke(New _ myDelegate(AddressOf updateTextBox), _ New Object() {}) End Sub

Here, I have used a delegate (myDelegate) to update the TextBox controls using the received data from the GPS receiver:

Public Delegate Sub myDelegate() Public Sub updateTextBox() TextBox1.Text += serialPort.ReadExisting() Dim lines() As String = TextBox1.Text.Split(vbLf) If lines.Length < 2 Then Exit Sub End If If TextBox1.Text.Length >= 500 Then '---clear until the last $--- TextBox1.Text = _ TextBox1.Text.Substring(TextBox1.Text.LastIndexOf("$")) End If If lines(lines.Length - 2).StartsWith("$GPGGA") Or _ lines(lines.Length - 2).StartsWith("$GPRMC") Then processGPSData(lines(lines.Length - 2)) End If End Sub

One tricky issue with the GPS receiver is that data is not received in discrete blocks. For example, instead of receiving the following two lines of data in two discrete blocks:

$GPGGA,001431.092,0118.2653,N,10351.1359,E,0,00,,-19.6,M,4.1,M,,0000*5B $GPGSA,A,1,,,,,,,,,,,,,,,*1E

The data may arrive in four separate blocks (the end of each of the two lines will be appended with characters 13 and 10 (new-line characters)):

$GPGGA,001431.092,0118.2653,N, 10351.1359,E,0,00,,-19.6, M,4.1,M,,0000*5B $GPGSA,A,1,,,,,,,,,,,,,,,*1E

Tip: Using the ReadLine() method of the SerialPort class seems like it will do the trick. However, in practice it does not work and often chokes on the receiving data.

Figure 5. Append all received GPS data to the TextBox control.
A good workaround is to append all incoming data to a TextBox control and manually retrieve the necessary data that you want to process. As illustrated in Figure 5, the latest data is always located at the bottom of the TextBox control. As the last line may contain incomplete GPS data, you should only examine the second-to-last line. You will do so by splitting the entire contents of the TextBox control into a string array (using the vbLf character as delimiter) and then examining the penultimate element in the array.

As the TextBox has a maximum limit on the number of characters it can store, you need to constantly clear it after a certain number of characters. To improve performance, ideally you should not leave too much data in the TextBox control as the splitting of its content into an array requires additional processing time. And hence, 500 characters is a good compromise.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap