Linux serial port reads GPS Data

Source: Internet
Author: User
Linux serial port reads GPS data-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. Related Concepts

1. GPS (Global Positioning System): Global Satellite Positioning System
The 24 satellites in the United States constantly send GPS signals to the ground. As long as there is a GPS receiving device, you can locate the height and speed of your location, its combination with electronic maps is commonly referred to as the GPS function.

Ii. Connection Mode

1. Serial Port: The computer usually reads gps data through serial ports (Bluetooth gps also uses virtual serial ports)

2. steps:

1) general serial port device
Open the serial port-> set the serial port-> Read data-> recover the serial port-> close the serial port

2) bluetooth device
Enable Bluetooth-> connect to the gps device-> open the serial port-> Read data-> close the serial port-> disconnect from the gps device-> disable the Bluetooth

3) Note
Set the connection password to 0000 (default Bluetooth password) if you need to connect to the gps bluetooth device)
Generally, you do not need to set serial ports when using a Bluetooth gps device.

Iii. gps Data Format

1. Read text data from the serial port. One row is read each time. Generally, multiple lines of data can be received in one second.

2. valid data starts with $ GP and can be divided into $ GPGGA, $ GPGSA, $ GPGSV, $ GPRS...

3. $ GPGGA is the main data for GPS positioning. It is parsed to obtain basic information such as longitude, latitude, altitude, time, and satellite usage. each item is separated by commas (,), and a total of fourteen commas (,) are used, example:
$ GPGGA, 064746.000, 4925.4895, N, 00103.9255, E, 2.1, 68.0,-47.1, M, 0000, M, * 4F
$ GPGGA (statement ID header), 064746.000 (UTC time), 4925.4895 (latitude), N (latitude hemisphere), 00103.9255 (longitude), E (longitude hemisphere ), 1 (positioning quality indication), 05 (number of satellites used), 2.1 (horizontal accuracy),-68.0 (altitude), M (height unit), 47.1 (geolevel height ), M (height unit), (Differential GPS data period), 0000 (differential reference base station number) * 4F (verification) (End mark carriage return line feed)

4. Routine

1. Function Description
Assume that the bluetooth module and gps are connected successfully, and the virtual serial port device of Bluetooth is ttyB2.
100 data records are read from the bluetooth device connection. After Resolution (longitude, latitude, altitude, and number of satellites), the data is displayed.

2. Code

# Include

# Include

# Include

# Include

# Include

# Include



Void parseData (char * buf)

{

Int ret, nQ, nN, nB, nC;

Char cX, cY, cM1, cM2;

Float fTime, fX, fY, fP, fH, fB, fD;



If (buf = NULL)

Return;

Ret = sscanf (buf,

"$ GPGGA, % f, % f, % c, % f, % c, % d, % 02d, % f, % f, % c, % f, % c, % c, % f, % 04d % 02x ",

& FTime, & fX, & cX, & fY, & cY, & nQ, & nN, & fP, & fH, & cM1, & fB,

& CM2, & fD, & nB, & nC );

Printf ("x: % c % f, y: % c % f, h % f, satellite: % d \ n ",

CX, fX, cY, fY, fH, nN );

}



Int main (int argc, char ** argv)

{

Int fd, I, ret;

Char buf [1024] =

"$ GPGGA, 064746.000, 4925.4895, N, 00103.99255, E, 2.1, 68.0,-47.1, M, 0000, M, * 4F \ r \ n "; // The value is used for testing.



If (fd = open ("/dev/ttyB2", O_RDWR) =-1)

Return-1;

// Set fd: tcsetattr... the baud rate must be set here for devices directly connected to the serial port.

For (I = 0; I <100; I ++)

{

Ret = read (fd, buf, 1024 );

If (ret> 1)

{

If (strstr (buf, "GPGGA ")! = NULL)

ParseData (buf );

}

}

// Restore fd: tcsetattr... the device that directly connects to the serial port must restore the baud rate here.

Close (fd );

}

V. Related Materials

1. GPGGA details
Http://hi.baidu.com/xydjh/blog/item/b0caa1433902ab1073f05dc8.html

2. Code for Reference
Http://bbs.tongji.net/viewthread.php? Tid = 562733

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.