安信可A7编程获取GPS信息

2024-01-25 16:10
文章标签 编程 获取 信息 a7 gps 安信

本文主要是介绍安信可A7编程获取GPS信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

曲终人散空愁暮,招屈亭前水车注。——刘禹锡《竞渡曲》

前面两篇文章已经描述了串口编程以及在串口调试助手下对GPS信息的解析。这篇文章主要讲怎么样在板子上通过编程来获取GPS信息。

前两篇文章地址:
串口编程:http://blog.csdn.net/peter_tang6/article/details/72790169
GPS信息解析:http://blog.csdn.net/peter_tang6/article/details/72146764

我们这个程序分三步走:
1、设置串口
2、获取GPS信息
3、提取出来

设置串口

我的串口用的是/dev/ttyUSB0,具体的串口设置程序在上一篇文章已经详细介绍过,本篇不再赘述。
这里我们使用命令先来监听我们的串口,当然,前提是我们的串口已经能使用,并且我们已经连接好模块,模块的连接方式前面已经说过http://blog.csdn.net/peter_tang6/article/details/72146764。

使用microcom命令监听GPS(GPS的波特率为9600)

这里写图片描述

获取GPS数据信息

/*********************************************************************************
    *      Copyright:  (C) 2017 tangyanjun<519656780@qq.com>
    *                  All rights reserved.
    *
    *       Filename:  test.c
    *    Description:  This file 
    *                 
    *        Version:  1.0.0(05/28/2017)
    *         Author:  tangyanjun <519656780@qq.com>*      ChangeLog:  1, Release initial version on "05/28/2017 11:58:17 AM"*                 ********************************************************************************/#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>#include "gps.h"#define GPS_LEN 512 int gps_analysis(char *buff,GPRMC *gps_date);
int print_gps(GPRMC *gps_date);
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);/*********************************************************************************  Description:   main():程序执行的入口*   Input Args:    *  Output Args:* Return Value:********************************************************************************/
int main (int argc, char **argv)
{
     int fd = 0;
     int nread = 0;     GPRMC gprmc;     char gps_buff[GPS_LEN];
     char *dev_name = "/dev/ttyUSB0";     fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY);
     if(fd < 0)
     {
         printf("open ttyS1 error!!\n");
         return -1;
     }     set_opt( fd, 9600, 8, 'N', 1);     while(1)
     {
         sleep(2);
         nread = read(fd, gps_buff, sizeof(gps_buff));
         if(nread < 0)
         {
             printf("read GPS date error!!\n");
             return -2;
         }
         printf("gps_buff: %s\n", gps_buff);         memset(&gprmc, 0 , sizeof(gprmc));
         gps_analysis(gps_buff, &gprmc);         print_gps(&gprmc);
     }    close(fd);
    return 0;
} /* ----- End of main() ----- */

附上头文件:

/**********************************************************************************      Copyright:  (C) 2017 tangyanjun<519656780@qq.com>*                  All rights reserved.**       Filename:  gps.h*    Description:  This file *                 *        Version:  1.0.0(05/28/2017)*         Author:  tangyanjun <519656780@qq.com>*      ChangeLog:  1, Release initial version on "05/28/2017 12:29:30 PM"*                 ********************************************************************************/#ifndef __GPS_H__#define __GPS_H__typedef unsigned int UINT;typedef int BYTE;typedef long int WORD;typedef struct __gprmc__{UINT time;                  //格林威治时间char pos_state;             //定位状态float latitude;             //纬度float longitude;            //经度float speed;                //移动速度float direction;            //方向UINT date;                  //日期float declination;          //磁偏角char dd;                    //磁偏角方向char mode;}GPRMC;extern int gps_analysis(char *buff, GPRMC *gps_date);extern int print_gps(GPRMC *gps_date);extern int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);#endif

提取GPS数据信息

/*********************************************************************************
    *      Copyright:  (C) 2017 tangyanjun<519656780@qq.com>
    *                  All rights reserved.
    *
    *       Filename:  analyze.c
    *    Description:  This file 
    *                 
    *        Version:  1.0.0(05/28/2017)
    *         Author:  tangyanjun <519656780@qq.com>*      ChangeLog:  1, Release initial version on "05/28/2017 12:33:11 PM"*                 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>#include "gps.h"/***************************************************************************************  Description:从GPS数据包中抽取出GPRMC最小定位信息*   Input Args:buff:GPS返回的数据包 gps_date:GPRMC信息存储结构体*  Output Args:* Return Value:*************************************************************************************/
