Click here to Skip to main content
Click here to Skip to main content
Go to top

Add GPS support to your desktop

, 18 Feb 2003
Rate this:
Please Sign up or sign in to vote.
Use serial ports to add GPS (Global Positioning System) support to your desktop computer by using NMEA0183 protocol

Sample Image - GPS_support.jpg

Introduction

GPS stand for Global Positioning System and is a device for finding useful information about geographical location (in other hand, Longitude and Latitude). But these information are not limited to Longitude and Latitude. Some other information like Time, Date, Compass information, Speed and etc. can be obtained from GPS.

GPS devices use satellites (minimum of 3 and maximum of 14) to find your location on the earth. Most modern GPS devices now support a protocol called NMEA0183. This protocol used to transfer geographical location information from GPS to your desktop computer or PDA.

Connecting GPS to your PC

To transfer data from GPS to your computer, you need a serial cable that can connect to your GPS. After this, you must configure your serial port to communication go right. In this project, I configure my serial port as follow:

COM Port: COM2
Baud Rate: 4800
Data Bits: 8
Parity: No Parity
Stop Bits: 2

You must refer to your GPS manual to configure your serial port properly.

NMEA0183

As I mentioned before, NMEA0183 is a standard protocol to transfer location information from GPS to your PC. I use a free library that can be added to your project without any modification.

you can find this library in above file. Thanks Sam Blackburn for his nice library. This protocol consists of several sentences that every sentences start by $. For example the sentence

"$GPGGA,104435.12,3337.19,N,11158.43,W,1,06,4.5,,,,,,"

has these informations (in other hand, translated to):

Time: 10:44:35 AM UTC
Latitude: 33 37.19 North
Longitude: 111 58.43 West
Number of satellites: 6

Using Library

First of all, you must add nmea0183.lib to your project. For this reason, Select Project, Settings and then Link Tab. In Object/Library Modules type path of nmea0183.lib library (for example: .\NMEA0183\Debug\NMEA0183.lib).

Then press OK to return to project.

If you use a dialog in your project, Add "NMEA0183.h" to your dialog header file. In other cases add "NMEA0183.h" to main header file.
Now, Add a member variable named nmea0183 with variable type NMEA0183, like this:
NMEA0183 nmea0183;

After this, you must add header and implementation files for serial communication. I use CSerialCom from Shibu K.V. Find his article at this URL: A simple Class for Implementing Serial Communication in Win-9X/2000

Add a member variable named Serial with variable type CSerialCom, like this:
CSerialCom Serial;

Now configure your serial port by:

//Open Port: COM2
Serial.OpenPort("COM2");

//Configure COM2
Serial.ConfigurePort(4800,          //Baud Rate
                     8,             //Data Bits
                     FALSE,         //Has Parity
                     NOPARITY,      //Parity Bits
                     TWOSTOPBITS    //Stop Bits
                    );

If anything goes right, you can obtain information from GPS by Read member function, as follow: Note: any NMEA0183 sentences will finished by "\r\n" characters.
//Read data from GPS 
    
char Data[100]="\0";

BYTE DataByte='\0';
int nIndex=0;

//Obtaining information from GPS character by character
//Note: NMEA0183 sentences will finished by "\r\n" characters!
BOOL Return=Serial.ReadByte(DataByte);
while (DataByte!='\r' && DataByte!='\n' && Return==TRUE)
{
    Data[nIndex]=DataByte;
    nIndex++;
    Return=Serial.ReadByte(DataByte);
}
    
Data[nIndex++]='\r';
Data[nIndex++]='\n';
Data[nIndex++]='\0';

//Remove garbage characters if any exists!
nIndex=0;
while (Data[nIndex]!='$' && nIndex<100)
    nIndex++;

Now, it's time to work with nmea0183 member variable. NMEA0183 class has two very important member functions and one member variable:

  1. AddTail() to add strings retreived from GPS.
  2. Parse() to parse retreived strings. And
  3. PlainText a member variable that stores plain text of translated string.

