C language decoding GPS-Implementation

Source: Internet
Author: User

I have finally decoded all the GPS data through my efforts over the past few days. I believe that people who have read my logs have been waiting for a long time. Only by sharing resources can we make progress; compared with the direct call interface on the internet is different from the c direction to solve the problem can make the problem more clear, this design chip uses the CC550-BG module of the star, to meet the performance of most chips, the system is in the Linux system below implementation, the choice of ttys0 node, according to different excuses, the choice of Serial Line is RS-232, I believe you also understand, For details, please see my code, all of them have a detailed explanation, and I hope to help you. Here I only parse the codes in the format of GPRS MC. You can also parse the codes in other formats as long as they are in read_data () in the function, you can change the name of the strncmp () array. You can write it as a switch-Format Decoding selector. If you do not need to talk about it, paste my code!

# Include <stdio. h>/* standard input/output definition */# include <stdlib. h>/* standard function library definition */# include <unistd. h>/* UNIX standard function definition */# include <sys/types. h> # include <sys/STAT. h> # include <errno. h> # include <string. h> # include <fcntl. h> # include <termios. h> // # define Dev "/dev/ttys0" # define buff_size 512 # define max_com_num 3int sectionid = 0, I = 0; struct data {char gps_time [20]; // UTC time char gps_sv; // use the satellite char gps_wd [12]; // latitude char gps_jd [12]; // Longitude // char gps_warn; // location warning char gps_speed [5]; // speed char gps_date [8]; // UTC date} gps_data; int set_com_config (int fd, int baud_rate, int data_bits, char parity, int stop_bits) {struct termios new_cfg, old_cfg; int speed; // save and test the existing serial port parameter settings. If the serial port number is incorrect, there will be related error information if (tcgetattr (FD, & old_cfg )! = 0) {perror ("tcgetattr"); Return-1 ;}tcflush (FD, tcioflush); new_cfg = old_cfg; cfmakeraw (& new_cfg ); // configure the original mode new_cfg.c_cflag & = ~ Csize; // set the baud rate switch (baud_rate) {Case 2400: {speed = b2400; break;} case 4800: {speed = b4800; break;} case 9600: {speed = b9600; break;} case 19200: {speed = b19200; break;} case 38400: {speed = b38400; break;} case 115200: {speed = b115200; break ;}} cfsetispeed (& new_cfg, speed); cfsetospeed (& new_cfg, speed); // set the data bit switch (data_bits) {Case 7: {new_cfg.c_cflag | = cs7; break;} case 8: {new_cfg.c_cflag | = cs8; break;} // sets the stop bit switch (Stop_bits) {Case 1: {new_cfg.c_cflag & = ~ Cstopb; break;} Case 2: {new_cfg.c_cflag | = cstopb; break ;}// set the parity bit switch (parity) {Case 'O ': {new_cfg.c_cflag | = (parodd | parenb); new_cfg.c_iflag | = (inpck | istrip); break;} case 'E': Case 'E': {new_cfg.c_cflag | = parenb; new_cfg.c_cflag & = ~ Parodd; new_cfg.c_iflag | = (inpck | istrip); break;} case's ': Case's': {new_cfg.c_cflag & = ~ Parenb; new_cfg.c_cflag & = ~ Cstopb; break;} case 'N': Case 'N': {new_cfg.c_cflag & = ~ Parenb; new_cfg.c_iflag & = ~ Inpck; break ;}} new_cfg.c_cc [vtime] = 10; new_cfg.c_cc [Vmin] = 5; // process the unreceived characters tcflush (FD, tciflush); If (tcsetattr (FD, if, tcsanow, & new_cfg ))! = 0) {perror ("tcsetattr"); Return-1 ;}return 0 ;}// open the serial port function int open_port (INT com_port) {int FD; # If (com_type = gnr_com) // use the common serial char * Dev [] = {"/dev/ttys0", "/dev/ttys1 ", "/dev/ttys2" };# else // use USB to serial char * Dev [] = {"/dev/ttyusb0", "/dev/ttyusb1 ", "/dev/ttyusb2" };# endifif (com_port <0) | (com_port> max_com_num) {return-1 ;} // open the serial port if (FD = open (Dev [com_port-1], o_rdwr | o_noctty | o_ndelay) <0) {perror ("open serial Po RT "); Return-1;} // restores the serial port to the blocked state. If (fcntl (FD, f_setfl, 0) <0) {perror (" fcntl f_setfl \ n "); return-1;} // test whether the device is a terminal device. If (isatty (stdin_fileno) = 0) {perror ("standard input is not a terminal device ");} return FD;} void print_info (void) {// print the selection interface, that is, the guiding symbol printf ("Now the receive time is % s \ n", gps_data.gps_time ); printf ("the star is % C 3 \ n", gps_data.gps_sv); printf ("The Earth latitude is: % s \ n", gps_data.gps_wd); printf ("the E Arth longpolling is: % s \ n ", gps_data.gps_jd); printf (" the train speed is: % s km/h \ n ", gps_data.gps_speed); printf (" the date is: % s \ n ", gps_data.gps_date);} void gps_resolve_uplmc (char data) {// $ uplmc, 092427.604, V, 4002.1531, N, 11618.3097, E, 0.000, 0.00, 280812, E, N * 08if (Data = ',') {++ sectionid; I = 0;} else {Switch (sectionid) {Case 1: // 02: 48: 13gps_data.gps_time [I ++] = data; if (I = 2 | I = 5) {gps_data.gps_time [I ++] = ':';} GPS _ Data. gps_time [8] = '\ 0'; break; Case 2: If (Data = 'A') gps_data.gps_sv ='> '; elsegps_data.gps_sv =' <'; break; case 3: // 3158.4608gps _ data. gps_wd [++ I] = data; gps_data.gps_wd [12] = '\ 0'; break; Case 4: If (Data = 'n ') gps_data.gps_wd [0] = 'n'; else if (Data = 's') gps_data.gps_wd [0] = 's'; break; Case 5: // 11848.3737, egps_data.gps_jd [++ I] = data; gps_data.gps_jd [12] = '\ 0'; break; Case 6: If (Data = 'E ') gps_data.gps_jd [0] = 'E'; else if (Data = 'W ') Gps_data.gps_jd [0] = 'W'; break; Case 7: // 10.05gps _ data. gps_speed [I ++] = data; gps_data.gps_speed [4] = '\ 0'; break; Case 9: // 15-07-06-> 06-07-15gps_data.gps_date [I ++] = data; if (I = 2 | I = 5) {gps_data.gps_date [I ++] = '-';} gps_data.gps_date [8] = '\ 0'; break ;}} void read_data (INT FD) {char buffer [buff_size], Dest [1024]; char array [10] = "$ uplmc"; int res, I = 0, j = 0, K; int DATA = 1, Len = 0; memset (DEST, 0, sizeof (DEST); do {MEMS Et (buffer, 0, sizeof (buffer); // $ gprs mc, 024813.640, A, 3158.4608, N, 11848.3737, E, 10.05, 324.27, 150706 ,,, A * 50if (RES = read (FD, buffer, 1)> 0) {// parameters are continuously input here. One read data may be ($ gprs mc, 024 ), RES is the read length, And now each bit is passed into the function for processing; strcat (DEST, buffer); If (buffer [0] = '\ n') {I = 0; if (strncmp (DEST, array, 6) = 0) {printf ("% s", DEST); Len = strlen (DEST); For (k = 0; k <Len; k ++) {gps_resolve_uplmc (Dest [k]);} sectionid = 0; print_info ();} bzero (DEST, sizeof (DEST ));}}} while (1); close (FD);} int main (INT argc, char * argv []) {int FD = 0; int host_com_port = 1; FD = open_port (host_com_port ); if (FD <0) {perror ("Open fail! ");} Printf (" Open sucess! \ N "); If (set_com_config (FD, 9600,8, 'n', 1) <0) {perror (" set_com_config fail! \ N ");} printf (" the specified ed worlds are: \ n "); read_data (FD); Return 0 ;}

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.