GPS测试程序举例

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int fd;
typedef struct GPSINFO{
    char    utc_time[64];
    char    status;
    float    latitude_value;
    char    latitude;
    float    longtitude_value;
    char    longtitude;
    float    speed;
    float    azimuth_angle;
    char    utc_data[64];
}GPSINFO;
int gps_read(char *buf)
{
    char buffer[1024]= {0};
    char sbuf[128] ={0};
    char * gpsbuf;
    GPSINFO gpsinfo;
    int i = 0;
    int timeout = 10;
    while(1) {
        int count;
        count = read(fd, buffer, sizeof(buffer));
        printf("%s",buffer);
        if(count < 0) {
            perror("read");
            continue;
        }
        if('A' == gpsinfo.status) {
            sprintf(sbuf, "X%c%fY%c%fT", gpsinfo.latitude, gpsinfo.latitude_value,gpsinfo.longtitude,gpsinfo.longtitude_value);
            gpsinfo.utc_time[strlen(gpsinfo.utc_time)] = 'W';
            int size = strlen(sbuf);
            i = 0;
            while(gpsinfo.utc_time[i] != 'W') {
                sbuf[size + i] = gpsinfo.utc_time[i];
                i++;
            }
        } else {
            continue;
        }
        break;
    }
    sbuf[strlen(sbuf)] = 'W';
    memcpy(buf, sbuf, sizeof(sbuf));
    return 0;
}
int main()
{
    struct termios opt;
    char buf[128];
    int i=0;
    for (i=0; i<10; i++) {
        sprintf(buf, "/dev/ttyUSB%d", i);printf("USP port:%s\n", buf);
        if((fd = open(buf,O_RDWR)) < 0) {
                printf("open error\n");
                continue;
        } else {
                printf("open ok\n");
                break;
        }
    }
    if (i==10) return -1;
    tcgetattr(fd, &opt);
    cfsetispeed(&opt, B4800);
    cfsetospeed(&opt, B4800);
    opt.c_cflag |= (CLOCAL | CREAD);
    opt.c_cflag &= ~CRTSCTS;
    opt.c_cflag &= ~CSIZE;
    opt.c_cflag |= CS8;
    opt.c_iflag |= IGNPAR;
    opt.c_cflag &= ~CSTOPB;
    opt.c_oflag = 0;
    opt.c_lflag = 0;
    tcsetattr(fd, TCSANOW, &opt);
    gps_read(buf);
    return 0;
}

二.硬件连接:

三.内核配置:

四.插上GPS模块(USB接口USBHOST1)

终端显示如下:

五.执行测试程序:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值