![]() |
General Reading »
Hardware & System »
Hardware programming
Intermediate
Add GPS support to your desktopBy A. RiaziUse serial ports to add GPS (Global Positioning System) support to your desktop computer by using NMEA0183 protocol |
VC6Win2K, WinXP, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
||||||||||||||||||

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.
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.
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
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 );
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:
AddTail() to add strings retreived from GPS.
Parse() to parse retreived strings. And
PlainText a member variable that stores plain text of translated string.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);
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.
For further information about NMEA0183 protocol and new specifications, visit NMEA.org
Enjoy!
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Feb 2003 Editor: Nishant Sivakumar |
Copyright 2003 by A. Riazi Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |