使用gpsd / libgps C获取gps时间

问题描述 投票:2回答:1

我正在尝试使用libgps从Adafruit Ultimate gps读取数据。我找到了一个代码示例,该示例为我提供了除gps时间以外所有我需要的信息。如何获取gps通过串口发送的gps时间,最好以小时/分钟/秒为单位?

我尝试过gps_data.fix.time,但不确定是系统时间还是gps时间。

#include <gps.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

int main() {
    int rc;
    struct timeval tv;

    struct gps_data_t gps_data;
    if ((rc = gps_open("localhost", "2947", &gps_data)) == -1) {
        printf("code: %d, reason: %s\n", rc, gps_errstr(rc));
        return EXIT_FAILURE;
    }
    gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);

    while (1) {
        /* time to wait to receive data */
        if (gps_waiting (&gps_data, 500000)) {
        /* read data */
        if ((rc = gps_read(&gps_data)) == -1) {
            printf("error occured reading gps data. code: %d, reason: %s\n", rc, gps_errstr(rc));
        } else {
            /* Display data from the GPS receiver. */
            if ((gps_data.status == STATUS_FIX) && 
                (gps_data.fix.mode == MODE_2D || gps_data.fix.mode == MODE_3D) &&
                !isnan(gps_data.fix.latitude) && 
                !isnan(gps_data.fix.longitude)) {
                    gettimeofday(&tv, NULL);
                //*****************WOULD LIKE TO PRINT THE TIME HERE.*****************************
                    printf("height: %f, latitude: %f, longitude: %f, speed: %f, timestamp: %f\n", gps_data.fix.altitude, gps_data.fix.latitude, gps_data.fix.longitude, gps_data.fix.speed, gps_data.fix.time/*tv.tv_sec*/);
            } else {
                printf("no GPS data available\n");
            }
        }
    }

    //sleep(1);
}

/* When you are done... */
gps_stream(&gps_data, WATCH_DISABLE, NULL);
gps_close (&gps_data);

return EXIT_SUCCESS;

}

c linux gps serial-port gpsd
1个回答
1
投票

我在libgps中跟踪了一些代码,gps_data.fix.time似乎是struct timespec类型的变量。定义如下:

struct timespec
    time_t  tv_sec;
    long    tv_nsec;    
};

您可能想尝试打印gps_data.fix.time.tv_sec和/或gps_data.fix.time.tv_nsec

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.