Solution

I use a thread in my dialog based project that it's duty is to communicate to GPS and show location information in a plain text. My thread function is as below:

UINT MyThread(LPVOID pParam)
{
    CSerialCom Serial;
    NMEA0183 nmea0183;
    CStringList StrList;

    if (!Serial.OpenPort("COM2"))
    {    
        AfxMessageBox("Can't Open Port!");
        return 0;
    }
    
    Serial.ConfigurePort(4800, 8, FALSE, NOPARITY, TWOSTOPBITS);
    
    //Read data from GPS
    char Data[100]="\0";
    
    BYTE DataByte='\0';
    int nIndex=0;
    
    BOOL Return=Serial.ReadByte(DataByte);
    while (DataByte!='\r' && DataByte!='\n' && Return==TRUE)
    {
        Data[nIndex]=DataByte;
        nIndex++;
        Return=Serial.ReadByte(DataByte);
    }
    
    Data[nIndex++]='\r';
    Data[nIndex++]='\n';
    Data[nIndex++]='\0';
    
    //Remove garbage characters
    nIndex=0;
    while (Data[nIndex]!='$' && nIndex<100)
        nIndex++;
    
    StrList.RemoveAll();
    StrList.AddTail(Data+nIndex);
    
    //Parse strings    
    POSITION position = StrList.GetHeadPosition();
    
    while(position!=NULL)
    {
        nmea0183 << StrList.GetNext(position);
        
        if (!nmea0183.Parse())
            AfxMessageBox("Can't parse!");
        else
        {    
            if (nmea0183.PlainText.GetLength()!= 0)
            {
                CString sMsg;
                sMsg.Format("%s",(const char *) nmea0183.PlainText);
                AfxMessageBox(sMsg);
            }
        }
    }
    Serial.ClosePort();
    
    return 0;
}

When you want to show GPS Information, use this thread as below:

AfxBeginThread(MyThread,NULL);

Linking NMEA0183 Library

We can link NMEA0183 library in two ways: Statically Linking and Dynamically Linking. For both of these situations, you must choose proper switches for compiler to compile NMEA0183 library. If you want to compile your application and link it statically with MFC, NMEA0183 should be compiled and linked statically. And if you want to compile your application and link it dynamically with MFC, you should compile NMEA0183 library dynamically.

Further Information

For further information about NMEA0183 protocol and new specifications, visit NMEA.org

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

You can see list of my articles, by clicking here


