无需等待时间从 gpsd 检索数据
retrieve data from gpsd without wait time
我是用 C 语言使用 gpsd 的新手。我实现了我的第一个使用 gps_stream 函数的客户端。如果我理解正确的话,它就像一个 pub/sub 函数,你可以使用 gps_read 读取 gps 数据。我想在数据可用时尽快检索数据。我发现的唯一方法是减少 gps_waiting 函数的时间。想知道有没有办法不使用gps_waiting函数,尽快检索。下面是我的代码。
int runGpsStreamClient() {
int rc;
int count = 0;
clock_t t;
struct gps_data_t gps_data;
t = clock();
if ((rc = gps_open("localhost", "2947", &gps_data)) == -1) {
printf("code: %d, reason: %s\n", rc, gps_errstr(rc));
return EXIT_FAILURE;
}
get_metric(t, "gps_open");
t = clock();
gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
get_metric(t, "gps_stream");
while (count < 60) {
/* wait for 0.1 second to receive data */
if (gps_waiting(&gps_data, 100000)) {
t = clock();
int rc = gps_read(&gps_data);
get_metric(t, "gps_read");
/* read data */
if (rc == -1) {
printf("error occurred reading gps data. code: %d, reason: %s\n", rc, gps_errstr(rc));
} else {
/* Display data from the GPS receiver. */
double lat = gps_data.fix.latitude;
double lon = gps_data.fix.longitude;
double alt = gps_data.fix.altitude;
double speed = gps_data.fix.speed;
double climb = gps_data.fix.climb;
time_t seconds = (time_t) gps_data.fix.time;
int status = gps_data.status;
int mode = gps_data.fix.mode;
printf("status[%d], ", status);
printf("mode[%d], ", mode);
printf("latitude[%f], ", lat);
printf("longitude[%f], ", lon);
printf("altitude[%f], ", alt);
printf("speed[%f], ", speed);
printf("v speed[%f], ", climb);
printf("Time[%s].", ctime(&seconds));
if ((status == STATUS_FIX)
&& (mode == MODE_2D || mode == MODE_3D)
&& !isnan(lat) && !isnan(lon)) {
printf(" GPS data OK.\n");
} else {
printf(" GPS data NOK.\n");
}
}
} else {
printf("counter[%d]. Timeout to retrieve data from gpsd. Maybe increase gps_waiting.\n", count);
}
count++;
}
/* When you are done... */
gps_stream(&gps_data, WATCH_DISABLE, NULL);
gps_close(&gps_data);
return EXIT_SUCCESS;
}
谢谢,
费利佩
来自 gpsd documents(强调我的)
gps_waiting()
can be used to check whether there is new data from the daemon. The second argument is the maximum amount of time to wait (in microseconds) on input before returning. It returns true if there is input waiting, false on timeout (no data waiting) or error condition. When using the socket export, this function is a convenience wrapper around a select(2) call...
如果没有新数据,gps_waiting(&gps_data, t)
将阻塞 最多 t
微秒。一旦从 GPS 接收到新数据,gps_waiting
应该 return。如果没有收到新数据,该函数将在 t
微秒后超时并 return。
获得更快的数据速率将取决于您的 GPS 输出数据的速度。仅仅减少 gps_waiting
的第二个参数会给你更快数据速率的错觉,但如果你检查函数的 return 值,你会发现你所做的只是使函数计时快点出来。
我是用 C 语言使用 gpsd 的新手。我实现了我的第一个使用 gps_stream 函数的客户端。如果我理解正确的话,它就像一个 pub/sub 函数,你可以使用 gps_read 读取 gps 数据。我想在数据可用时尽快检索数据。我发现的唯一方法是减少 gps_waiting 函数的时间。想知道有没有办法不使用gps_waiting函数,尽快检索。下面是我的代码。
int runGpsStreamClient() {
int rc;
int count = 0;
clock_t t;
struct gps_data_t gps_data;
t = clock();
if ((rc = gps_open("localhost", "2947", &gps_data)) == -1) {
printf("code: %d, reason: %s\n", rc, gps_errstr(rc));
return EXIT_FAILURE;
}
get_metric(t, "gps_open");
t = clock();
gps_stream(&gps_data, WATCH_ENABLE | WATCH_JSON, NULL);
get_metric(t, "gps_stream");
while (count < 60) {
/* wait for 0.1 second to receive data */
if (gps_waiting(&gps_data, 100000)) {
t = clock();
int rc = gps_read(&gps_data);
get_metric(t, "gps_read");
/* read data */
if (rc == -1) {
printf("error occurred reading gps data. code: %d, reason: %s\n", rc, gps_errstr(rc));
} else {
/* Display data from the GPS receiver. */
double lat = gps_data.fix.latitude;
double lon = gps_data.fix.longitude;
double alt = gps_data.fix.altitude;
double speed = gps_data.fix.speed;
double climb = gps_data.fix.climb;
time_t seconds = (time_t) gps_data.fix.time;
int status = gps_data.status;
int mode = gps_data.fix.mode;
printf("status[%d], ", status);
printf("mode[%d], ", mode);
printf("latitude[%f], ", lat);
printf("longitude[%f], ", lon);
printf("altitude[%f], ", alt);
printf("speed[%f], ", speed);
printf("v speed[%f], ", climb);
printf("Time[%s].", ctime(&seconds));
if ((status == STATUS_FIX)
&& (mode == MODE_2D || mode == MODE_3D)
&& !isnan(lat) && !isnan(lon)) {
printf(" GPS data OK.\n");
} else {
printf(" GPS data NOK.\n");
}
}
} else {
printf("counter[%d]. Timeout to retrieve data from gpsd. Maybe increase gps_waiting.\n", count);
}
count++;
}
/* When you are done... */
gps_stream(&gps_data, WATCH_DISABLE, NULL);
gps_close(&gps_data);
return EXIT_SUCCESS;
}
谢谢, 费利佩
来自 gpsd documents(强调我的)
如果没有新数据,
gps_waiting()
can be used to check whether there is new data from the daemon. The second argument is the maximum amount of time to wait (in microseconds) on input before returning. It returns true if there is input waiting, false on timeout (no data waiting) or error condition. When using the socket export, this function is a convenience wrapper around a select(2) call...
gps_waiting(&gps_data, t)
将阻塞 最多 t
微秒。一旦从 GPS 接收到新数据,gps_waiting
应该 return。如果没有收到新数据,该函数将在 t
微秒后超时并 return。
获得更快的数据速率将取决于您的 GPS 输出数据的速度。仅仅减少 gps_waiting
的第二个参数会给你更快数据速率的错觉,但如果你检查函数的 return 值,你会发现你所做的只是使函数计时快点出来。