int gps_analysis(char *buff,GPRMC *gps_date)
{
    char *ptr=NULL;    if(gps_date==NULL)
      return -1;    if(strlen(buff)<10)
      return -1;    if(NULL==(ptr=strstr(buff,"$GPRMC")))
     return -1;    sscanf(ptr,"$GPRMC,%d.000,%c,%f,N,%f,E,%f,%f,%d,,,%c*",&(gps_date->time),&(gps_date->pos_state),&(gps_date->latitude),&(gps_date->longitude),&(gps_date->speed),&(gps_date->direction),&(gps_date->date),&(gps_date->mode));return 0;
} /* ----- End of if()  ----- *//********************************************************************************************strstr(str1,str2) 函数用于在字符串str1中搜寻str2,并返回str2在str1中首次出现的地址;否则,返回NULL
sscanf()格式化字符串输入,从一个字符串中读进与指定格式相符的数据并在存入到另一个字符串中**********************************************************************************************//***************************************************************************************  Description:解析GPRMC最小定位信息,并打印到屏幕上*   Input Args:gps_date:gps_analysis函数抽取的GPRMC信息*  Output Args:* Return Value:*************************************************************************************/
int print_gps(GPRMC *gps_date)
{
    printf("                                                           \n");
    printf("                                                           \n");
    printf("===========================================================\n");
    printf("==                   全球GPS定位导航模块                 ==\n");
    printf("==              开发人员:唐延军                           ==\n");
    printf("==              邮箱:519656780@qq.com                  ==\n");
    printf("==              平台:fl2440                             ==\n");
    printf("==              GPS型号:FIT-GPS_SF2820(代替ET-312)    ==\n");
    printf("===========================================================\n");
    printf("                                                         \n");
    printf("                                                         \n");
    printf("===========================================================\n");
    printf("==                                                       \n");
    printf("==   GPS状态位 : %c  [A:有效状态 V:无效状态]             \n",gps_date->pos_state);
    printf("==   GPS模式位 : %c  [A:自主定位 D:差分定位]             \n", gps_date->mode);
    printf("==   日期 : 20%02d-%02d-%02d                             \n",gps_date->date%100,(gps_date->date%10000)/100,gps_date->date/10000);
    printf("==   时间 : %02d:%02d:%02d                               \n",(gps_date->time/10000+8)%24,(gps_date->time%10000)/100,gps_date->time%100);
    printf("==   纬度 : 北纬:%d度%d分%d秒                                    \n", ((int)gps_date->latitude) / 100, (int)(gps_date->latitude - ((int)gps_date->latitude / 100 * 100)), (int)(((gps_date->latitude - ((int)gps_date->latitude / 100 * 100)) - ((int)gps_date->latitude - ((int)gps_date->latitude / 100 * 100))) * 60.0));
    printf("==   经度 : 东经:%d度%d分%d秒                                    \n", ((int)gps_date->longitude) / 100, (int)(gps_date->longitude - ((int)gps_date->longitude / 100 * 100)), (int)(((gps_date->longitude - ((int)gps_date->longitude / 100 * 100)) - ((int)gps_date->longitude - ((int)gps_date->longitude / 100 * 100))) * 60.0));
    printf("==   速度 : %.3f                                         \n",gps_date->speed);
    printf("==                                                       \n");
    printf("===========================================================\n");
    return 0;
} /* ----- End of print_gps()  ----- */

1、我们的时间是格林威治时间,即世界时间(UTC),我们需要把它转换成北京时间,加上8个小时即可。
2、定位状态为V的时候才是有效的数据。
3、经纬度的转换:我们把它转化成度分秒格式

我们收到的是4546.40891
然后4546.40891 / 100 = 45.4640891 则读出45度
然后4546.40891 - 45 * 100 = 46.40891 则读出46分
最后46.40891 - 46 = 0.40891 * 60 = 24.5346 读出24秒
因此最后结果是:45度46分24秒。

然后我通过强制类型转换将这个计算过程用一个表达式表示出来,即上面程序中的经度和纬度的结果。

最后将前面的串口程序和这三个程序放在同一文件夹下,再附上Makefile文件:

CC =/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc
objs   =  serial.o analyze.o test.o
srcs   =  serial.c analyze.c test.cgps: $(objs)$(CC) -o gps $(objs)@make cleantest.o: $(srcs) gps.h$(CC) -c $(srcs)serial.o: serial.c$(CC) -c serial.canalyze.o: analyze.c gps.h$(CC) -c analyze.cclear:@rm  gps.PHONY: clean
clean:@rm *.o

make之后生成gps文件,传到开发板中,给其可执行权限,运行:
这里写图片描述

程序代码就不一一分析了,这里理清一下代码实现的基本流程:

首先打开我们的设备,即串口
设置我们的串口
将串口输出的内容放到一个buf中
利用strstr和sscanf函数将我们buf中的$GPRMC这一行内容提取出来,并用sscanf函数将其内容与头文件中定义的结构体信息(即这一行信息的内容)一一配对。
sscanf()格式化字符串输入,从一个字符串中读进与指定格式相符的数据并在存入到另一个字符串中;strstr(str1,str2) 函数用于在字符串str1中搜寻str2,并返回str2在str1中首次出现的地址;否则,返回NULL。
最后打印我们的信息。

这段程序主要是针对我们对数据的提取以及对数据的处理能力。

这篇关于安信可A7编程获取GPS信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/643812

相关文章

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@