Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

GPS (NMEA) data parsing

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. GPS location information

Set the gps mode and start gps. If normal, NMEA data will be reported in the gps channel, as follows:

$GPGSV,3,1,11,01,62,130,42,07,61,201,43,11,72,075,28,17,20,251,38*7A

$GPGSV,3,2,11,30,63,272,44,03,00,149,08,34,046,13,05,309,*76

$GPGSV,3,3,11,22,08,127,27,03,057,28,34,312,*4C

$GPGGA,042523.0,3413.610533,N,10854.063257,E,1,05,2.6438.5,M,-28.0,M,*78

$GPVTG,245.9,T,245.9,M,0.0,N,0.0,K,A*23

$GPRMC,042523.0,A,3413.610533,N,10854.063257,E,0.0245.9,190716,0.0,E,A*0F

$GPGSA,A,2,01,07,11,17,30,2.8,2.6,1.0*3F

(1) $GPGGA (GPS location information)

Protocol format:

$GPGGA,161229.487,3723.2475,N,12158.3416,W,1,07,1.0,9.0,M, 00000018

Detailed analysis of the protocol format:

(2) $GPGLL (geolocation information)

Protocol format:

$GPGLL,*hh

Sample data:

$GPGLL,3723.2475,N,12158.3416,W,161229.487,A*2C

Detailed analysis of the protocol format:

(3) $GPGSA (current satellite information)

Protocol format:

$GPGSA,*hh

Sample data:

$GPGSA,A,3,07,02,26,27,09,04,15, 1.8, 1.0, 1.5, 33

Detailed analysis of the protocol format:

(4) $GPGSV (visible satellite information)

Protocol format:

$GPGSV,..., * hh

Sample data:

$GPGSV,2,1,07,07,79,048,42,02,51,062,43,26,36,256,42,27,27,138,42*71$ GPGSV,2,2,07,09,23,313,42,04,19,159,41,15,12,041,42*41

It should be noted that there are two sample data here, because there are currently seven visible satellites, but each statement contains at most four satellites, so it is divided into two statements. The information of each satellite has four data items, namely: (satellite number), (satellite elevation), (satellite azimuth), and (signal-to-noise ratio).

Detailed analysis of the protocol format (only analyze the first sample data statement):

(5) $GPRMC (minimum location information)

Protocol format:

$GPRMC,*hh

Sample data:

$GPRMC,161229.487,A,3723.2475,N,12158.3416,W,0.13309.62,120598,*10

Detailed analysis of the protocol format:

(6) $GPVTG (ground speed information)

Protocol format:

$GPVTG,*hh

Sample data:

$GPVTG,309.62,T, Mpr 0.13pr. Nje 0.2pr Ke.

Detailed analysis of the protocol format:

II. GPS (NMEA) data parsing

/ * /

Ingps.h

/ * /

# include

# include

# include

# include

# include

# include

# include

# include

# include

/ * /

Degps.h

/ * /

# include "ingps.h"

# define TTYUSB "/ dev/ttyUSB3" / * Serial device * /

# define BAUD_RATE 4800 / * Baud rate * /

# define DATA_BITS 8 / * data bits * /

# define NEVENT'N' / * check * /

# define NSTOP 1 / * stop bit * /

# define BUFLEN 512 / * can be taken at will, but larger than GPGGA_MAX*/

# define GPGGA_MAX 100 / * "$GPGGA." Maximum value of, TBD * /

# define DBG_GPS

Typedef struct {

Int year

Int month

Int day

Int hour

Int minute

Int second

} date_time

Typedef struct {

Date_time D * time * /

Char status; / * receiving status * /

Double latitude; / * Latitude * /

Double longitude; / * longitude * /

Char NS; / * North and South Pole * /

Char EW; / * things * /

Int num; / * number of satellites * /

Double speed; / * Speed * /

Double high; / * height *

} GPS_INFO

/ * /

Getgps.c

