linux下编程实现GPS数据获取与解析(2)

参考网址:http://blog.csdn.net/zouleideboke/article/details/73521122


*************************************************************************************************************************************************

开发板:fl2440

开发模块:A7(GPRS/GPS)

*************************************************************************************************************************************************

前言:我使用的开发模块是安信可公司研发的A7模块,同时具有GPRS与GPS功能,要注意的是它出的是TTL电平,所以使用USB转串口线,得支持这样的电平才行。

一,硬件连接打开GPS及测试

我使用的USB转串口线上的芯片是cp210的,所以在内核配置的时候,必须把这个支持选上,否则,开发板不识别。

[[email protected] Linux-3.0]$ make menuconfig

[plain] view plain copy
  1. make menuconfig  
  2.     Device Drivers->  
  3.         [*]USB support ->  
  4.             [*]USB Serial Converter supportUSB CP210x family of UART Bridge Controllers  
make编译之后重新烧录到开发板,开发板即可识别USB转串口线。

插上USB转串口线后,开发板上/dev/目录下出现ttyUSB0设备,说明识别成功。

linux下编程实现GPS数据获取与解析(2)

1.将串口AT指令控制发送端(U_TXD)和串口AT指令控制接收端(U_RXD)分别与USB转TTL转接头上的RXD和TXD相连,GND与GND相连。另一端接入

开发板的USB口即可。

2.网线连接PC与开发板,使用putty软件远程登录开发板,至于怎么连接我前面有篇博客有介绍,可以参考一下:点击打开链接

3.将A7连上开发板以后,在开发板上使用microcom命令监听相关串口,USB转串口芯片连接则监听串口/dev/ttyUSB0。然后把GPS功能打开:

[plain] view plain copy
  1. >: microcom -s 115200 /dev/ttyUSB0   
  2. AT   //检测模块是否正常  
  3. OK   //正常回显OK  
  4. AT+GPS=1   //打开GPS功能  
  5. OK  
4.此时将A7模块上的U_TXD连线切换到A7模块上的GPS_TXD引脚,其他连线不变。以波特率为9600重新打开监听串口,将会不断的收到GPS定位的数据。

linux下编程实现GPS数据获取与解析(2)
这时候会发现GPS已经正常工作了。

二,GPS数据解析

在编程实现获取GPS数据之前,先了解一下上面的数据分别表示什么意思。

如:$GPRMC,131913.000,A,3029.64972,N,11423.62352,E,0.00,0.00,200617,,,A*67

字段0:$GPRMC,语句ID,表明该语句为Recommended Minimum Specific GPS/TRANSIT Data(RMC)推荐最小定位信息

字段1:UTC时间,hhmmss.sss格式(UTC这个是格林威治时间即世界时间

字段2:状态,A=定位,V=未定位

字段3:纬度ddmm.mmmm,度分格式(前导位数不足则补0)

字段4:纬度N(北纬)或S(南纬)

字段5:经度dddmm.mmmm,度分格式(前导位数不足则补0)

字段6:经度E(东经)或W(西经)

字段7:速度,节,Knots

字段8:方位角,度

字段9:UTC日期,DDMMYY格式

字段10:磁偏角,(000 - 180)度(前导位数不足则补0)

字段11:磁偏角方向,E=东W=西

字段12:模式,A=自动,D=差分,E=估测,N=数据无效(3.0协议内容)

字段13:校验值($与*之间的数异或后的值)

因为本次编程要获取的就是$GPRMC......这一行的信息,所以只分析这一行的,其他的百度百科有介绍,就不一一解释了。

三,编程获取信息

1.串口配置

由于我们是通过串口来进行数据传输,也就是把GPS定位的信息,通过串口最后输出到我们的终端设备上。

[cpp] view plain copy
  1. /********************************************************************************* 
  2.  *      Copyright:  (C) 2017 zoulei 
  3.  *                  All rights reserved. 
  4.  * 
  5.  *       Filename:  uart1.c 
  6.  *    Description:  This file 
  7.  * 
  8.  *        Version:  1.0.0(2017年06月19日) 
  9.  *         Author:  zoulei <[email protected]> 
  10.  *      ChangeLog:  1, Release initial version on "2017年06月19日 16时41分59秒" 
  11.  * 
  12.  ********************************************************************************/  
  13. #include <stdio.h>  
  14. #include <errno.h>  
  15. #include <sys/stat.h>  
  16. #include <fcntl.h>   /* 文件控制定义*/  
  17. #include <termios.h> /* PPSIX 终端控制定义*/  
  18. #include <stdlib.h>  
  19. #include <string.h>  
  20. #include <sys/types.h>  
  21. #include <unistd.h>  
  22.   
  23.   
  24. int set_serial(int fd,int nSpeed,int nBits,char nEvent,int nStop)  
  25. {  
  26.     struct termios newttys1,oldttys1;  
  27.   
  28.      /*保存原有串口配置*/  
  29.      if(tcgetattr(fd,&oldttys1)!=0)  
  30.      {  
  31.           perror("Setupserial 1");  
  32.           return -1;  
  33.      }  
  34.      memset(&newttys1,0,sizeof(newttys1));/* 先将新串口配置清0 */  
  35.      newttys1.c_cflag|=(CLOCAL|CREAD ); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */  
  36.   
  37.      newttys1.c_cflag &=~CSIZE;/* 设置数据位 */  
  38.      /* 数据位选择 */  
  39.      switch(nBits)  
  40.      {  
  41.          case 7:  
  42.              newttys1.c_cflag |=CS7;  
  43.              break;  
  44.          case 8:  
  45.              newttys1.c_cflag |=CS8;  
  46.              break;  
  47.      }  
  48.      /* 设置奇偶校验位 */  
  49.      switch( nEvent )  
  50.      {  
  51.          case '0':  /* 奇校验 */  
  52.              newttys1.c_cflag |= PARENB;/* 开启奇偶校验 */  
  53.              newttys1.c_iflag |= (INPCK | ISTRIP);/*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特  */  
  54.              newttys1.c_cflag |= PARODD;/*启用奇校验(默认为偶校验)*/  
  55.              break;  
  56.          case 'E':/*偶校验*/  
  57.              newttys1.c_cflag |= PARENB; /*开启奇偶校验  */  
  58.              newttys1.c_iflag |= ( INPCK | ISTRIP);/*打开输入奇偶校验并去除字符第八个比特*/  
  59.              newttys1.c_cflag &= ~PARODD;/*启用偶校验*/  
  60.              break;  
  61.          case 'N'/*无奇偶校验*/  
  62.              newttys1.c_cflag &= ~PARENB;  
  63.              break;  
  64.      }  
  65.      /* 设置波特率 */  
  66.     switch( nSpeed )  
  67.     {  
  68.         case 2400:  
  69.             cfsetispeed(&newttys1, B2400);  
  70.             cfsetospeed(&newttys1, B2400);  
  71.             break;  
  72.         case 4800:  
  73.             cfsetispeed(&newttys1, B4800);  
  74.             cfsetospeed(&newttys1, B4800);  
  75.             break;  
  76.         case 9600:  
  77.             cfsetispeed(&newttys1, B9600);  
  78.             cfsetospeed(&newttys1, B9600);  
  79.             break;  
  80.         case 115200:  
  81.             cfsetispeed(&newttys1, B115200);  
  82.             cfsetospeed(&newttys1, B115200);  
  83.             break;  
  84.         default:  
  85.             cfsetispeed(&newttys1, B9600);  
  86.             cfsetospeed(&newttys1, B9600);  
  87.             break;  
  88.     }  
  89.      /*设置停止位*/  
  90.     if( nStop == 1)/* 设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则**CSTOPB */  
  91.     {  
  92.         newttys1.c_cflag &= ~CSTOPB;/*默认为一位停止位; */  
  93.     }  
  94.     else if( nStop == 2)  
  95.     {  
  96.         newttys1.c_cflag |= CSTOPB;/* CSTOPB表示送两位停止位 */  
  97.     }  
  98.   
  99.     /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/  
  100.     newttys1.c_cc[VTIME] = 0;/* 非规范模式读取时的超时时间;*/  
  101.     newttys1.c_cc[VMIN]  = 0; /* 非规范模式读取时的最小字符数*/  
  102.     tcflush(fd ,TCIFLUSH);/* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */  
  103.   
  104.      /***配置使其生效*/  
  105.     if((tcsetattr( fd, TCSANOW,&newttys1))!=0)  
  106.     {  
  107.         perror("com set error");  
  108.         exit(1);  
  109.     }  
  110.   
  111.     return 0;  
  112. }  
******************************************************************************************************************************************************************************
这个代码段最重要的就是这个结构体

[cpp] view plain copy
  1. struct termios  
  2.       {  
  3.       tcflag_t  c_iflag;  //输入选项  
  4.       tcflag_t  c_oflag;  //输出选项  
  5.       tcflag_t  c_cflag;  //控制选项  
  6.       tcflag_t  c_lflag;  //行选项  
  7.       cc_t      c_cc[NCCS]; //控制字符  
  8.       };   
其中我们更关注的是c_cflag控制选项。其中包含了波特率、数据位、校验位、停止位的设置。 
它可以支持很多常量名称其中设置数据传输率为相应的数据传输率前要加上“B”

*********************************************************************************************************************************

2.gps数据分析

gps_analyse.c:

[cpp] view plain copy
  1. /********************************************************************************* 
  2.  *      Copyright:  (C) 2017 zoulei 
  3.  *                  All rights reserved. 
  4.  * 
  5.  *       Filename:  gps_analyse.c 
  6.  *    Description:  This file 
  7.  * 
  8.  *        Version:  1.0.0(2017年06月19日) 
  9.  *         Author:  zoulei <[email protected]> 
  10.  *      ChangeLog:  1, Release initial version on "2017年06月19日 12时16分39秒" 
  11.  * 
  12.  ********************************************************************************/  
  13. #include <string.h>  
  14. #include <stdio.h>  
  15. #include <errno.h>  
  16. #include "gps.h"  
  17.   
  18. int gps_analyse (char *buff,GPRMC *gps_data)  
  19. {  
  20.     char *ptr=NULL;  
  21.      if(gps_data==NULL)  
  22.       {  
  23.          return -1;  
  24.       }  
  25.       if(strlen(buff)<10)  
  26.       {  
  27.          return -1;  
  28.       }  
  29. /* 如果buff字符串中包含字符"$GPRMC"则将$GPRMC的地址赋值给ptr */  
  30.       if(NULL==(ptr=strstr(buff,"$GPRMC")))  
  31.       {  
  32.          return -1;  
  33.       }  
  34. /* sscanf函数为从字符串输入,意思是将ptr内存单元的值作为输入分别输入到后面的结构体成员 */  
  35.       sscanf(ptr,"$GPRMC,%d.000,%c,%f,N,%f,E,%f,%f,%d,,,%c*",&(gps_data->time),&(gps_data->pos_state),&(gps_data->latitude),&(gps_data->longitude),&(gps_data->speed),&(gps_data->direction),&(gps_data->date),&(gps_data->mode));  
  36.       return 0;  
  37. }  
  38.   
  39. int print_gps (GPRMC *gps_data)  
  40. {  
  41.     printf("                                                           \n");  
  42.     printf("                                                           \n");  
  43.     printf("===========================================================\n");  
  44.     printf("==                   全球GPS定位导航模块                 ==\n");  
  45.     printf("==              Author:zoulei                           ==\n");  
  46.     printf("==              Email:[email protected]               ==\n");  
  47.     printf("==              Platform:fl2440                         ==\n");  
  48.     printf("===========================================================\n");  
  49.     printf("                                                           \n");  
  50.     printf("===========================================================\n");  
  51.     printf("==   GPS state bit : %c  [A:有效状态 V:无效状态]              \n",gps_data->pos_state);  
  52.     printf("==   GPS mode  bit : %c  [A:自主定位 D:差分定位]               \n", gps_data->mode);  
  53.     printf("==   Date : 20%02d-%02d-%02d                                  \n",gps_data->date%100,(gps_data->date%10000)/100,gps_data->date/10000);  
  54.     printf("==   Time : %02d:%02d:%02d                                   \n",(gps_data->time/10000+8)%24,(gps_data->time%10000)/100,gps_data->time%100);  
  55.     printf("==   纬度 : 北纬:%d度%d分%d秒                              \n", ((int)gps_data->latitude) / 100, (int)(gps_data->latitude - ((int)gps_data->latitude / 100 * 100)), (int)(((gps_data->latitude - ((int)gps_data->latitude / 100 * 100)) - ((int)gps_data->latitude - ((int)gps_data->latitude / 100 * 100))) * 60.0));  
  56.     printf("==   经度 : 东经:%d度%d分%d秒                              \n", ((int)gps_data->longitude) / 100, (int)(gps_data->longitude - ((int)gps_data->longitude / 100 * 100)), (int)(((gps_data->longitude - ((int)gps_data->longitude / 100 * 100)) - ((int)gps_data->longitude - ((int)gps_data->longitude / 100 * 100))) * 60.0));  
  57.     printf("==   速度 : %.3f  m/s                                      \n",gps_data->speed);  
  58.     printf("==                                                       \n");  
  59.     printf("============================================================\n");  
  60.   
  61.     return 0;  
  62. }  
*************************************************************************************************************************

说明:1.由于我直接获取的是格林威治时间即世界时间(UTC),所以要把它转换成北京时间(BTC),也就是在这个时间

基础上加8个小时。

           2. 经纬度,GPRMC返回的纬度数据位ddmm.mmmm格式即度分格式,我们把它转换成常见的度分秒的格式,计算方法:如接收到的纬度是:3029.60430 
3029.60430/100=30.2960430可以直接读出30度, 3029.60430–30*100=29.60430, 可以直接读出29分 
(29.60430–29)*60 =0.60430*60=36.258读出36秒, 所以纬度是:30度29分36秒。
             3.GPRMC返回的速率值是海里/时,单位是节,把它转换成千米/时,换算为:1海里=1.85公里,把得到的速率乘以1.85。

           4.航向指的是偏离正北的角度 .

           5.GPRMC的日期格式为:ddmmyy,如:200617表示2017年06月20日,这个日期是准确的,不需要转换.

************************************************************************************************************************************

3.主函数打开串口设备读操作

main.c:

[cpp] view plain copy
  1. /********************************************************************************* 
  2.  *      Copyright:  (C) 2017 zoulei 
  3.  *                  All rights reserved. 
  4.  * 
  5.  *       Filename:  main.c 
  6.  *    Description:  This file 
  7.  * 
  8.  *        Version:  1.0.0(2017年06月19日) 
  9.  *         Author:  zoulei <[email protected]> 
  10.  *      ChangeLog:  1, Release initial version on "2017年06月19日 14时55分30秒" 
  11.  * 
  12.  ********************************************************************************/  
  13. #include <sys/types.h>  
  14. #include <sys/stat.h>  
  15. #include <fcntl.h>  
  16. #include <errno.h>  
  17. #include <stdio.h>  
  18. #include <string.h>  
  19. #include "gps.h"  
  20. #define GPS_LEN 1024  
  21. int set_serial(int fd,int nSpeed, int nBits, char nEvent, int nStop);  
  22. int gps_analyse(char *buff,GPRMC *gps_data);  
  23. int print_gps(GPRMC *gps_data);  
  24.   
  25. int main (int argc, char **argv)  
  26. {  
  27.     int fd=0;  
  28.     int n=0;  
  29.     GPRMC gprmc;  
  30.     char buff[GPS_LEN];  
  31.     char *dev_name="/dev/ttyUSB0";  
  32.   
  33.     if((fd=open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY))<0)  
  34.     {  
  35.             perror("Can't Open the ttyUSB0 Serial Port");  
  36.             return -1;  
  37.     }  
  38.     set_serial( fd,9600,8,'N',1);  
  39.   
  40.     while(1)  
  41.     {  
  42.        sleep(2);  
  43.        if((n=read(fd,buff,sizeof(buff)))<0)  
  44.         {  
  45.            perror("read error");  
  46.            return -1;  
  47.         }  
  48.         printf("buff:%s\n",buff);  
  49.         memset(&gprmc, 0 , sizeof(gprmc));  
  50.         gps_analyse(buff,&gprmc);  
  51.         print_gps(&gprmc);  
  52.   
  53.     }  
  54.     close(fd);  
  55.     return 0;  
  56. }  
