Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch errors when decoding ASCII #3

Open
ElectricRCAircraftGuy opened this issue Feb 29, 2024 · 1 comment
Open

Catch errors when decoding ASCII #3

ElectricRCAircraftGuy opened this issue Feb 29, 2024 · 1 comment

Comments

@ElectricRCAircraftGuy
Copy link
Owner

Do a try/catch around this line, and gracefully handle errors when the chars are binary instead of ASCII:

data_str = data_bytes.decode('ascii')

Sometimes garbled data comes over serial, crashing the program otherwise when an invalid (ie: binary instead of ASCII) byte arrives.

@ElectricRCAircraftGuy
Copy link
Owner Author

Example error:

auto-flusTraceback (most recent call last):
  File "C:\Users\gabriel\GS\dev\eRCaGuy_PyTerm\serial_terminal.py", line 264, in <module>

    main()
  File "C:\Users\gabriel\GS\dev\eRCaGuy_PyTerm\serial_terminal.py", line 151, in main
    data_str = data_bytes.decode('ascii')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf9 in position 3: ordinal not in range(128)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant