如何实现 gpsd 客户端(在 C 中)以获取纬度、经度和高度? [英] How can I implement a gpsd client (in C) to get Latitude, Longitude and Altitude?

查看:8
本文介绍了如何实现 gpsd 客户端(在 C 中)以获取纬度、经度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的支持 GPS 的 Raspberry Pi 编写一个数字运算数据记录 C 程序.我抓住了 gpsd,它的示例应用程序 cgps 正确显示了 gps 信息.我想使用 libgps 与守护进程交互,这样我就可以在我的应用程序中获得所有方便的信息,但我很快就被其 API 的复杂性弄得不知所措.

I am writing a number-crunching data-logging C program for my GPS enabled Raspberry Pi. I grabbed gpsd, and its sample app cgps displays gps information correctly. I wanted to use libgps to interface with the daemon so that I could have all that handy information in my app, but I was quickly overwhelmed by the complexity of its API.

HOWTO 页面上的文档让我看看 cgpsgpxlogger 例如代码,但是有太多的耦合,我无法全部通过.在光谱的另一端,libgps 页面上的 C 代码示例被剥离,以至于无法使用.

The documentation on its HOWTO page points me to look at cgps and gpxlogger for example code, but there's so much coupling that I can't wade through it all. On the opposite end of the spectrum, the C code example on the libgps page is so stripped back that it's unusable.

谁能给我指出一个可以揭开这个神秘面纱的班级样本?也许包含 getCoordinates() 函数的东西?

Can anybody point me to a single class sample that might demystify this? Perhaps something that contains a getCoordinates() function?

推荐答案

我说得太早了.浏览其他 SO 问题后,我遇到了完全不相关的 this问题.这是我稍作修改的版本:

I spoke too soon. After browsing other SO questions, I ran into this completely unrelated question. Here's my very slightly modified version:

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

int main() {
struct timeval tv;
struct gps_data_t gps_data;

if ((gps_open("localhost", "2947", &gps_data)) == -1) {
    printf("code: %d, reason: %s
", errno, gps_errstr(errno));
    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 ((gps_read(&gps_data,NULL,0)) == -1) {
            printf("error occured reading gps data. code: %d, reason: %s
", errno, gps_errstr(errno));
        } 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: %lf
", 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
");
            }
        }
    }

    sleep(3);
}

/* When you are done... */
gps_stream(&gps_data, WATCH_DISABLE, NULL);
gps_close (&gps_data);

return EXIT_SUCCESS;
}

我通过运行 gcc -o gps filename.c -lm -lgps

这篇关于如何实现 gpsd 客户端(在 C 中)以获取纬度、经度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