libgps C代码示例

我正在为支持GPS的Raspberry Pi编写一个数字运算数据记录C程序。 我抓住gpsd ,它的示例应用程序cgps正确显示gps信息。 我想使用libgps与守护进程交互,以便我可以在我的应用程序中获得所有方便的信息,但我很快就被其API的复杂性所淹没。

HOWTO页面上的文档指向我查看cgps和gpxlogger的示例代码,但是有很多耦合,我无法通过它来解决所有问题。 在光谱的另一端, libgps页面上的C代码示例被剥离回来使它无法使用。

任何人都能指出一个可能揭开神秘面纱的单一类样本吗? 也许包含getCoordinates()函数的东西?

我说得太早了。 在浏览了其他SO问题之后,我遇到了这个完全不相关的问题。 这是我的略微修改版本:

 #include  #include  #include  #include  #include  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) { /* wait for 2 seconds to receive data */ if (gps_waiting (&gps_data, 2000000)) { /* 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); EDIT: tv.tv_sec isn't actually the timestamp! printf("latitude: %f, longitude: %f, speed: %f, timestamp: %lf\n", gps_data.fix.latitude, gps_data.fix.longitude, gps_data.fix.speed, gps_data.fix.time); //EDIT: Replaced tv.tv_sec with gps_data.fix.time } else { printf("no GPS data available\n"); } } } sleep(3); } /* When you are done... */ gps_stream(&gps_data, WATCH_DISABLE, NULL); gps_close (&gps_data); return EXIT_SUCCESS; } 

我通过运行gcc -o gps filename.c -lm -lgps编译它