Example of source code for parsing GPS Data Format

Source: Internet
Author: User
Tags filetime time and date

 

Example of source code for parsing GPS Data Format

 

With more and more built-in GPS Mobile Phones, more and more GPS-related applications, GPS is not only getting a latitude and longitude information, you can use GPS to develop more applications, such as location pictures, for example, the display of friend locations, such as tracking, etc. timesyncppc can use the GPS clock for time synchronization.

All these functions need to know the GPS data format and be able to parse the data they need. The following describes how to parse GPS data by taking the date and time of GPS in timesyncppc as an example.

Timesyncppc is a time synchronization tool used on the Pocket PC. Therefore, to obtain the time and date of GPS, the GPS command is $ GPRS. The command format is as follows:

$ Uplmc, <1>, <2>, <3>, <4>, <5>, <6>, <7>, <8>, <9>, <10>, <11>

1) standard positioning Time (UTC time) Format: time, minute, second (hhmmss. Sss ).
2) Positioning status, A = data available, V = data unavailable.
3) latitude, in the format of ddmm. Mmmm ).
4) latitude differentiation: Northern Hemisphere (n) or Southern Hemisphere (s ).
5) longitude, in the format of "degree score. Score.
6) The longitude is distinguished, East (e) hemisphere or West (w) hemisphere.
7) relative displacement speed: 0.0 to 1851.8 knots
8) relative displacement direction: 000.0 to 359.9 degrees. The actual value.
9) date, in the format of ddmmyy ).
10) magnetic pole variable, ranging from 000.0 to 180.0.
11) degree.
12) checksum. (checkbit)

We can see from the data format that we need to get the data of the fields 1 and 9.

Of course, to use GPS, you must first open the serial port of GPS. The Code is as follows:
Handle opencom (cstring strcom, DWORD baudrate, byte bytesize, byte stopbits, byte parity, int flowcontrol)
{
Handle hcommp ORT;

Cstring strtemp;

If (hcommp ORT = createfile (strcom, generic_read | generic_write, 0,
Null, open_existing, 0, 0) = invalid_handle_value)
{
Return NULL;
}

DCB commdcb;
Cstring strwintext;

Getcommstate (hcommp ORT, & commdcb );

Commdcb. baudrate = baudrate;
Commdcb. bytesize = bytesize;
Commdcb. stopbits = stopbits;
Commdcb. fparity = (noparity = parity )? False: true;
Commdcb. Parity = parity;

Commdcb. fdsrsensiti.pdf = false;
Commdcb. fdtrcontrol = dtr_control_enable;

If (flowcontrol = 1) // hardware
{
// Enable RTS/CTS Flow Control
Commdcb. frtscontrol = rts_control_handshake;
Commdcb. foutxctsflow = 1;
Commdcb. foutx = 0;
Commdcb. finx = 0;
}
Else if (flowcontrol = 0) // Software
{
// Enable Xon/xoff Flow Control
Commdcb. frtscontrol = rts_control_enable;
Commdcb. foutxctsflow = 0;
Commdcb. foutx = 1;
Commdcb. finx = 1;
}
Else
{
Commdcb. frtscontrol = rts_control_enable;
Commdcb. foutxctsflow = 0;
Commdcb. foutx = 0;
Commdcb. finx = 0;
}

Setcommstate (hcommp ORT, & commdcb );

Return hcommp ORT;
}

After the serial port is opened, you need to get a line of GPS data, and the GPS data is ended. The Code is as follows:
// Return value:-1: timeout; other values: Data Length
Int getgpslinedata (handle hcommp ORT, char * readbuf, int length)
{
// Read a row of GPS data
DWORD nbytes;
Int I = 0;
DWORD timeout = 0;
While (I <length)
{
If (g_isquit)
Return I;

Timeout ++;
If (timeout> = 100)
Return-1;

Readfile (hcommp ORT, (lpvoid) & readbuf [I], 1, & nbytes, null );
If (nbytes = 0)
{
Sleep (1 );
Continue;
}

Timeout = 0;

If (readbuf [I] = 0x0d)
{
Readbuf [I] = 0;
Break;
}
If (readbuf [I] = 0x0a)
{
Continue;
}

I ++;
}

Return I;
}