/ * /

# include "degps.h"

# include "ingps.h"

Static int GetComma (int num,char * str)

{

Int iJournal jigsaw 0

Int len=strlen (str)

For (iSuppli 0 + isecondally available); / * add one second * /

If (GPS- > second > 59) {

GPS- > second=0

GPS- > minute++

If (GPS- > minute > 59) {

GPS- > minute=0

GPS- > hour++

}

}

GPS- > hour+=8; / * 8 hours between Beijing time and UTC time * /

If (GPS- > hour > 23)

{

GPS- > hour-=24

GPS- > day+=1

If (GPS- > month==2 | | GPS- > month==4 | | GPS- > month==6 | | GPS- > month==9 | | GPS- > month==11) {

If (GPS- > day > 30) {/ * the above months are 30 days per month, and February is less than 30 days / month.

GPS- > day=1

GPS- > month++

}

}

Else {

If (GPS- > day > 31) {/ * the remaining months are 31 days per month * /

GPS- > day=1

GPS- > month++

}

}

If (GPS- > year% 4 = = 0) {

If (GPS- > day > 29 & & GPS- > month = = 2) {/ * February in leap years is 29 days * /

GPS- > day=1

GPS- > month++

}

}

Else {

If (GPS- > day > 28 & & GPS- > month = = 2) {/ * other February is 28 days per month * /

GPS- > day=1

GPS- > month++

}

}

If (GPS- > month > 12) {

GPS- > month-=12

GPS- > year++

}

}

}

Void gps_parse (char * line,GPS_INFO * GPS)

{

Int tmp

Char c

Char* buf=line

C=buf [5]

If (clockwork C') / * "GPRMC" * /

{

GPS- > D.hour = (buf [7] -'0') * 10 + (buf [8] -'0')

GPS- > D.minute = (buf [9] -'0') * 10 + (buf [10] -'0')

GPS- > D.second = (buf [11] -'0') * 10 + (buf [12] -'0')

Tmp = GetComma (9 ~ ~ Buf); / * get the serial number of the next character of the 9th comma * /

GPS- > D.day = (buf [tmp+0] -'0') * 10 + (buf [tmp + 1] -'0')

GPS- > D.month = (buf [tmp+2] -'0') * 10 + (buf [tmp + 3] -'0')

GPS- > D.year = (buf [tmp+4] -'0') * 10 + (buf [tmp + 5] -'0') + 2000

/ * /

GPS- > status = buf [GetComma (2jinbuf)]; / * status * /

GPS- > latitude = get_double_number (& buf [GetComma (3jinbuf)]); / * Latitude * /

GPS- > NS = buf [GetComma (4jinbuf)]; / * North and South latitudes * /

GPS- > longitude=get_double_number (& buf [GetComma (5jinbuf)]); / * longitude * /

GPS- > EW = buf [GetComma (6jinbuf)]; / * East-West Classic * /

UTC2BTC (& GPS- > D); / * time to Beijing * /

}

If (clockwork A') / * "$GPGGA" * /

{

GPS- > high = get_double_number (& buf [GetComma (9 minutes Buf)])

GPS- > num = get_int_number (& buf [GetComma (7djinbuf)])

}

}

Voidshow_gps (GPS_INFO * GPS)

{

Printf ("year:% ld-d-d\ n", GPS- > D.Yearbook GPS-> D.monthJournal GPS-> D.day)

Printf ("time: d:d:d\ n", GPS- > D.hourGPS-> D.minutewonGPS-> D.second)

Printf ("Latitude:% S.4F\ n", (GPS- > NS=='N')? "North Latitude": "South Latitude", GPS- > latitude)

Printf ("longitude:% S.4F\ n", (GPS- > EW=='W')? "West longitude": "East longitude", GPS- > longitude)

Printf ("number of satellites: d\ n", GPS- > num)

Printf ("height:% .4f\ n", GPS- > high)

Printf ("status:% s\ n", (GPS- > status=='A')? "location": "Navigation")

Printf ("-\ n")

}

Int set_opt (int fd,int nSpeed, int nBits, char nEvent, int nStop)

{

Struct termios newtio,oldtio

If (tcgetattr (fd,&oldtio)! = 0) {

Perror ("SetupSerial 1")

Return-1

}

Bzero (& newtio, sizeof (newtio))

Newtio.c_cflag | = CLOCAL | CREAD

Newtio.c_cflag & = ~ CSIZE

Switch (nBits)

{

Case 7:

Newtio.c_cflag | = CS7

Break

Case 8:

Newtio.c_cflag | = CS8

Break

}

Switch (nEvent)

{

Case'oclinic:

Case'Oliver:

Newtio.c_cflag | = PARENB

Newtio.c_cflag | = PARODD

Newtio.c_iflag | = (INPCK | ISTRIP)

Break

Case'ebacks:

Case'Emission:

Newtio.c_iflag | = (INPCK | ISTRIP)

Newtio.c_cflag | = PARENB

Newtio.c_cflag & = ~ PARODD

Break

Case'nails:

Case'Night:

Newtio.c_cflag & = ~ PARENB

Break

Case's website:

Case'Smits:

Newtio.c_cflag & = ~ PARENB

Newtio.c_cflag & = ~ CSTOPB

Break

Default:

Printf ("Unsupported parity\ n")

Return-1

}

Switch (nSpeed)

{

Case 2400:

Cfsetispeed (& newtio, B2400)

Cfsetospeed (& newtio, B2400)

Break

Case 4800:

Cfsetispeed (& newtio, B4800)

Cfsetospeed (& newtio, B4800)

Break

Case 9600:

Cfsetispeed & newtio, B9600)

Cfsetospeed & newtio, B9600)

Break

Case 115200:

Cfsetispeed & newtio, B115200)

Cfsetospeed & newtio, B115200)

