安信可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

相关文章

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

Python获取浏览器Cookies的四种方式小结

《Python获取浏览器Cookies的四种方式小结》在进行Web应用程序测试和开发时,获取浏览器Cookies是一项重要任务,本文我们介绍四种用Python获取浏览器Cookies的方式,具有一定的... 目录什么是 Cookie?1.使用Selenium库获取浏览器Cookies2.使用浏览器开发者工具

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取