2

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:

  1. ssh -l user x.x.x.x -L 2948:127.0.0.1:2947
  2. 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.

Anthony Calandra
  • 1,429
  • 2
  • 11
  • 17
  • Not problem related, but don't call your own types `something_t` -- the `_t` suffix is reserved for the implementation in POSIX. –  Jun 09 '17 at 16:55
  • the posted code does not compile! amongst many other things, it is missing the statement: `#include ` for `printf()`, etc – user3629249 Jun 11 '17 at 03:21
  • could you post the contents of `gps.h`? – user3629249 Jun 11 '17 at 03:22
  • when displaying an error message, it should be output to `stderr`, not `stdout` so this line: `printf("ERROR: occured reading gps data. code: %d, reason: %s\n", err, gps_errstr(err));` should be: `fprintf( stderr, "ERROR: occurred reading gps data. code: %d, reason: %s\n", err, gps_errstr(err) );. Similar considerations apply to `printf("ERROR: no data waiting\n");` – user3629249 Jun 11 '17 at 03:30

2 Answers2

0

I had a look at https://fossies.org/dox/gpsd-3.16/gps_8h_source.html and found in lines 70ff that mode 0 basically tells you there there was no update, yet.

And along https://fossies.org/dox/gpsd-3.16/gps_8h_source.html#l01996 tells you that status 0 indicates that you just do not have a fix.

I have not fully scrutinized your code, but it seems worthwhile to just wait for another few minutes.

smoe
  • 500
  • 3
  • 13
0

Changing one of the flags from WATCH_RAW to WATCH_JSON seemed to do the trick without any other changes. I don't know why trying to read in raw data (or NMEA which I tried with WATCH_NMEA) didn't work in the first place... Feel free to leave a comment if you know.

Anthony Calandra
  • 1,429
  • 2
  • 11
  • 17