Skip to main content

Using GPS for obtaining coordinates and speed

For the administrator: checking the GNSS receiver operation

Module: GNSS N10HD, serial port /dev/ttyS2, 115200 baud, 8 bits, no parity (115200n8).

Antenna: An active antenna with a 3.3 V supply is required. If the antenna is missing or broken, the status ANT_OPEN is returned.

1. Checking for data availability

# Grant read permissions to the port (if necessary)
chmod 666 /dev/ttyS2

# Start receiving NMEA sentences
cu -l /dev/ttyS2 -s 115200

After starting, the terminal will display NMEA sentences beginning with $GNGGA, $GNGSA, $GNRMC, $GNTXT, etc.

Example output:

$GNGGA,,,,,,0,00,,,M,,M,,*78
$GNRMC,,V,,,,,,,,0.0,E,N,V*5C
$GNTXT,01,01,02,ANT_OPEN,B2,*33
  • $GNRMC with the A flag contains valid coordinates of the point and navigation data (speed, course, time).
  • $GNTXT also returns the antenna connection status. For an active, working antenna, the string ANT_OK is displayed. If the antenna is missing or faulty, ANT_OPEN (or ANT_SHORT) is displayed.
  • While there is no latching (no A), all coordinate and speed fields will be empty or zero.

2. Administrator checklist

  • Ensure that the antenna is connected and exposed to the sky.
  • Check that the port is not in use by another process: lsof /dev/ttyS2.
  • If necessary, add the user to the dialout group to access the port.

For the programmer: receiving and parsing GPS data

1. Opening and reading the port

Port /dev/ttyS2 is configured for baud rate 115200, 8N1. Data is read line by line (\r\n is the end of an NMEA line).

C example:

int fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY);
struct termios tty;
tcgetattr(fd, &tty);
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
tty.c_cflag |= CS8 | CLOCAL | CREAD;
tcsetattr(fd, TCSANOW, &tty);
// reading a line
char buf[256];
read(fd, buf, sizeof(buf));

Python example (using pynmea2):

import serial
import pynmea2

ser = serial.Serial('/dev/ttyS2', 115200, timeout=1)
while True:
line = ser.readline().decode('ascii', errors='replace').strip()
if line.startswith('$GNRMC'):
msg = pynmea2.parse(line)
if msg.status == 'A': # data valid
speed_knots = msg.spd_over_grnd
speed_kmh = speed_knots * 1.852
lat = msg.latitude
lon = msg.longitude
print(f"Coordinates: {lat}, {lon} | Speed: {speed_kmh:.1f} km/h")
elif line.startswith('$GNTXT'):
# Example: $GNTXT,01,01,02,ANT_OPEN,B2,*33
if 'ANT_OK' in line:
print("Antenna connected")
elif 'ANT_OPEN' in line:
print("Antenna missing or broken")

2. Checking data reception (for programmers)

  • Run a simple port read without parsing and ensure that lines arrive at intervals of ~1 second.
  • Ensure that after $GNRMC appears with the A flag, the coordinates and speed are non-zero.
  • If A is missing, the antenna is not receiving a signal from the satellites or is faulty (check $GNTXT).

3. Recommendations for use in production code

  • Use ready-made NMEA parsing libraries (e.g., libnmea for C, pynmea2 for Python, and the gpsd client for various languages).
  • Don't rely on cu or gpsmon in the final solution – they are intended for debugging purposes only.
  • Handle cases with the V flag (invalid data) and read timeouts.