Better GPS read
This commit is contained in:
@@ -36,23 +36,17 @@ class GROVEGPS:
|
|||||||
def read(self):
|
def read(self):
|
||||||
"""Reads the latest GGA sentence from serial, thread-safely."""
|
"""Reads the latest GGA sentence from serial, thread-safely."""
|
||||||
with serial_lock:
|
with serial_lock:
|
||||||
# 1. Flush accumulated stale data in the UART buffer
|
for _ in range(10): # Read pending lines from buffer
|
||||||
if self.ser.in_waiting > 0:
|
if self.ser.in_waiting == 0:
|
||||||
self.ser.reset_input_buffer()
|
break
|
||||||
|
|
||||||
# 2. Try reading up to 15 lines to catch the freshest GGA sentence
|
|
||||||
for _ in range(5):
|
|
||||||
raw_bytes = self.ser.readline()
|
raw_bytes = self.ser.readline()
|
||||||
try:
|
try:
|
||||||
line = raw_bytes.decode('utf-8', errors='ignore').strip()
|
line = raw_bytes.decode('utf-8', errors='ignore').strip()
|
||||||
# log(f"GPS: Read line: {line}")
|
|
||||||
except Exception:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Supports both $GPGGA and modern $GNGGA sentences
|
|
||||||
if (line.startswith('$GPGGA') or line.startswith('$GNGGA')) and calculate_nmea_checksum(line):
|
if (line.startswith('$GPGGA') or line.startswith('$GNGGA')) and calculate_nmea_checksum(line):
|
||||||
if self.parse_gga(line):
|
return self.parse_gga(line)
|
||||||
return True
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def parse_gga(self, line):
|
def parse_gga(self, line):
|
||||||
|
|||||||
Reference in New Issue
Block a user