我正在尝试编写代码来返回带有 GPS 天线的盒子的纬度,但是我似乎无法弄清楚如何取回这些数据。远程盒子正在运行 gpsd,我可以看到正在使用 gpspipe 从 GPS 天线检索数据。
以下是我为将 GPS 数据发送到本地机器所做的工作:
ssh -l user x.x.x.x -L 2948:127.0.0.1:2947
gpsd -N -n "gpsd://localhost:2948"
接下来,为了验证我是否正在获取 NMEA 数据,我运行了 gpspipe,我可以看到数据在流动。
我编写了以下 C 代码:
#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;
}
当我运行代码时,我得到以下输出:
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
等等...
我的问题是:我的代码是否正确?为什么我无法检索任何修复数据?我的设置正确吗?
如果您需要更多信息,请随时询问。谢谢。