Reputation: 3757
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;
}
Upvotes: 2
Views: 3961
Reputation: 1
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);
...
Upvotes: 0
Reputation: 140
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.
Upvotes: 1