You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
https://56k.es/fanta/utilizando-un-modulo-gps-usb-barato-en-gnulinux/
apt install python3-nmea2 python3-serial
NMEA 0183 PROTOCOL: https://www.serialmon.com/protocols/nmea0183.shtml
'Timestamp', 'timestamp'
'Latitude', 'lat'
'Latitude Direction', 'lat_dir'
'Longitude', 'lon'
'Longitude Direction', 'lon_dir'
'GPS Quality Indicator', 'gps_qual', int
'Number of Satellites in use', 'num_sats'
'Horizontal Dilution of Precision', 'horizontal_dil'
'Antenna Alt above sea level (mean)', 'altitude', float
'Units of altitude (meters)', 'altitude_units'
'Geoidal Separation', 'geo_sep'
'Units of Geoidal Separation (meters)', 'geo_sep_units'
'Age of Differential GPS Data (secs)', 'age_gps_data'
'Differential Reference Station ID', 'ref_station_id'
"""
import io
import os
import sys
import pynmea2
import serial
if os.geteuid() != 0:
sys.exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo' for example. Exiting.")
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=5.0)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
while 1:
try:
line = sio.readline()
msg = pynmea2.parse(line)
if msg.talker == "GP" and msg.sentence_type == "GGA":
print("[", msg.timestamp, "] [ Satelites:", msg.num_sats, "] - Lat:", msg.lat, "Lon:", msg.lon, "Alt:", msg.altitude)
except serial.SerialException as e:
print('Device error: {}'.format(e))
break
except pynmea2.ParseError as e:
print('Parse error: {}'.format(e))
continue