2

I'm trying to read data from a Adafruit ultimate gps using libgps. I've found a code example that gives me all the information I need except gps time. How can I grab the gps time that the gps sent over the serial port preferably in hours/minutes/seconds?

I tried gps_data.fix.time but I'm not sure if that is system time or gps time.

#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;

}

Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • In the $GPGGA message manages by the libgps library, the time value is skipped (look on the `void nmea_parse_gpgga(char *nmea, gpgga_t *loc)` in the [Github source files](https://github.com/wdalmut/libgps/blob/master/src/nmea.c)). So, you have to modify that parsing function and updating the loc_t structure to add the missing time parameter. – J. Piquard Nov 02 '16 at 06:24
  • @J.Piquard is that the same library as the one I installed with 'sudo apt-get install libgps'? – Lightsout Nov 02 '16 at 08:59
  • Humm, when looking the `struct gps_data_t`, I did see the the `.fix.time` field. So, your libgps is more recent, could you provide the Github source ? – J. Piquard Nov 02 '16 at 09:05
  • When checking source code of pointed by that post [libgps C code example](http://stackoverflow.com/questions/32000328/libgps-c-code-example), the `struct gps_data_t` seems to have a field `timestamp_t online;` which is the **Unix time in seconds with fractional part** collected from the GPS receiver. – J. Piquard Nov 02 '16 at 09:57

2 Answers2

1

I tracked some code in the libgps and gps_data.fix.time seems to be a variable of type struct timespec. which is defined like :

struct timespec
    time_t  tv_sec;
    long    tv_nsec;    
};

you might want to try to print gps_data.fix.time.tv_sec and/or gps_data.fix.time.tv_nsec

Hope this helps.

Akkshay
  • 35
  • 6
0

I read my PC's gps.h and found timesec_t is double. And I try as follows.

int my_gps_time; // for cast fix.time(double) to int
struct tm *ptm; // for date and time

...
my_gps_time = gps_data.fix.time;
ptm = localtime((time_t *)&my_gps_time);
printf("time: %04d/%02d/%02d,%02d:%02d:%02d\n",\
       ptm->tm_year + 1900, ptm->tm_mon + 1,\
       ptm->tm_mday,ptm->tm_hour, ptm->tm_min, ptm->tm_sec); 
...