example1.c.txt 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // example gpsd client
  2. // compile this way:
  3. // gcc example1.c -o example1 -lgps -lm
  4. #include <gps.h>
  5. #include <math.h> // for isfinite()
  6. #include <unistd.h> // for sleep()
  7. #define MODE_STR_NUM 4
  8. static char *mode_str[MODE_STR_NUM] = {
  9. "n/a",
  10. "None",
  11. "2D",
  12. "3D"
  13. };
  14. int main(int argc, char *argv[])
  15. {
  16. struct gps_data_t gps_data;
  17. if (0 != gps_open("localhost", "2947", &gps_data)) {
  18. printf("Open error. Bye, bye\n");
  19. return 1;
  20. }
  21. (void)gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
  22. while (gps_waiting(&gps_data, 5000000)) {
  23. if (-1 == gps_read(&gps_data, NULL, 0)) {
  24. printf("Read error. Bye, bye\n");
  25. break;
  26. }
  27. if (MODE_SET != (MODE_SET & gps_data.set)) {
  28. // did not even get mode, nothing to see here
  29. continue;
  30. }
  31. if (0 > gps_data.fix.mode ||
  32. MODE_STR_NUM <= gps_data.fix.mode) {
  33. gps_data.fix.mode = 0;
  34. }
  35. printf("Fix mode: %s (%d) Time: ",
  36. mode_str[gps_data.fix.mode],
  37. gps_data.fix.mode);
  38. if (TIME_SET == (TIME_SET & gps_data.set)) {
  39. // not 32 bit safe
  40. printf("%ld.%09ld ", gps_data.fix.time.tv_sec,
  41. gps_data.fix.time.tv_nsec);
  42. } else {
  43. puts("n/a ");
  44. }
  45. if (isfinite(gps_data.fix.latitude) &&
  46. isfinite( gps_data.fix.longitude)) {
  47. // Display data from the GPS receiver if valid.
  48. printf("Lat %.6f Lon %.6f\n",
  49. gps_data.fix.latitude, gps_data.fix.longitude);
  50. }
  51. }
  52. // When you are done...
  53. (void)gps_stream(&gps_data, WATCH_DISABLE, NULL);
  54. (void)gps_close(&gps_data);
  55. return 0;
  56. }