1. /* main.c */
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <getopt.h>
  11. #include "gps.h"
  12. #include "serial.h"
  13.  
  14. #define GPS_LEN 1024
  15.  
  16. static char *dev_name = "/dev/ttyS2";
  17. static int baud_rate = ;
  18. static unsigned int loop_time = ;
  19. static struct option opts[] = {
  20. {"device", required_argument, NULL, 'd'},
  21. {"baud", required_argument, NULL, 'b'},
  22. {"loop", required_argument, NULL, 'l'},
  23. {"help", no_argument, NULL, 'h'},
  24. };
  25.  
  26. static void parse_cmd(int argc, char *argv[])
  27. {
  28. int ch;
  29.  
  30. while ((ch = getopt_long(argc, argv, "d:b:l:h", opts, NULL)) != -) {
  31. switch (ch) {
  32. case 'd':
  33. //printf("dev_name: %s\n", optarg);
  34. dev_name = optarg;
  35. break;
  36. case 'b':
  37. //printf("baud_rate: %s\n", optarg);
  38. baud_rate = atoi(optarg);
  39. break;
  40. case 'l':
  41. //printf("loop_time: %s\n", optarg);
  42. loop_time = atoi(optarg);
  43. break;
  44. case 'h':
  45. printf("Usage: %s -d dev_name -b baud_rate\n", argv[]);
  46. printf("like:\n");
  47. printf("\t %s -d /dev/ttyS2\n", argv[]);
  48. printf("\t %s --device /dev/ttyS2 -b 9600\n", argv[]);
  49. break;
  50. default:
  51. printf("Unknown option or invalid format!\n");
  52. printf("Pls: %s --help for more info\n", argv[]);
  53. break;
  54. }
  55. }
  56. }
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60. int fd=;
  61. int cnt=;
  62. char buff[GPS_LEN];
  63.  
  64. parse_cmd(argc, argv);
  65. printf("dev_name=%s, baud_rate=%d\n", dev_name, baud_rate);
  66.  
  67. fd = open(dev_name,O_RDWR|O_NOCTTY|O_NDELAY);
  68. if(fd < ) {
  69. printf("Can't Open %s\n", dev_name);
  70. return -;
  71. }
  72.  
  73. set_serial(fd, baud_rate, , 'N', );
  74.  
  75. while(loop_time--) {
  76. memset(buff, , GPS_LEN);
  77. sleep();
  78. cnt = read(fd, buff, GPS_LEN);
  79. if(cnt<) {
  80. printf("read error\n");
  81. return -;
  82. }
  83. printf("read cnt: %d, as fallow:\n%s\n", cnt, buff);
  84.  
  85. if(cnt)
  86. gps_analyse(buff);
  87.  
  88. }
  89.  
  90. close(fd);
  91. return ;
  92. }

main.c

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include "gps.h"
  4.  
  5. void print_gprmc(struct gprmc *gprmc_data)
  6. {
  7. printf("===================== GPRMC ====================\n");
  8. printf("== GPS state bit : %c [A:有效状态 V:无效状态]\n", gprmc_data->pos_state);
  9. printf("== GPS mode bit : %c [A:自主定位 D:差分定位]\n", gprmc_data->mode);
  10. printf("== Date : 20%02d-%02d-%02d \n", gprmc_data->date%,(gprmc_data->date%)/,gprmc_data->date/);
  11. printf("== Time : %02d:%02d:%02d \n", (gprmc_data->time/+)%,(gprmc_data->time%)/,gprmc_data->time%);
  12. 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));
  13. 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));
  14. printf("== 速度 : %.3f m/s \n", gprmc_data->speed);
  15. printf("================================================\n");
  16. }
  17.  
  18. void gprmc_analyse(char *buff)
  19. {
  20. char *ptr=NULL;
  21. struct gprmc gprmc_data;
  22.  
  23. /* FIXME:
  24. * 1. 002743.261 the last '261' will change so sometime can not match
  25. * 2. only attach once from buff, maybe should use while(ptr != NULL) ...
  26. */
  27. /* eg: $GPRMC,002743.261,V,,,,,0.00,0.00,060180,,,N*45 */
  28. if(NULL != (ptr=strstr(buff, "$GPRMC"))) {
  29. sscanf(ptr, "$GPRMC,%d.261,%c,%f,N,%f,E,%f,%f,%d,,,%c*",
  30. &(gprmc_data.time), &(gprmc_data.pos_state), &(gprmc_data.latitude),
  31. &(gprmc_data.longitude), &(gprmc_data.speed), &(gprmc_data.direction),
  32. &(gprmc_data.date), &(gprmc_data.mode));
  33.  
  34. print_gprmc(&gprmc_data);
  35. }
  36. }
  37.  
  38. void gps_analyse(char *buff)
  39. {
  40. gprmc_analyse(buff);
  41. //gpgsv_analyse(buff); // do more type parse
  42. }

