GPS Serial Communications

GPS Serial Communications

GPS Serial Communications

Keywords: GPS, Garmin Emap, serial communication, RS-232,C program, string messages, reading GPS string, parsing string message, extracting longitude and latitude from GPS string, NMEA protocal

The photo shows a Garmin eMap, a common handheld GPS unit, that comes with a serial cable allowing you to interface it with a PC. Using a terminal program like Windows' Hyper terminal or DOS' Lynx, one can directly access the GPS to view the incoming geospatial message received from satellites orbiting Earth. Among other parameters, the message describes the unit's position (longitude and latitude)and speed (if carried or transported in a vehicle). Using a terminal program to read the geospatial message isn't very practical. The messages scroll too quickly to read well and they contain a lot of extraneous information.This tutorial presents DOS Turbo C code that shows you how to serially read the GPS unit and extract desired information from the ASCII message.

Motivation and Audience

A GPS (Global Positioning System) receiver reports its location on Earth. The longitudinal and lateral coordinates can be used for applications like navigating vehicles, coordinating search and rescue efforts and mapping trails and exploring new terrains. Custom building devices that use GPS are possible - one designs around systems like the Trimble Lassen LP starter kit ($595 USD) or a Motorola OnCore development kit. A more affordable option is to purchase and design around a handheld GPS receiver, leveraging its serial interface port.

An audience interested in this tutorial could be asking the following questions:

  • How do I serially interface a handheld GPS to a PC or embedded micro?
  • How do I extract or parse the NMEA GPS message string?
  • Is there DOS C source code examples for extracting or parsing the GPS message string?

This tutorial does not explain GPS theory, discuss GPS hardware or describe NMEA message strings in great detail. Some references for these are given at the end of the tutorial.

The tutorial breakdown is as follows:

GPS Message Strings and Terminal Programs

The National Marine Electronics Association (NMEA) defined a RS-232 communication standard for devices that include GPS receivers.The GPS receivers can output geospatial location, time, headings and navigation-relevant information in the form of ASCII comma-delimited message strings. Hyper terminal, bundled with Windows, can be used to view these message strings. For DOS there's Lync, a popular shareware program for dialing modems via the serial port.

GPS and Hyperterminal

Handheld GPS receivers, like the Garmin eMap, come equipped with a cable that plugs into your PC's serial port. Hyper terminal is a communications program that comes with Windows 95/98/ME/NT and 2K. Your GPS receiver's handbook probably describes the necessary terminal settings; a typical port setting is 4800 baud, 8N1 (eight data bits, no parity, 1 stop bit) with no flow control. With eMap hooked up with serial port COM1, your Hyper terminal display should look like the screen shot below, with message strings updated every 2 seconds or so.

GPS and DOS Lync

Lync is DOS shareware popular for PC modem dial-ups. Downloading and installing will allow you to display GPS receiver message strings in DOS. My eMap was plugged into my PC's COM1 serial port and Lync was was configured at 4800 baud, 8N1 and no flow control. The screenshot below is the result; GPS message strings could be seen with a new message displayed every 2 seconds or so.

Dissecting the GPS Message String

The screen shots above showed message string output from a GPS receiver. The NMEA standard dictates how each string is formed with a dollar sign ($) leading each new GPS message.

References to details of each message string are listed at the end of the tutorial. A brief description of the seven standard message strings are:

$GPGLL Geographical postiion, latitude and longitude $GPGSA GPS dilution of precision and active satellites $GPGSV GPS satellite in view $GPGGA GPS fixed data $GPRMC Recommended minimum specific GPS/TRANSIT data $GPVTG Track made good and ground speed $GPZDA Time and date

Highlighted above, the $GPGGA string is popular examined because it contains navigational data most commonly sought after. For example, looking at the $GPDDA string in the previous DOS Lync screen shot more closely reveals the following

The format for the $GPGGA message string is: ``$GPGGA,,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,g.g,z,t.t,iii*CC**ending** with a CR and LF (carriage return and line feed). Where we havehhmmss.ssin UTC (coordinated universal time zone). UTC used be known as GMT.ddmm.mmmm,Nlatitude of the GPS position fixdddmm.mmmm,Wlongitude of the GPS position fixqquality of the GPS fix (1 = fix, but no differential correction)ssnumber of satellites being usedy.yhorizontal dilution of precisiona.a,MGPS antenna altitude in metersg.g,Mgeoidal separation in meters t.t age of the deferential correction dataiiii` deferential station's ID *CC checksum for the sentence

Extracting or Parsing the GGA string

A program can be written to serially read the incoming message strings and determine if it's a GPGGA message. If so, the program can extract and/or display only geospatial information we might want, like the time and longitude/latitude coordinates.

Written in accordance to ANSI standards, a C program will run on any platform. C was thus used anticipating a future design where the PC interface is replaced with a PIC microcontroller or single board computer. A DOS program was written because the software overhead incurred using Windows wasn't warranted in this design. DOS is also relatively easier to work with and the resulting code can even run on older PCs.

Software used:

Borland's Turbo C DOS compiler, which continues to be a freeware download, is used in this tutorial. To simplify serial port programming, shareware/freeware libraries exist.In my experience, many libraries don't work well or at all, and hence some caution should be exercised.

Source Code

Turbo C code

Download C source file gps1_5.c

To compile, at the DOS prompt type tcc -ml gps1_5.c ibmcom3.obj.This of course assumes that ibmcom3.obj is in the same directory as gps1_5.c. the -ml option invoke Turbo C's large memory model. Running the executable (gps1_5.exe) will display theUTC time, the Eastern Standard time (EST) equivalent and both latitude and longitude coordinates. Additionally, all message strings output by your GPS receiver are saved into an ASCII file named gpsData.txt.

Code Description

gps1_5.c begins by opening a file gpsData.txt which will save all GPS message strings in ASCII. Next, the serial port is opened using a function prototype comm_setting() which invokes functions found in the IBMCOM library.

A while loop is entered, where the statementcharRead = com_rx(); serially reads a character and checks if it begins with a dollar sign.If so, this indicates a new GPS message string has been received and more characters are read until a carriage return (CR) is found.

tempString holds the GPS message string that was serially read. If it is a $GPGGA message, additional reading is done, where we know that commas separate geospatial data. sscanf issused to extract numerical data from the ASCII characters.

Where To Go From Here

A handheld GPS receiver that has a serial interface allows message strings to be viewed with a PC terminal program like Hyper terminal. This tutorial showed that a DOS program can be written to display, store, extract or parse the message string. Doing so allow you to leverage the GPS' ability to report its location to design equipment for applications demanding navigational, mapping and localization data.

The Turbo C program featured is an example where onlyUTC time, longitudinal and latitude coordinates were extracted from the $GPGGA message string. One could customize the code to seek other GPS message strings to extract altitude, or ground speed for example. DOS and C were used keeping expansion in mind; compact GPS-based equipment can be made by using a PIC microcontroller to replace a PC. This tutorial was not meant to describe GPS in detail, but rather share how to serially interface a GPS for one's own hardware interests. Some references for GPS follow to best conclude this tutorial and leave you with imaginative possibilities.


  • UTC Time Zone from the U.S. Naval Observatory (USNO).
  • One can build serial cables for popular GPS handhelds from Pfranc's web site. Their cables are also very affordable.
  • type a U.S. address and the longitudinal and latitude coordinates are reported
  • Dale DePriest's web site is a comprehensive collection of useful GPS-related information
  • Samuel J. Wormley's site is an engineering-based collection of GPS information