I'm trying to write code that returns the latitude off a box with a GPS antenna, however I can't seem to figure out how to get this data back. The remote box is running gpsd and I can see that data is being retrieved from the GPS antenna using gpspipe.
Here's what I've done to get GPS data sent to my local machine:
ssh -l user x.x.x.x -L 2948:127.0.0.1:2947
gpsd -N -n "gpsd://localhost:2948"
Next, to verify I am getting NMEA data back, I've ran gpspipe and I can see data flowing.
I've written the following C code:
#include <unistd.h>
#include <math.h>
#include <gps.h>
#define DEFAULT_HOST "localhost"
#define DEFAULT_PORT "2947"
typedef int GpsErrorCode_t;
static struct gps_data_t gpsdata;
static void process(struct gps_data_t *gps_data) {
printf("%d %d %f %f\n", gps_data->status, gps_data->fix.mode, gps_data->fix.latitude, gps_data->fix.longitude);
}
GpsErrorCode_t getLatitude(double* lat) {
GpsErrorCode_t err = gps_open(DEFAULT_HOST, DEFAULT_PORT, &gpsdata);
if (err != 0) {
return err;
}
gps_stream(&gpsdata, WATCH_ENABLE | WATCH_RAW, NULL);
//gps_mainloop(&gpsdata, 5000000, process);
int retries = 10;
int rc;
while (1) {
//retries--;
usleep(50000);
if (gps_waiting(&gpsdata, 500)) {
if ((err = gps_read(&gpsdata)) == -1) {
printf("ERROR: occured reading gps data. code: %d, reason: %s\n", err, gps_errstr(err));
break;
} else {
if (gpsdata.set & PACKET_SET) {
printf("gps_read return code: %d\n", err);
printf("%d %d %f %f\n", gpsdata.status, gpsdata.fix.mode, gpsdata.fix.latitude, gpsdata.fix.longitude);
}
}
} else {
printf("ERROR: no data waiting\n");
break;
}
}
gps_stream(&gpsdata, WATCH_DISABLE, NULL);
gps_close(&gpsdata);
return err;
}
int main() {
double lat = 0;
int err = 0;
err = getLatitude(&lat);
printf("Error code: %d\n", err);
return 0;
}
When I run the code I get the following output:
gps_read return code: 92
0 0 nan nan
gps_read return code: 152
0 0 nan nan
gps_read return code: 125
0 0 nan nan
gps_read return code: 67
0 0 nan nan
gps_read return code: 34
0 0 nan nan
gps_read return code: 79
0 0 nan nan
gps_read return code: 65
0 0 nan nan
gps_read return code: 55
0 0 nan nan
gps_read return code: 51
0 0 nan nan
gps_read return code: 41
0 0 nan nan
gps_read return code: 37
0 0 nan nan
gps_read return code: 67
0 0 nan nan
gps_read return code: 34
0 0 nan nan
gps_read return code: 79
0 0 nan nan
gps_read return code: 65
0 0 nan nan
And so on...
My questions are: Is my code correct? Why can't I retrieve any fix data? Is my setup correct?
If you need more information please don't hesitate to ask. Thanks.