Sybase iAnywhere SQL AAnywhere Mobile and Embedded Database

Rows and Columns


SQL Anywhere perspective on technology

header image

GPS – How does it Work?

March 31st, 2009 · 3 Comments

GPS devices are becoming more and more prevalent. They’ve been sold as separate navigation units by a variety of vendors, been embedded into the dashboards of newer luxury vehicles, and I expect they will soon become a standard part of a cell phone, much like the camera has evolved. This will lead to more and more consumer and business applications that take advantage of location information. Being able to access and interact intelligently with this kind of data is especially important to SQL Anywhere.

I have found GPS very handy. I’ve used one (a Garmin) to find directions in a variety of places. I’ve also used desktop programs like Google Earth and Microsoft MapPoint to generate directions and zoom to exotic locations around the globe.

I also found out that I could build my own GPS tracking device using Google Earth or MapPoint and a “GPS Puck”. A GPS puck is basically a GPS receiver, about the size of a USB key/thumb drive. It receives a GPS signal and transmits it (either via direct connection or bluetooth) to a computer. Both Google Earth and MapPoint have the ability to receive and interpret GPS signals, so all I had to do was turn on the puck, enable bluetooth on my laptop, and start MapPoint. Voila! There I was a little dot on the screen travelling around in my car. Cool.

I wondered if I could actually receive the GPS signal myself in an application, interpret it and store the data into a database, allowing me to keep a trip log. Some more googling led me to this 3 part sample on the CodeProject website:
Writing Your Own GPS Applications: Part I
Writing Your Own GPS Applications: Part 2
Writing GIS and Mapping Software for .NET

To put everything together, I had to first read the data being received by the puck. The GPS puck uses bluetooth to transmit the data it receives to the computer. I cobbled together a simple COM port reader to read the data, and call the parser (I don’t remember where I got the source from):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class COMreader
{
  static private System.IO.Ports.SerialPort m_serialPort1;
  static private Thread m_th = null;
  static private bool m_run = false;
  static public String m_readed = "";
  static private String m_port;
  static private bool m_demo;
  public delegate void DataReceivedEventHandler(String data);
  public event DataReceivedEventHandler dataReceived;
 
  public COMreader(String port) {   
      m_port = port;
  }
  public void start(bool demo) {
      m_demo = demo;
 
      if (m_th == null) {
          if (!demo && m_serialPort1 == null) {
              m_serialPort1 = new System.IO.Ports.SerialPort(m_port);
          }
          m_th = new Thread(methodTh);
      }
      m_run = true;
      m_th.Start();
  }
 
  public void stop() {
      m_run = false;
      Thread.Sleep(500);
      if (!m_demo) {
          m_serialPort1.Close();
      }
      if (m_th != null) {
          m_th.Abort();
          m_th = null;
      }
  }
 
  private void methodTh() {
      if (!m_demo) {
          m_serialPort1.Open();
      }
 
      byte[] buffer = new byte[100];
      while (m_run) {
          Thread.Sleep(1000);
          if (m_demo) {
              dataReceived(randomString());
          }
          else {
              m_readed = m_serialPort1.ReadLine();
              if (m_readed.Length > 0) {
                  dataReceived(m_readed);
              }
          }
      }
  }
}

This, coupled with the code sample from Writing Your Own GPS Applications: Part I gave me a library that would receive and parse data from the GPS device.

Now all I needed was a simple GUI to display/record/operate on the received data. I’ll save that for a future post.

Be Sociable, Share!

Tags: Development · GIS · Spatial

3 responses so far ↓