gps示例代码
- /* main.c */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <getopt.h>
- #include "gps.h"
- #include "serial.h"
- #define GPS_LEN 1024
- static char *dev_name = "/dev/ttyS2";
- static int baud_rate = ;
- static unsigned int loop_time = ;
- static struct option opts[] = {
- {"device", required_argument, NULL, 'd'},
- {"baud", required_argument, NULL, 'b'},
- {"loop", required_argument, NULL, 'l'},
- {"help", no_argument, NULL, 'h'},
- };
- static void parse_cmd(int argc, char *argv[])
- {
- int ch;
- while ((ch = getopt_long(argc, argv, "d:b:l:h", opts, NULL)) != -) {
- switch (ch) {
- case 'd':
- //printf("dev_name: %s\n", optarg);
- dev_name = optarg;
- break;
- case 'b':
- //printf("baud_rate: %s\n", optarg);
- baud_rate = atoi(optarg);
- break;
- case 'l':
- //printf("loop_time: %s\n", optarg);
- loop_time = atoi(optarg);
- break;
- case 'h':
- printf("Usage: %s -d dev_name -b baud_rate\n", argv[]);
- printf("like:\n");
- printf("\t %s -d /dev/ttyS2\n", argv[]);
- printf("\t %s --device /dev/ttyS2 -b 9600\n", argv[]);
- break;
- default:
- printf("Unknown option or invalid format!\n");
- printf("Pls: %s --help for more info\n", argv[]);
- break;
- }
- }
- }
- int main(int argc, char *argv[])
- {
- int fd=;
- int cnt=;
- char buff[GPS_LEN];
- parse_cmd(argc, argv);
- printf("dev_name=%s, baud_rate=%d\n", dev_name, baud_rate);
- fd = open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY);
- if(fd < ) {
- printf("Can't Open %s\n", dev_name);
- return -;
- }
- set_serial(fd, baud_rate, , 'N', );
- while(loop_time--) {
- memset(buff, , GPS_LEN);
- sleep();
- cnt = read(fd, buff, GPS_LEN);
- if(cnt<) {
- printf("read error\n");
- return -;
- }
- printf("read cnt: %d, as fallow:\n%s\n", cnt, buff);
- if(cnt)
- gps_analyse(buff);
- }
- close(fd);
- return ;
- }
main.c
- #include <string.h>
- #include <stdio.h>
- #include "gps.h"
- void print_gprmc(struct gprmc *gprmc_data)
- {
- printf("===================== GPRMC ====================\n");
- printf("== GPS state bit : %c [A:有效状态 V:无效状态]\n", gprmc_data->pos_state);
- printf("== GPS mode bit : %c [A:自主定位 D:差分定位]\n", gprmc_data->mode);
- printf("== Date : 20%02d-%02d-%02d \n", gprmc_data->date%,(gprmc_data->date%)/,gprmc_data->date/);
- printf("== Time : %02d:%02d:%02d \n", (gprmc_data->time/+)%,(gprmc_data->time%)/,gprmc_data->time%);
- printf("== 纬度 : 北纬:%d度%d分%d秒 \n", ((int)gprmc_data->latitude) / , (int)(gprmc_data->latitude - ((int)gprmc_data->latitude / * )), (int)(((gprmc_data->latitude - ((int)gprmc_data->latitude / * )) - ((int)gprmc_data->latitude - ((int)gprmc_data->latitude / * ))) * 60.0));
- printf("== 经度 : 东经:%d度%d分%d秒 \n", ((int)gprmc_data->longitude) / , (int)(gprmc_data->longitude - ((int)gprmc_data->longitude / * )), (int)(((gprmc_data->longitude - ((int)gprmc_data->longitude / * )) - ((int)gprmc_data->longitude - ((int)gprmc_data->longitude / * ))) * 60.0));
- printf("== 速度 : %.3f m/s \n", gprmc_data->speed);
- printf("================================================\n");
- }
- void gprmc_analyse(char *buff)
- {
- char *ptr=NULL;
- struct gprmc gprmc_data;
- /* FIXME:
- * 1. 002743.261 the last '261' will change so sometime can not match
- * 2. only attach once from buff, maybe should use while(ptr != NULL) ...
- */
- /* eg: $GPRMC,002743.261,V,,,,,0.00,0.00,060180,,,N*45 */
- if(NULL != (ptr=strstr(buff, "$GPRMC"))) {
- sscanf(ptr, "$GPRMC,%d.261,%c,%f,N,%f,E,%f,%f,%d,,,%c*",
- &(gprmc_data.time), &(gprmc_data.pos_state), &(gprmc_data.latitude),
- &(gprmc_data.longitude), &(gprmc_data.speed), &(gprmc_data.direction),
- &(gprmc_data.date), &(gprmc_data.mode));
- print_gprmc(&gprmc_data);
- }
- }
- void gps_analyse(char *buff)
- {
- gprmc_analyse(buff);
- //gpgsv_analyse(buff); // do more type parse
- }
gps.c
- #ifndef __GPS_H__
- #define __GPS_H__
- struct gprmc
- {
- unsigned int time;/* gps定位时间 */
- char pos_state;/*gps状态位*/
- float latitude;/*纬度 */
- float longitude;/* 经度 */
- float speed; /* 速度 */
- float direction;/*航向 */
- unsigned int date; /*日期 */
- float declination; /* 磁偏角 */
- char dd;
- char mode;/* GPS模式位 */
- };
- extern void gps_analyse(char *buff);
- #endif
gps.h
- #include <stdio.h>
- #include <termios.h>
- #include <stdlib.h>
- #include <string.h>
- #include "serial.h"
- int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop)
- {
- struct termios tty_cfg;
- memset(&tty_cfg, , sizeof(tty_cfg));
- tty_cfg.c_cflag |= (CLOCAL|CREAD); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
- tty_cfg.c_cflag &= ~CSIZE; /* 设置数据位 */
- switch(baud_rate) {
- case :
- cfsetispeed(&tty_cfg, B2400);
- cfsetospeed(&tty_cfg, B2400);
- break;
- case :
- cfsetispeed(&tty_cfg, B4800);
- cfsetospeed(&tty_cfg, B4800);
- break;
- case :
- cfsetispeed(&tty_cfg, B9600);
- cfsetospeed(&tty_cfg, B9600);
- break;
- case :
- cfsetispeed(&tty_cfg, B115200);
- cfsetospeed(&tty_cfg, B115200);
- break;
- default:
- cfsetispeed(&tty_cfg, B9600);
- cfsetospeed(&tty_cfg, B9600);
- break;
- }
- switch(nBits) {
- case :
- tty_cfg.c_cflag |= CS7;
- break;
- case :
- tty_cfg.c_cflag |= CS8;
- break;
- }
- switch(nEvent) {
- case '': /* 奇校验 */
- tty_cfg.c_cflag |= PARENB; /* 开启奇偶校验 */
- tty_cfg.c_iflag |= (INPCK | ISTRIP); /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特 */
- tty_cfg.c_cflag |= PARODD; /*启用奇校验(默认为偶校验)*/
- break;
- case 'E': /*偶校验*/
- tty_cfg.c_cflag |= PARENB; /*开启奇偶校验 */
- tty_cfg.c_iflag |= ( INPCK | ISTRIP); /*打开输入奇偶校验并去除字符第八个比特*/
- tty_cfg.c_cflag &= ~PARODD; /*启用偶校验*/
- break;
- case 'N': /*无奇偶校验*/
- tty_cfg.c_cflag &= ~PARENB;
- break;
- }
- /* 设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB */
- if(nStop == )
- tty_cfg.c_cflag &= ~CSTOPB; /*默认为一位停止位; */
- else if( nStop == )
- tty_cfg.c_cflag |= CSTOPB; /* CSTOPB表示送两位停止位 */
- /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/
- tty_cfg.c_cc[VTIME] = ; /* 非规范模式读取时的超时时间;*/
- tty_cfg.c_cc[VMIN] = ; /* 非规范模式读取时的最小字符数*/
- tcflush(fd, TCIFLUSH); /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
- /*激活配置使其生效*/
- if((tcsetattr(fd, TCSANOW, &tty_cfg)) != ) {
- printf("com set error");
- exit();
- }
- return ;
- }
serial.c
- #ifndef __FUZK_SERIAL_H__
- #define __FUZK_SERIAL_H__
- extern int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop);
- #endif
serial.h
- CC = /opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-gcc
- CFLAGS = -O1
- TARGET = gps_test
- OBJS = serial.o gps.o main.o
- $(TARGET): $(OBJS)
- $(CC) -o $(TARGET) $(OBJS) $(CFLAGS)
- $(OBJS): %o:%c
- $(CC) -c $(CFLAGS) $< -o $@
- clean:
- rm -f $(TARGET) $(OBJS)
Makefile
gps示例代码的更多相关文章
- 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token
1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- ActiveMQ笔记(1):编译、安装、示例代码
一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...
- C#微信公众平台接入示例代码
http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 这是微信公众平台提供的接入指南.官网只提供了php的示例代码 ...
- 编译opengl编程指南第八版示例代码通过
最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...
- 股票数据调用示例代码php
<!--?php // +---------------------------------------------------------------------- // | JuhePHP ...
- php示例代码之类似于C#中的String.Format方法
php示例代码之类似于C#中的String.Format方法 原文来自于 http://stackoverflow.com/questions/1241177/c-string-format-equ ...
- redis 学习笔记(2)-client端示例代码
redis提供了几乎所有主流语言的client,java中主要使用二种:Jedis与Redisson 一.Jedis的使用 <dependency> <groupId>redi ...
- 正则表达式学习笔记(附:Java版示例代码)
具体学习推荐:正则表达式30分钟入门教程 . 除换行符以外的任意字符\w word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s space,空白符 ...
随机推荐
- Starting Jenkins bash: /usr/bin/java: 没有那个文件或目录
[root@localhost /]# systemctl status jenkins.service ● jenkins.service - LSB: Jenkins Automation Ser ...
- sublime的注册方法 非常好用
摘自:https://blog.csdn.net/weixin_42444922/article/details/81006107 转载 阿东的天空之城 发布于2018-07-11 20:03:43 ...
- Linux下安装{Git}
环境 { "操作系统":"CentOS 7.5 64位", "CPU":"1核", "内存":&qu ...
- Springboot中IDE支持两种打包方式,即jar包和war包
Springboot中IDE支持两种打包方式,即jar包和war包 打包之前修改pom.xml中的packaging节点,改为jar或者war 在项目的根目录执行maven 命令clean pa ...
- sql简单存储过程分享
很多程序员朋友都视sql为洪湖水猛兽,其实深入分析一下,多用些时间与耐心,sql还是可以理解的. 本文主要是针对刚刚接触sql的新手朋友,进行一个sql存储过程的简单分享. 小子第一次发布文章,也是借 ...
- 局域网-断网&劫持(kali)
1.查看局域网中的主机 fping –asg 192.168.1.0/24 2.断网 arpspoof -i wlan0 -t 192.168.100 192.168.1.1 (arpspoof - ...
- python快速开始一-------常见的数据类型
前置条件:已经安装好python程序 常用的数据类型 Python常见的数据类型: 整数:就是我们数学里面的整数,比如3455,python目前支持最大的32bit或者64bit 长整数(long): ...
- 53 容器(八)——TreeMap 红黑树
红黑树是比较难以理解的一种数据结构.它能从10亿数据中进行10几次比较就能查找到需要的数据.效率非常高. 理解起内部结构也难. 现阶段我们知道有这种东西就行了. 参考文章: https://www.j ...
- Vue框架(一)——Vue导读、Vue实例(挂载点el、数据data、过滤器filters)、Vue指令(文本指令v-text、事件指令v-on、属性指令v-bind、表单指令v-model)
Vue导读 1.Vue框架 vue是可以独立完成前后端分离式web项目的js框架 三大主流框架之一:Angular.React.Vue vue:结合其他框架优点.轻量级.中文API.数据驱动.双向绑定 ...
- php GD 和图像处理函数, 制作一张图片
php GD 和图像处理函数, 制作一张图片 // GD 和图像处理函数 // https://www.php.net/manual/zh/ref.image.php // https://www.p ...