gpt4 book ai didi

c - 从 GPSD 检索数据,无需等待时间

转载 作者:行者123 更新时间:2023-11-30 14:46:41 24 4
gpt4 key购买 nike

我是使用 gpsd 和 C 的新手。我实现了我的第一个客户端,它使用 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 就会返回。如果没有收到新数据,该函数将超时并在 t 微秒后返回。

获得更快的数据速率取决于 GPS 输出数据的速度。仅仅减少 gps_waiting 的第二个参数就会给您带来数据速率更快的错觉,但如果您检查函数的返回值,您会发现您所做的只是导致函数超时更快。

关于c - 从 GPSD 检索数据,无需等待时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52042075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com