GPS encoding format and C language decoding code

Source: Internet
Author: User
Tags in degrees
As long as the GPS receiver is in the working state, it will continuously transmit the received and computed GPS navigation and positioning information to the computer through the serial port. The previous code is only responsible for receiving data from the serial port and storing it in the cache. before further processing, the cache contains a long string of byte streams, this information cannot be used before being extracted by category. Therefore, the information of each field must be extracted from the cached byte stream by a program, and converted into location information data that is meaningful and can be used by high-level decision-making. Similar to other communication protocols, to extract GPS information, you must first specify its frame structure, and then extract the positioning information according to its structure. For the Garmin GPS antenna board used in this article, the data sent to the computer mainly consists of the frame header, frame tail, and intra-frame data. According to the different data frames, the frame headers are also different, they mainly include "$ gpgga", "$ gpgsa", "$ gpgsv", and "$ gpmc. These frame headers identify the structure of data in subsequent frames. Each frame uses a carriage return and a line break as the end of the frame. In general cases, positioning data, such as longitude and latitude, speed, and time, can be obtained from the "$ uplmc" frame. The Frame Structure and fields are interpreted as follows:

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

<1> The GMT Time of the current location, in the format of hhmmss

<2> status. The value a indicates the valid position, and the value V indicates that no warning is received. That is, the number of satellites above the current antenna's field of view is less than three.

<3> latitude, in the format of ddmm. Mmmm

<4> indicates the North-South Hemisphere. N indicates the Northern Hemisphere and S indicates the southern hemisphere.

<5> diameter, in the format of dddmm. Mmmm

<6> specify the Eastern Hemisphere, e as the Eastern Hemisphere, and w as the Western Hemisphere.

<7> speed on the ground, ranging from 0.0 to 999.9

<8> azimuth, ranging from 000.0 to 359.9 degrees

<9> date, in the format of ddmmyy

<10> geomagnetic changes, from 000.0 to 180.0 degrees

<11> geomagnetic variation direction, E or W

Other frame formats are not commonly used except for special purposes. Although the receiver is continuously sending various data frames to the host, however, during processing, the data is extracted and processed only for the "$ uplmc" frame by determining the frame header. In special cases, data needs to be obtained from other frames, and the processing method is similar. Because each data segment in a frame is separated by a comma, when processing cached data, it is generally determined whether it is a frame header by searching for the ASCII code "$, after recognizing the category of the frame header, you can determine the positioning and navigation parameters that are currently being processed by counting the number of processed commas (,) and process them accordingly. The following is the main code for decoding the data in the cache data. This article only cares about the time (date and time) and geographical coordinates (longitude and latitude ):

For (INT I = 0; I if (Data = '$') file: // frame header, sectionid is a comma counter
Sectionid = 0;
If (Data = 10) {file: // end of the frame
}
If (Data = ',') file: // comma count
Sectionid ++;
Else {
Switch (sectionid ){
Case 1: file: // extract time
M_stime + = data;
Break;
Case 2: file: // determine whether the data is credible (A is credible when the GPS antenna can receive three GPS satellites)
If (Data = 'A ')
Gpsparam [m_nnumber]. m_bvalid = true;
Break;
Case 3: file: // extracts the latitude
M_spositiony + = data;
Break;
Case 5: file: // extract the longitude
M_spositionx + = data;
Break;
Case 9: file: // extract date
M_sdate + = data;
Break;
Default:
Break;
}
}
}

Now the required information has been extracted to the memory, that is, the time, date, and latitude and longitude are stored in the cstring variables m_stime, m_data, m_spositiony, and m_spositionx respectively. In practical applications, further computation is often required. For example, the time information obtained from the GPS receiver is Greenwich Mean Time, therefore, we need to add 8 hours to the acquisition time to set the standard time in China. Moreover, the WGS-84 coordinate system used by GPS is also different from the coordinate system used in China, and sometimes it needs to be transformed. These transformation operations must be completed through numerical operations. Therefore, you need to convert the obtained numeric variables to numeric variables. This work can be completed at the end of the detected frame:

: Strcpy (BUF, m_stime );
Str. Format ("% C", Buf [0], Buf [1]);
Gpsparam [m_nnumber]. m_nhour = (atoi (STR) + 8) % 24; file: // extract the hour and convert it to 24-hour Beijing Time
File: // Buf 2nd, 3 bytes for minutes, 4, 5 bytes for seconds, the extraction method is the same as above
......
: Strcpy (BUF, m_sdate );
Str. Format ("% C", Buf [0], Buf [1]); file: // extract the month
File: // Buf 2nd, 3 bytes is a day, 4, 5 bytes is a year, the extraction method is the same as above
......
: Strcpy (BUF, m_spositiony );
Str. Format ("% C", Buf [0], Buf [1]);
Positionvalue = atoi (STR );
Str. format ("% C", Buf [2], Buf [3], Buf [4], Buf [5], buf [6], Buf [7], Buf [8]);
Gpsparam [m_nnumber]. m_dpositiony = positionvalue * 60 + atof (STR); file: // extracts the latitude
......
: Strcpy (BUF, m_spositionx );
If (m_spositionx.getlength () = 10) file: // the longitude exceeds 90 degrees (for example, the longitude is 125 degrees)
{
Str. Format ("% C", Buf [0], Buf [1], Buf [2]);
Positionvalue = atoi (STR );
Str. format ("% C", Buf [3], Buf [4], Buf [5], Buf [6], buf [7], Buf [8], Buf [9]);
Gpsparam [m_nnumber]. m_dpositionx = positionvalue * 60 + atof (STR); file: // extract the longitude (unit: minute)
}
If (m_spositionx.getlength () = 9) file: // the longitude does not exceed 90 degrees (for example, the longitude is 89 degrees)
{
File: // processing method is the same as above, except that the 0th and 1 bytes of the Buf are measured in degrees, 2 ~ 9 is the score.
}

So far, the time and latitude and longitude information has been extracted to the various variables in the GPS structure array gpsparam, subsequent processing and high-level decision-making can be performed based on the data stored in the structure.

Related Article

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.