Break

Case 460800:

Cfsetispeed (& newtio, B460800)

Cfsetospeed (& newtio, B460800)

Break

Default:

Cfsetispeed & newtio, B9600)

Cfsetospeed & newtio, B9600)

Break

}

If (nStop = = 1)

Newtio.c_cflag & = ~ CSTOPB

Else if (nStop = = 2)

Newtio.c_cflag | = CSTOPB

Newtio.c_ cc [VTIME] = 0 position hand * timeout important * /

Newtio.c_ cc [VMIN] = 100 * the minimum returned value is important * /

Tcflush (fd,TCIFLUSH)

If ((tcsetattr (fd,TCSANOW,&newtio))! = 0)

{

Perror ("com set error")

Return-1

}

Return 0

}

Int Get_GPSData (char* gpd,char* buf,char* tar_value)

{

Char * pos = NULL

Int cur = 0, countermeasure 1, I = 0

Pos = strstr (buf,gpd)

If (pos = = NULL) {

Return counter

}

Cur = pos-buf

Tar_value [0] ='$'

For (I = cur; I

< BUFLEN; i++){ if(buf[i]!='$' && buf[i]!='\n' ){ tar_value[counter]=buf[i]; counter++; } else{ tar_value[counter]='\0'; break; } } return counter; } int main(void) { GPS_INFO GPS; int fd1,nset1,nread; char buf[BUFLEN],tar_value[512]; fd1 = open(TTYUSB, O_RDWR);/*打开串口*/ if (fd1 == -1) exit(1); nset1 = set_opt(fd1,BAUD_RATE, DATA_BITS, NEVENT, NSTOP);/*设置串口属性*/ if (nset1 == -1) exit(1); while(1) { memset(buf,0,BUFLEN); memset(tar_value,0,sizeof(tar_value)); nread = read(fd1, buf, BUFLEN);/*读串口*/ if (nread >

0) {

If (Get_GPSData ("GPGGA", buf,tar_value)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 242

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report