*************************************************************************************************************************

说明:这段代码:fd=open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY。是通过open系统调用打开串口设备ttyUSB0,
O_NOCTTY:是为了告诉linux这个程序不会成为这个端口上的“控制终端”.如果不这样做的话,所有的输入,比如键盘上过来的Ctrl+C中止信号等等,会影响到你的进程。 
O_NDELAY:这个标志则是告诉Linux这个程序并不关心DCD信号线的状态,也就是不管串口是否有数据到来,都是非阻塞的,程序继续执行。

******************************************************************************************************************************

gps.h:

[cpp] view plain copy
  1. /******************************************************************************** 
  2.  *      Copyright:  (C) 2017 zoulei 
  3.  *                  All rights reserved. 
  4.  * 
  5.  *       Filename:  gps.h 
  6.  *    Description:  This head file 
  7.  * 
  8.  *        Version:  1.0.0(2017年06月19日) 
  9.  *         Author:  zoulei <[email protected]> 
  10.  *      ChangeLog:  1, Release initial version on "2017年06月19日 12时25分59秒" 
  11.  * 
  12.  ********************************************************************************/  
  13.   
  14. #ifndef __GPS_H__  
  15. #define __GPS_H__  
  16.   
  17. typedef unsigned int UINT;  
  18. typedef int BYTE;  
  19.   
  20. typedef struct __gprmc__  
  21. {  
  22.    UINT time;/* gps定位时间 */  
  23.    char pos_state;/*gps状态位*/  
  24.    float latitude;/*纬度 */  
  25.    float longitude;/* 经度 */  
  26.    float speed; /* 速度 */  
  27.    float direction;/*航向 */  
  28.    UINT date;  /*日期  */  
  29.    float declination; /* 磁偏角 */  
  30.    char dd;  
  31.    char mode;/* GPS模式位 */  
  32.   
  33. }GPRMC;  
  34. extern int set_option(int fd,int nSpeed, int nBits, char nEvent, int nStop);  
  35. extern int print_gps(GPRMC *gps_date);  
  36. extern int gps_analyse(char *buff,GPRMC *gps_data);  
  37. #endif  
4.编写Makefile

[cpp] view plain copy
  1. CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc  
  2.   
  3. objs=uart1.o gps_analyse.o main.o  
  4. srcs=uart1.c gps_analyse.c main.c  
  5.   
  6. gps_test: $(objs)  
  7.         $(CC) -o gps_test $(objs)  
  8.         @make clean  
  9.   
  10. main.o: $(srcs) gps.h  
  11.         $(CC) -c  $(srcs)  
  12.   
  13. uart1.o:  uart1.c  
  14.         $(CC) -c  uart1.c  
  15.   
  16. analyse_gps.o: gps_analyse.c gps.h  
  17.         $(CC) -c  gps_analyse.c  
  18.   
  19. clean:  
  20.         rm *.o  
make编译之后,生成gps_test可执行文件,当然这个名字可根据自己的爱好在Makefile里设置成自己想要的名字,然后将gps_test文件下载到开发板给予777

权限,然后运行测试.

linux下编程实现GPS数据获取与解析(2)

5.测试

测试结果如图:

linux下编程实现GPS数据获取与解析(2)




分类:

技术点:

相关文章: