在Ubuntu14.04上获取gps数据时,gps_waiting()返回0

在Ubuntu14.04上获取gps数据时,gps_waiting()返回0,ubuntu,gps,Ubuntu,Gps,我正在尝试使用Ubuntu14.04上的USB GPS设备UB-353+获取GPS数据。它在\dev目录中的名称是ttyACM0,通过命令cat ttyACM0,我可以看到NMEA GPS数据,如下图所示。 我不想自己解析NMEA数据,而是希望通过gpsd守护进程获取纬度和经度,因此我使用 之后,我使用以下代码获取GPS数据: /*compile command: gcc -o gps get_get.c -lm -lgps*/ #include <gps.h> #include

我正在尝试使用Ubuntu14.04上的USB GPS设备UB-353+获取GPS数据。它在\dev目录中的名称是ttyACM0,通过命令cat ttyACM0,我可以看到NMEA GPS数据,如下图所示。 我不想自己解析NMEA数据,而是希望通过gpsd守护进程获取纬度和经度,因此我使用

之后,我使用以下代码获取GPS数据:

/*compile command: gcc -o gps get_get.c -lm -lgps*/

#include <gps.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

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: %ld\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;
}
然而,我发现这段代码通常并不总是停留在gps_waiting&gps_数据2000000处,这意味着函数gps_waiting返回0。但我不知道为什么会发生这种情况,因为GPS设备工作正常


我被这个错误困扰了几天,非常感谢你的帮助

>不确定你对C++有多熟悉,但你想试试这个:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>

#include <libgpsmm.h>

int main(void)
{
  gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT);

  if (gps_rec.stream(WATCH_ENABLE | WATCH_JSON) == NULL) {
    std::cerr << "No GPSD running.\n";
    return 1;
  }

  for (;;) {
    struct gps_data_t *newdata;

    if (!gps_rec.waiting(50000000)) continue;

    if ((newdata = gps_rec.read()) == NULL) {
      std::cerr << "Read error.\n";
      return 1;
    } else {
      while (((newdata = gps_rec.read()) == NULL) ||
             (newdata->fix.mode < 1)) {
        // Do nothing; don't want to output wrong initial time
      }
      timestamp_t ts   = newdata->fix.time;
      double latitude  = newdata->fix.latitude;
      double longitude = newdata->fix.longitude;

      // convert GPSD's timestamp_t into time_t
      time_t seconds;
      seconds = (time_t)ts;
      auto tm = *std::localtime(&seconds);

      std::ostringstream oss;
      oss << std::put_time(&tm, "%d-%m-%Y %H:%M:%S");
      auto time_str = oss.str();

      // set decimal precision
      std::cout.precision(6);
      std::cout.setf(std::ios::fixed, std::ios::floatfield);
      std::cout << time_str << "," <<
        latitude << "," <<
        longitude << '\n';
    }
  }
  return 0;
}
并编译:g++-Wall-std=c++14-pedantic$pkg config-cflags-libs libgps gpsd-example.cpp-o gpsd-example


我使用这些代码作为我对gpsd所做的任何事情的基础,我还没有遇到任何奇怪的问题。如果是编译器问题、gspd守护进程问题或其他问题,也许我们可以缩小范围。

在等待答案时,我尝试使用最新的源代码安装gpsd,问题得到了解决。稍后我将尝试您的代码。
#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>

#include <libgpsmm.h>

int main(void)
{
  gpsmm gps_rec("localhost", DEFAULT_GPSD_PORT);

  if (gps_rec.stream(WATCH_ENABLE | WATCH_JSON) == NULL) {
    std::cerr << "No GPSD running.\n";
    return 1;
  }

  for (;;) {
    struct gps_data_t *newdata;

    if (!gps_rec.waiting(50000000)) continue;

    if ((newdata = gps_rec.read()) == NULL) {
      std::cerr << "Read error.\n";
      return 1;
    } else {
      while (((newdata = gps_rec.read()) == NULL) ||
             (newdata->fix.mode < 1)) {
        // Do nothing; don't want to output wrong initial time
      }
      timestamp_t ts   = newdata->fix.time;
      double latitude  = newdata->fix.latitude;
      double longitude = newdata->fix.longitude;

      // convert GPSD's timestamp_t into time_t
      time_t seconds;
      seconds = (time_t)ts;
      auto tm = *std::localtime(&seconds);

      std::ostringstream oss;
      oss << std::put_time(&tm, "%d-%m-%Y %H:%M:%S");
      auto time_str = oss.str();

      // set decimal precision
      std::cout.precision(6);
      std::cout.setf(std::ios::fixed, std::ios::floatfield);
      std::cout << time_str << "," <<
        latitude << "," <<
        longitude << '\n';
    }
  }
  return 0;
}