The following code parses a line of GPS data and stores the data results in an array:
// Parse a line of GPS data
Int analyzegpsdata (char * gpsdata, int length, char Command [] [0, 100])
{
Int I = 0;
Int J = 0, K = 0;
While (I <length)
{
If (gpsdata [I] = ',')
{
Command [J] [k] = 0;
J ++;
K = 0;
I ++;
Continue;
}
Command [J] [k] = gpsdata [I];
K ++;
I ++;
}

Return J;
}
The parsed GPS data contains commands and parameters. The following is a command to determine whether it is needed. If so, obtain the first and ninth parameters. However, you must note that GPS time is the world's standard time, so you need to change the local time. The Code is as follows:
DWORD winapi getgpstime (lpvoid lpparameter)
{
Char readbuf [1000];
DWORD timeout = 0;

Char Command [40] [1, 100];

Handle hcommp ORT = opencom (g_strcomport, g_baudrate );
If (hcommp ORT = NULL)
{
MessageBox (null, _ T ("can not open COM port! "), Null, mb_ OK | mb_topmost );
Goto gpsexit;
}

While (1)
{
If (g_isquit)
Break;

// Read a row of GPS data
Int length = getgpslinedata (hcommp ORT, readbuf, 999 );
If (length =-1)
{
Timeout ++;
If (timeout <100)
Continue;
}

If (timeout> = 100)
{
MessageBox (null, _ T ("read GPS data timeout! "), Null, mb_ OK | mb_topmost );
Goto gpsexit;
}
Timeout = 0;

Analyzegpsdata (readbuf, length, command );

If (strcmp (command [0], "$ uplmc") = 0)
{
Systemtime St, localst;

St. whour = (command [1] [0]-'0') * 10 + (command [1] [1]-'0 ');
St. wminute = (command [1] [2]-'0') * 10 + (command [1] [3]-'0 ');
St. wsecond = (command [1] [4]-'0') * 10 + (command [1] [5]-'0 ');
St. wmilliseconds = (command [1] [7]-'0') * 100 + (command [1] [8]-'0 ') * 10 + (command [1] [9]-'0 ');

St. wday = (command [9] [0]-'0') * 10 + (command [9] [1]-'0 ');
St. wmonth = (command [9] [2]-'0') * 10 + (command [9] [3]-'0 ');
St. wyear = (command [9] [4]-'0') * 10 + (command [9] [5]-'0') + 2000;

Filetime, localfiletime;
Systemtimetofiletime (& St, & filetime );
Filetimetolocalfiletime (& filetime, & localfiletime );
Filetimetosystemtime (& localfiletime, & localst );

G_gpsdatectrl-> settime (localst );
G_gpstimectrl-> settime (localst );
If (strcmp (command [2], "A") = 0)
{

Bool ret = setsystemtime (& St );
MessageBox (null, _ T ("synctime success! "), _ T (" "), mb_ OK | mb_topmost );

Strgpsbutton = _ T ("Get gps time ");
G_thisdlg-> setdlgitemtext (idc_getgpstime, strgpsbutton );
G_thisdlg-> getdlgitem (idc_setgpstime)-> enablewindow (0 );

Break;
}
Else
{
Timeout ++;
}
}

}

Gpsexit:

If (hcommp ORT! = NULL)
Closehandle (hcommp ORT );

G_getgpstimethread = NULL;

Strgpsbutton = _ T ("Get gps time ");
G_thisdlg-> setdlgitemtext (idc_getgpstime, strgpsbutton );
G_thisdlg-> getdlgitem (idc_setgpstime)-> enablewindow (0 );

Return true;
}

If you need to parse other commands, you can refer to the implementation method of the function DWORD winapi getgpstime (lpvoid lpparameter), which is actually relatively simple.

These codes have been tested in timesyncppc and can be used directly. The development environment is vs2005 and Windows Mobile 6.1 for PPC. If you have any questions, please leave a message below

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.