From 04ec0c33be74ee63385b5e7e8c098235552db5eb Mon Sep 17 00:00:00 2001 From: Mauricio Buschinelli Date: Thu, 21 Apr 2022 11:23:55 -0400 Subject: [PATCH] add try-except hint for gps decode error --- .../1-location-tracking/pi-gps-sensor.md | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/3-transport/lessons/1-location-tracking/pi-gps-sensor.md b/3-transport/lessons/1-location-tracking/pi-gps-sensor.md index 01148da1..8344dc19 100644 --- a/3-transport/lessons/1-location-tracking/pi-gps-sensor.md +++ b/3-transport/lessons/1-location-tracking/pi-gps-sensor.md @@ -149,13 +149,31 @@ Program the device. $BDGSV,1,1,00*68 ``` - > If you get one of the following errors when stopping and restarting your code, kill the VS Code terminal, then launch a new one and try again. + > If you get one of the following errors when stopping and restarting your code, add a `try - except` block to your while loop. ```output UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 0: invalid start byte UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 0: invalid continuation byte ``` + ```python + while True: + try: + line = serial.readline().decode('utf-8') + + while len(line) > 0: + print_gps_data() + line = serial.readline().decode('utf-8') + + # There's a random chance the first byte being read is part way through a character. + # Read another full line and continue. + + except UnicodeDecodeError: + line = serial.readline().decode('utf-8') + + time.sleep(1) + ``` + > 💁 You can find this code in the [code-gps/pi](code-gps/pi) folder. 😀 Your GPS sensor program was a success!