From a70bdbc18c240945b08a6154ceeb6e671a94b46c Mon Sep 17 00:00:00 2001 From: Tristan Date: Sat, 5 Apr 2025 15:00:03 -0700 Subject: [PATCH 1/4] Change RMC parsing so that datetime values are available even when there's no GPS fix. Fixes https://github.com/adafruit/Adafruit_CircuitPython_GPS/issues/105 --- adafruit_gps.py | 4 +++- tests/adafruit_gps_test.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/adafruit_gps.py b/adafruit_gps.py index aa98d46..5b5b50a 100644 --- a/adafruit_gps.py +++ b/adafruit_gps.py @@ -26,6 +26,7 @@ https://github.com/adafruit/circuitpython/releases """ + import time from micropython import const @@ -61,7 +62,7 @@ # 0 - _GLL "dcdcscC", # 1 - _RMC - "scdcdcffsDCC", + "scDCDCFFsDCC", # 2 - _GGA "sdcdciiffsfsIS", # 3 - _GSA @@ -588,6 +589,7 @@ def _parse_rmc(self, data: List[str]) -> bool: self.fix_quality = 1 else: self.fix_quality = 0 + return True # break early since no fix means no following values will be populated # Latitude self.latitude = _read_degrees(parsed_data, 2, "s") diff --git a/tests/adafruit_gps_test.py b/tests/adafruit_gps_test.py index 291fd09..0663eee 100644 --- a/tests/adafruit_gps_test.py +++ b/tests/adafruit_gps_test.py @@ -162,6 +162,18 @@ def test_GPS_update_timestamp_timestamp_utc_was_not_none_new_date_none(): assert gps.timestamp_utc == exp_struct +def test_GPS_update_time_from_RTC_without_fix(): + r = b"$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,N*4E\r\n" + with mock.patch.object(GPS, "readline", return_value=r): + gps = GPS(uart=UartMock()) + gps.update() + exp_time = time.struct_time((2025, 4, 5, 21, 6, 48, 0, 0, -1)) + assert gps.has_fix is False + assert gps.timestamp_utc == exp_time + assert gps.datetime == exp_time + assert gps.nmea_sentence == "$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,N*4E" + + def test_GPS_update_with_unknown_talker(): r = b"$XYRMC,215032.086,A,1234.5678,N,00123.12345,E,0.45,56.35,021021,,,A*7c\r\n" with mock.patch.object(GPS, "readline", return_value=r): From b02081f900da191e97eaf9c1a424826fdc00525f Mon Sep 17 00:00:00 2001 From: Tristan Date: Thu, 4 Sep 2025 12:27:47 -0700 Subject: [PATCH 2/4] Make same RMC parsing update to the newer RMC_4_1 format --- adafruit_gps.py | 2 +- tests/adafruit_gps_test.py | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/adafruit_gps.py b/adafruit_gps.py index 5b5b50a..0613693 100644 --- a/adafruit_gps.py +++ b/adafruit_gps.py @@ -78,7 +78,7 @@ # 8 - _GSV19 "iiiiiiIiiiIiiiIiiiI", # 9 - _RMC_4_1 - "scdcdcffsDCCC", + "scDCDCFFsDCCC", # 10 - _VTG "fcFCfcfcC", ) diff --git a/tests/adafruit_gps_test.py b/tests/adafruit_gps_test.py index 0663eee..a29ce91 100644 --- a/tests/adafruit_gps_test.py +++ b/tests/adafruit_gps_test.py @@ -166,7 +166,7 @@ def test_GPS_update_time_from_RTC_without_fix(): r = b"$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,N*4E\r\n" with mock.patch.object(GPS, "readline", return_value=r): gps = GPS(uart=UartMock()) - gps.update() + assert gps.update() is True exp_time = time.struct_time((2025, 4, 5, 21, 6, 48, 0, 0, -1)) assert gps.has_fix is False assert gps.timestamp_utc == exp_time @@ -174,6 +174,21 @@ def test_GPS_update_time_from_RTC_without_fix(): assert gps.nmea_sentence == "$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,N*4E" +def test_GPS_update_time_from_RTC_without_fix_RMC_4_1(): + # RMC_4_1 has an extra "Mode" parameter right before the checksum. + r = b"$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,A,N*23\r\n" + with mock.patch.object(GPS, "readline", return_value=r): + gps = GPS(uart=UartMock()) + assert gps.update() is True + exp_time = time.struct_time((2025, 4, 5, 21, 6, 48, 0, 0, -1)) + assert gps.has_fix is False + assert gps.timestamp_utc == exp_time + assert gps.datetime == exp_time + assert ( + gps.nmea_sentence == "$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,A,N*23" + ) + + def test_GPS_update_with_unknown_talker(): r = b"$XYRMC,215032.086,A,1234.5678,N,00123.12345,E,0.45,56.35,021021,,,A*7c\r\n" with mock.patch.object(GPS, "readline", return_value=r): From 79c6af3cbda12e8d83194ad6cfd9d81dbc12f088 Mon Sep 17 00:00:00 2001 From: Tristan Date: Thu, 4 Sep 2025 16:25:16 -0700 Subject: [PATCH 3/4] formatting fix --- tests/adafruit_gps_test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/adafruit_gps_test.py b/tests/adafruit_gps_test.py index a29ce91..787e4fd 100644 --- a/tests/adafruit_gps_test.py +++ b/tests/adafruit_gps_test.py @@ -184,10 +184,7 @@ def test_GPS_update_time_from_RTC_without_fix_RMC_4_1(): assert gps.has_fix is False assert gps.timestamp_utc == exp_time assert gps.datetime == exp_time - assert ( - gps.nmea_sentence == "$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,A,N*23" - ) - + assert gps.nmea_sentence == "$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,A,N*23" def test_GPS_update_with_unknown_talker(): r = b"$XYRMC,215032.086,A,1234.5678,N,00123.12345,E,0.45,56.35,021021,,,A*7c\r\n" From 2a85a4809fea70114cb8eb9a272317db6fcae283 Mon Sep 17 00:00:00 2001 From: Tristan Date: Thu, 4 Sep 2025 16:30:15 -0700 Subject: [PATCH 4/4] another formatting fix --- tests/adafruit_gps_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/adafruit_gps_test.py b/tests/adafruit_gps_test.py index 787e4fd..e59f0de 100644 --- a/tests/adafruit_gps_test.py +++ b/tests/adafruit_gps_test.py @@ -186,6 +186,7 @@ def test_GPS_update_time_from_RTC_without_fix_RMC_4_1(): assert gps.datetime == exp_time assert gps.nmea_sentence == "$GPRMC,210648.000,V,,,,,0.71,105.86,050425,,,A,N*23" + def test_GPS_update_with_unknown_talker(): r = b"$XYRMC,215032.086,A,1234.5678,N,00123.12345,E,0.45,56.35,021021,,,A*7c\r\n" with mock.patch.object(GPS, "readline", return_value=r):