Using GPSD¶
How to configure GPSD and expose GNSS receiver data to Linux applications.
This English edition retains the original diagrams and practical code or command examples.





Commands and examples¶
# 启动
sudo systemctl start gpsd
# restart
sudo systemctl restart gpsd
# 停止
sudo systemctl stop gpsd
# 查询状态
sudo systemctl satus gpsd
# 停止
sudo systemctl stop gpsd
sudo systemctl stop gpsd.socket
$ cat /etc/default/gpsd
# Devices gpsd should collect to at boot time.
# They need to be read/writeable, either by user gpsd or the group dialout.
DEVICES="/dev/ttyUSB0"
# Other options you want to pass to gpsd
GPSD_OPTIONS="-F /var/run/gpsd.sock"
# Automatically hot add/remove USB GPS devices via gpsdctl
USBAUTO="true"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gps.h>
#define GPSD_HOST "localhost"
#define GPSD_PORT "2947"
void print_gps_data(struct gps_data_t *gps_data) {
if (gps_data->fix.mode >= MODE_2D) {
printf("Latitude: %f\n", gps_data->fix.latitude);
printf("Longitude: %f\n", gps_data->fix.longitude);
printf("Altitude: %f\n", gps_data->fix.altitude);
// Time is stored as a struct timespec, so we need to check if tv_sec is non-zero
if (gps_data->fix.time.tv_sec != 0) {
printf("Time: %ld.%09ld\n",
gps_data->fix.time.tv_sec, gps_data->fix.time.tv_nsec);
} else {
printf("Time: N/A\n");
}
} else {
printf("GPS data not available.\n");
}
}
int main(void) {
struct gps_data_t gps_data;
char buf[256];
int ret;
// Initialize GPSD library
if (gps_open(GPSD_HOST, GPSD_PORT, &gps_data) != 0) {
fprintf(stderr, "Could not connect to GPSD\n");
return 1;
}
// Configure GPSD to provide data
gps_stream(&gps_data, WATCH_ENABLE, NULL);
while (1) {
// Read GPS data with appropriate arguments
ret = gps_read(&gps_data, buf, sizeof(buf));
if (ret == -1) {
fprintf(stderr, "Error reading GPS data\n");
break;
}
print_gps_data(&gps_data);
sleep(1); // Update interval
}
gps_stream(&gps_data, WATCH_DISABLE, NULL);
gps_close(&gps_data);
return 0;
}
Practical notes¶
Validate pin multiplexing, clocks, power sequencing, kernel configuration, and device-tree bindings on the target board before drawing conclusions from a test result.