Hi
i am trying to test my program with fakegps but when i call open_gps() this function return -1. I dont understand why. I hope you can help me.
I run the fakegps + cgps in the terminal without problems.
code:
#include <stdio.h>
#include <stdlib.h>
#include <gps.h>
#include <errno.h>
void debugDump(struct gps_data_t* gpsdata){
fprintf(stderr,"Longitude: %lf\nLatitude: %lf\nAltitude: %lf\nAccuracy: %lf\n\n",
gpsdata->fix.latitude, gpsdata->fix.longitude, gpsdata->fix.altitude,
(gpsdata->fix.epx>gpsdata->fix.epy)?gpsdata->fix.epx:gpsdata->fix.epy);
}
int main(){
int result = 0;
static struct gps_data_t *gpsdata;
char* server = "localhost";
char* port = "2947";
char *err_str = NULL;
printf ("%s %s \n", server, port);
result = gps_open(server, port, gpsdata);
printf("open returned %d %s\n",result,gps_errstr(errno));
if (gpsdata == NULL) {
printf("Could not connect to gpsd!\n");
return 1;
}
//register for updates
gps_stream(gpsdata, WATCH_ENABLE, NULL);
printf("Waiting for gps lock.\n");
//when status is >0, you have data.
while(gpsdata->status==0){
//block for up to .5 seconds
if (gps_waiting(gpsdata, 500)){
//dunno when this would happen but its an error
if(gps_read(gpsdata)==-1){
printf("GPSd Error\n");
gps_stream(gpsdata, WATCH_DISABLE, NULL);
gps_close(gpsdata);
return(-1);
break;
}
else{
//status>0 means you have data
if(gpsdata->status>0){
//sometimes if your GPS doesnt have a fix, it sends you data anyways
//the values for the fix are NaN. this is a clever way to check for NaN.
if(gpsdata->fix.longitude!=gpsdata->fix.longitude || gpsdata->fix.altitude!=gpsdata->fix.altitude){
printf("Could not get a GPS fix.\n");
gps_stream(gpsdata, WATCH_DISABLE, NULL);
gps_close(gpsdata);
return(-1);
}
//otherwise you have a legitimate fix!
else
debugDump(gpsdata);
printf("hm\n");
}
//if you don't have any data yet, keep waiting for it.
else
printf(".");
}
}
//apparently gps_stream disables itself after a few seconds.. in this case, gps_waiting returns false.
//we want to re-register for updates and keep looping! we dont have a fix yet.
else
gps_stream(gpsdata, WATCH_ENABLE | WATCH_JSON, NULL);
//just a sleep for good measure.
sleep(1);
}
//cleanup
gps_stream(gpsdata, WATCH_DISABLE, NULL);
gps_close(gpsdata);
}
best regards