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 = 9600;
static unsigned int loop_time = 20;
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)) != -1) {
        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[0]);
            printf("like:\n");
            printf("\t %s -d /dev/ttyS2\n", argv[0]);
            printf("\t %s --device /dev/ttyS2 -b 9600\n", argv[0]);
            break;
        default:
            printf("Unknown option or invalid format!\n");
            printf("Pls: %s --help for more info\n", argv[0]);
            break;
        }
    }
}


int main(int argc, char *argv[])
{
    int fd=0;
    int cnt=0;
    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 < 0) {
        printf("Can't Open %s\n", dev_name);
        return -1;
    }

    set_serial(fd, baud_rate, 8, 'N', 1);

    while(loop_time--) {
        memset(buff, 0 , GPS_LEN);
        sleep(3);
        cnt = read(fd, buff, GPS_LEN);
        if(cnt<0) {
            printf("read error\n");
            return -1;
        }
        printf("read cnt: %d, as fallow:\n%s\n", cnt, buff);

        if(cnt)
            gps_analyse(buff);

    }

    close(fd);
    return 0;
}
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%100,(gprmc_data->date%10000)/100,gprmc_data->date/10000);
    printf("==   Time : %02d:%02d:%02d                      \n", (gprmc_data->time/10000+8)%24,(gprmc_data->time%10000)/100,gprmc_data->time%100);
    printf("==   纬度 : 北纬:%d度%d分%d秒                   \n", ((int)gprmc_data->latitude) / 100, (int)(gprmc_data->latitude - ((int)gprmc_data->latitude / 100 * 100)), (int)(((gprmc_data->latitude - ((int)gprmc_data->latitude / 100 * 100)) - ((int)gprmc_data->latitude - ((int)gprmc_data->latitude / 100 * 100))) * 60.0));
    printf("==   经度 : 东经:%d度%d分%d秒                   \n", ((int)gprmc_data->longitude) / 100, (int)(gprmc_data->longitude - ((int)gprmc_data->longitude / 100 * 100)), (int)(((gprmc_data->longitude - ((int)gprmc_data->longitude / 100 * 100)) - ((int)gprmc_data->longitude - ((int)gprmc_data->longitude / 100 * 100))) * 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, 0, sizeof(tty_cfg));
    tty_cfg.c_cflag |= (CLOCAL|CREAD); /* CREAD 开启串行数据接收,CLOCAL并打开本地连接模式 */
    tty_cfg.c_cflag &= ~CSIZE; /* 设置数据位 */

    switch(baud_rate) {
    case 2400:
        cfsetispeed(&tty_cfg, B2400);
        cfsetospeed(&tty_cfg, B2400);
        break;
    case 4800:
        cfsetispeed(&tty_cfg, B4800);
        cfsetospeed(&tty_cfg, B4800);
        break;
    case 9600:
        cfsetispeed(&tty_cfg, B9600);
        cfsetospeed(&tty_cfg, B9600);
        break;
    case 115200:
        cfsetispeed(&tty_cfg, B115200);
        cfsetospeed(&tty_cfg, B115200);
        break;
    default:
        cfsetispeed(&tty_cfg, B9600);
        cfsetospeed(&tty_cfg, B9600);
        break;
    }

    switch(nBits) {
    case 7:
        tty_cfg.c_cflag |= CS7;
        break;
    case 8:
        tty_cfg.c_cflag |= CS8;
        break;
    }

    switch(nEvent) {
    case '0':  /* 奇校验 */
        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 == 1)
        tty_cfg.c_cflag &= ~CSTOPB; /*默认为一位停止位; */
    else if( nStop == 2)
        tty_cfg.c_cflag |= CSTOPB; /* CSTOPB表示送两位停止位 */

    /* 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时*/
    tty_cfg.c_cc[VTIME] = 0; /* 非规范模式读取时的超时时间;*/
    tty_cfg.c_cc[VMIN]  = 0; /* 非规范模式读取时的最小字符数*/
    tcflush(fd, TCIFLUSH); /* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */

    /*激活配置使其生效*/
    if((tcsetattr(fd, TCSANOW, &tty_cfg)) != 0) {
        printf("com set error");
        exit(1);
    }

    return 0;
}
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

 

posted @ 2019-07-17 16:06  Vedic  阅读(615)  评论(0编辑  收藏  举报