gps.c

  1. #ifndef __GPS_H__
  2. #define __GPS_H__
  3.  
  4. struct gprmc
  5. {
  6. unsigned int time;/* gps定位时间 */
  7. char pos_state;/*gps状态位*/
  8. float latitude;/*纬度 */
  9. float longitude;/* 经度 */
  10. float speed; /* 速度 */
  11. float direction;/*航向 */
  12. unsigned int date; /*日期 */
  13. float declination; /* 磁偏角 */
  14. char dd;
  15. char mode;/* GPS模式位 */
  16. };
  17.  
  18. extern void gps_analyse(char *buff);
  19.  
  20. #endif

gps.h

  1. #include <stdio.h>
  2. #include <termios.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "serial.h"
  6.  
  7. int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop)
  8. {
  9. struct termios tty_cfg;
  10.  
  11. memset(&tty_cfg, , sizeof(tty_cfg));
  12. tty_cfg.c_cflag |= (CLOCAL|CREAD); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
  13. tty_cfg.c_cflag &= ~CSIZE; /* 设置数据位 */
  14.  
  15. switch(baud_rate) {
  16. case :
  17. cfsetispeed(&tty_cfg, B2400);
  18. cfsetospeed(&tty_cfg, B2400);
  19. break;
  20. case :
  21. cfsetispeed(&tty_cfg, B4800);
  22. cfsetospeed(&tty_cfg, B4800);
  23. break;
  24. case :
  25. cfsetispeed(&tty_cfg, B9600);
  26. cfsetospeed(&tty_cfg, B9600);
  27. break;
  28. case :
  29. cfsetispeed(&tty_cfg, B115200);
  30. cfsetospeed(&tty_cfg, B115200);
  31. break;
  32. default:
  33. cfsetispeed(&tty_cfg, B9600);
  34. cfsetospeed(&tty_cfg, B9600);
  35. break;
  36. }
  37.  
  38. switch(nBits) {
  39. case :
  40. tty_cfg.c_cflag |= CS7;
  41. break;
  42. case :
  43. tty_cfg.c_cflag |= CS8;
  44. break;
  45. }
  46.  
  47. switch(nEvent) {
  48. case '': /* 奇校验 */
  49. tty_cfg.c_cflag |= PARENB; /* 开启奇偶校验 */
  50. tty_cfg.c_iflag |= (INPCK | ISTRIP); /*INPCK打开输入奇偶校验;ISTRIP去除字符的第八个比特 */
  51. tty_cfg.c_cflag |= PARODD; /*启用奇校验(默认为偶校验)*/
  52. break;
  53. case 'E': /*偶校验*/
  54. tty_cfg.c_cflag |= PARENB; /*开启奇偶校验 */
  55. tty_cfg.c_iflag |= ( INPCK | ISTRIP); /*打开输入奇偶校验并去除字符第八个比特*/
  56. tty_cfg.c_cflag &= ~PARODD; /*启用偶校验*/
  57. break;
  58. case 'N': /*无奇偶校验*/
  59. tty_cfg.c_cflag &= ~PARENB;
  60. break;
  61. }
  62.  
  63. /* 设置停止位;若停止位为1,则清除CSTOPB,若停止位为2,则激活CSTOPB */
  64. if(nStop == )
  65. tty_cfg.c_cflag &= ~CSTOPB; /*默认为一位停止位; */
  66. else if( nStop == )
  67. tty_cfg.c_cflag |= CSTOPB; /* CSTOPB表示送两位停止位 */
  68.  
  69. /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/
  70. tty_cfg.c_cc[VTIME] = ; /* 非规范模式读取时的超时时间;*/
  71. tty_cfg.c_cc[VMIN] = ; /* 非规范模式读取时的最小字符数*/
  72. tcflush(fd, TCIFLUSH); /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
  73.  
  74. /*激活配置使其生效*/
  75. if((tcsetattr(fd, TCSANOW, &tty_cfg)) != ) {
  76. printf("com set error");
  77. exit();
  78. }
  79.  
  80. return ;
  81. }

