add try-except hint for gps decode error (#374)

* add try-except hint for gps decode error

* try-except in code samples to fix gps decode error
pull/376/head
Mauricio Buschinelli 3 years ago committed by GitHub
parent 82e1859de2
commit 692356937e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,10 +22,14 @@ def print_gps_data(line):
print(f'{lat},{lon} - from {msg.num_sats} satellites')
while True:
line = serial.readline().decode('utf-8')
try:
line = serial.readline().decode('utf-8')
while len(line) > 0:
print_gps_data(line)
line = serial.readline().decode('utf-8')
while len(line) > 0:
print_gps_data(line)
except UnicodeDecodeError:
line = serial.readline().decode('utf-8')
time.sleep(1)

@ -9,10 +9,14 @@ def print_gps_data():
print(line.rstrip())
while True:
line = serial.readline().decode('utf-8')
try:
line = serial.readline().decode('utf-8')
while len(line) > 0:
print_gps_data()
line = serial.readline().decode('utf-8')
while len(line) > 0:
print_gps_data()
except UnicodeDecodeError:
line = serial.readline().decode('utf-8')
time.sleep(1)

@ -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!

Loading…
Cancel
Save