Skip to content

Fix UnicodeDecodeError in serial_comms by adding safe fallback decodi…#121

Open
daibe wants to merge 1 commit intobabca:masterfrom
daibe:fix/decode-unicode-error
Open

Fix UnicodeDecodeError in serial_comms by adding safe fallback decodi…#121
daibe wants to merge 1 commit intobabca:masterfrom
daibe:fix/decode-unicode-error

Conversation

@daibe
Copy link

@daibe daibe commented Mar 21, 2026

Problem

Receiving SMS messages containing non-UTF-8 characters (e.g. Latin-1 encoded French messages) causes a crash in _readLoop:

UnicodeDecodeError: 'utf-8' codec can't decode byte ...

This leads to termination of the modem RX thread and message loss.

Root Cause

The library assumes all incoming data is UTF-8, but many GSM modems return SMS in Latin-1 or GSM encoding.

Solution

Introduce a safe decoding fallback:

  • Try UTF-8
  • Fallback to Latin-1
  • Final fallback with ignore

Impact

  • Prevents crashes in _readLoop
  • Improves compatibility with real-world GSM networks
  • Maintains backward compatibility

Tested on

  • Huawei USB modem
  • Orange / Airtel SMS (French content with accents)

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

Successfully merging this pull request may close these issues.

1 participant