serial.c

  1. #ifndef __FUZK_SERIAL_H__
  2. #define __FUZK_SERIAL_H__
  3.  
  4. extern int set_serial(int fd, int baud_rate, int nBits, char nEvent, int nStop);
  5.  
  6. #endif

serial.h

  1. CC = /opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-gcc
  2. CFLAGS = -O1
  3.  
  4. TARGET = gps_test
  5. OBJS = serial.o gps.o main.o
  6.  
  7. $(TARGET): $(OBJS)
  8. $(CC) -o $(TARGET) $(OBJS) $(CFLAGS)
  9.  
  10. $(OBJS): %o:%c
  11. $(CC) -c $(CFLAGS) $< -o $@
  12.  
  13. clean:
  14. rm -f $(TARGET) $(OBJS)

Makefile

gps示例代码的更多相关文章

  1. 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token

    1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...

  2. 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题

    调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...

  3. ActiveMQ笔记(1):编译、安装、示例代码

    一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...

  4. C#微信公众平台接入示例代码

    http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 这是微信公众平台提供的接入指南.官网只提供了php的示例代码 ...

  5. 编译opengl编程指南第八版示例代码通过

    最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...

  6. 股票数据调用示例代码php

    <!--?php // +---------------------------------------------------------------------- // | JuhePHP ...

  7. php示例代码之类似于C#中的String.Format方法

    php示例代码之类似于C#中的String.Format方法 原文来自于  http://stackoverflow.com/questions/1241177/c-string-format-equ ...

  8. redis 学习笔记(2)-client端示例代码

    redis提供了几乎所有主流语言的client,java中主要使用二种:Jedis与Redisson 一.Jedis的使用 <dependency> <groupId>redi ...

  9. 正则表达式学习笔记(附:Java版示例代码)

    具体学习推荐:正则表达式30分钟入门教程 .         除换行符以外的任意字符\w      word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s        space,空白符 ...

随机推荐

  1. Starting Jenkins bash: /usr/bin/java: 没有那个文件或目录

    [root@localhost /]# systemctl status jenkins.service ● jenkins.service - LSB: Jenkins Automation Ser ...

  2. sublime的注册方法 非常好用

    摘自:https://blog.csdn.net/weixin_42444922/article/details/81006107 转载 阿东的天空之城 发布于2018-07-11 20:03:43 ...

  3. Linux下安装{Git}

    环境 { "操作系统":"CentOS 7.5 64位", "CPU":"1核", "内存":&qu ...

  4. Springboot中IDE支持两种打包方式,即jar包和war包

    Springboot中IDE支持两种打包方式,即jar包和war包 打包之前修改pom.xml中的packaging节点,改为jar或者war    在项目的根目录执行maven 命令clean pa ...

  5. sql简单存储过程分享

    很多程序员朋友都视sql为洪湖水猛兽,其实深入分析一下,多用些时间与耐心,sql还是可以理解的. 本文主要是针对刚刚接触sql的新手朋友,进行一个sql存储过程的简单分享. 小子第一次发布文章,也是借 ...

  6. 局域网-断网&劫持(kali)

    1.查看局域网中的主机 fping –asg 192.168.1.0/24 2.断网 arpspoof -i wlan0 -t 192.168.100 192.168.1.1 (arpspoof  - ...

  7. python快速开始一-------常见的数据类型

    前置条件:已经安装好python程序 常用的数据类型 Python常见的数据类型: 整数:就是我们数学里面的整数,比如3455,python目前支持最大的32bit或者64bit 长整数(long): ...

  8. 53 容器(八)——TreeMap 红黑树

    红黑树是比较难以理解的一种数据结构.它能从10亿数据中进行10几次比较就能查找到需要的数据.效率非常高. 理解起内部结构也难. 现阶段我们知道有这种东西就行了. 参考文章: https://www.j ...

  9. 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.数据驱动.双向绑定 ...

  10. php GD 和图像处理函数, 制作一张图片

    php GD 和图像处理函数, 制作一张图片 // GD 和图像处理函数 // https://www.php.net/manual/zh/ref.image.php // https://www.p ...