Comments and Discussions

 
QuestionGPS based system example Pinmemberkasun61810-Oct-13 22:27 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 19:58 
QuestionProblem - information [modified] Pinmemberpentagona22-Jun-11 0:56 
QuestionHow does the program know if its GPGGA data? Pinmemberwollow18-Aug-10 15:12 
AnswerRe: How does the program know if its GPGGA data? Pinmemberwollow18-Aug-10 15:21 
GeneralCan't Parse still a problem... Pinmemberjo3ran7-Oct-09 0:04 
GeneralRe: Can't Parse still a problem... PinmemberMozartjpn29-Oct-09 4:36 
GeneralNeed HELP PinmemberBui Tan Duoc7-Dec-08 20:29 
Generalcricket Pinmembergivitha12-Aug-08 19:53 
QuestionA C program to read GPS data Pinmemberroopan_data26-Jul-08 9:57 
QuestionHow to use this code in VS2005, Smart device platform, MFC or win32 Pinmembersusilrani8-May-08 17:15 
Questionone receive data, one time display "cannot parcing" ...what problem? plz help me Pinmemberpsmst13-Mar-07 16:53 
AnswerRe: one receive data, one time display "cannot parcing" ...what problem? plz help me PinmemberA. Riazi15-Mar-07 8:49 
Questionhow can I link the nmea0183.lib Pinmemberjhualiu12-Nov-06 22:42 
Generalask about making thread Pinmemberlibbey20-Sep-06 18:17 
Questioni need your help plz Pinmemberbrahimi moussa10-Jul-06 8:58 
AnswerRe: i need your help plz PinmemberA. Riazi18-Jul-06 7:57 
QuestionOptimization? PinmemberMarcello Faga10-Jul-06 8:00 
GeneralGPS Pinmemberk.sushma7-Jul-06 21:21 
GeneralCan't Parse msg. PinmemberAndrew Qu30-May-06 23:54 
AnswerRe: Can't Parse msg. PinmemberA. Riazi31-May-06 3:54 
GeneralRe: Can't Parse msg. PinmemberAlastair Stell18-Oct-06 11:21 
GeneralRe: Can't Parse msg. PinmemberAndrew Qu24-Oct-06 0:35 
GeneralRe: Can't Parse msg. Pinmemberrajarcr21-Jun-07 18:35 
GeneralRe: Can't Parse msg. PinmemberAndrew Qu21-Jun-07 23:19 
GeneralRe: Can't Parse msg. Pinmemberrajarcr28-Jun-07 0:50 
GeneralRe: Can't Parse msg. PinmemberAndrew Qu29-Jun-07 11:16 
GeneralMy inaccurate data or conversions PinmemberJohnnyBoyWonder19-Sep-05 9:11 
GeneralRe: My inaccurate data or conversions PinmemberJohnnyBoyWonder20-Sep-05 5:05 
GeneralRe: My inaccurate data or conversions PinmemberJohnnyBoyWonder21-Sep-05 2:02 
Generalnice city Pinmemberbiberkopf6-Sep-05 23:04 
GeneralRe: nice city PinmemberA. Riazi6-Sep-05 23:47 
GeneralRe: nice city Pinmemberbiberkopf7-Sep-05 21:17 
GeneralRe: nice city PinmemberA. Riazi7-Sep-05 22:14 
GeneralConverting positions to x-y coordinates PinsussAnonymous19-May-05 8:37 
GeneralRe: Converting positions to x-y coordinates PinsussMuhammad Arsalan Khan9-Jul-05 1:38 
GeneralCan't Open Port Pinsussilmo Jung20-Mar-05 19:17 
GeneralRe: Can't Open Port PinmemberA. Riazi20-Mar-05 19:32 
GeneralRe: Can't Open Port Pinmember_DIFF_20-Mar-05 23:07 
GeneralRe: Can't Open Port Pinsussilmo Jung20-Mar-05 19:45 
Generalxyz co-ordinates PinsussAnonymous27-Jan-05 8:24 
GeneralRe: &amp;#52852;&amp;#46300;&amp;#49324; &amp;#51204;&amp;#54868; &amp;#48155;&amp;#44592;/&amp;#48730; &amp;#50504;&amp;#44058;&amp;#44256; &amp;#50724;&amp;#47000; &amp;#48260;&amp;#54000;&amp;#44592; &amp;#50836;&amp;#47161; PinmemberA. Riazi17-Apr-04 17:33 
GeneralGPS -Requirements Pinmemberjohnmcaffe16-Apr-04 19:51 
GeneralRe: GPS -Requirements PinmemberA. Riazi16-Apr-04 21:01 
GeneralRe: GPS -Requirements PinmemberAlex Evans6-May-04 12:47 
GeneralRe: GPS -Requirements PinmemberA. Riazi6-May-04 19:09 
GeneralGPS / Microcontroller Pinmemberknightri17-Mar-04 6:10 
GeneralRe: GPS / Microcontroller PinmemberA. Riazi19-Mar-04 3:51 
GeneralThats so cool PinmemberHockey11-Feb-04 22:21 
GeneralRe: Thats so cool PinmemberA. Riazi14-Feb-04 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 19 Feb 2003
Article Copyright 2003 by A. Riazi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid