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

Fix corruption of incoming UTF-8-encoded serial data #1758

Merged
merged 2 commits into from
Dec 13, 2022

Conversation

palazzol
Copy link
Contributor

@palazzol palazzol commented Dec 9, 2022

Motivation

Fix for #589

Change description

Fix corruption of incoming UTF8-encoded serial data

Other information

Serial data is received in chunks. When a UTF8-encoded symbol is multibyte, it cannot be decoded properly until all the bytes are available. If an attempt at decoding is made, it will fail and the symbol for UTF-8 "Replacement Character" � is inserted into the stream. TextDecoder has a "stream" option to buffer any bytes that cannot be decoded yet, for exactly this reason. This fix creates an instance of TextDecoder which is persistent for the life of the connection, and then uses that decoder with the stream option to decode UTF8-encoded data properly.

Note - this assumes we expect UTF8 encoded data from the serial port. Generally speaking, this happens if we have the sketch editor in UTF8 mode, which appears to be the default, and then UTF8 encoded characters are included in a print() statement.

Reviewer checklist

  • PR addresses a single concern.
  • The PR has no duplicates (please search among the Pull Requests before creating one)
  • PR title and description are properly filled.
  • Docs have been added / updated (for bug fixes / features)

@kittaakos kittaakos added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Dec 9, 2022
@kittaakos kittaakos self-requested a review December 9, 2022 16:24
Co-authored-by: z3bra <111462146+z3bra5hax@users.noreply.github.com>
Copy link
Contributor

@kittaakos kittaakos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a quality contribution.

The code looks great and #589 works with the changes.

Thank you!

@kittaakos kittaakos requested a review from per1234 December 12, 2022 09:04
@per1234 per1234 linked an issue Dec 13, 2022 that may be closed by this pull request
3 tasks
Copy link
Contributor

@per1234 per1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified this fixes #589

Thanks @palazzol!

@kittaakos kittaakos merged commit 7c86f1f into arduino:main Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serial Monitor periodically corrupts post-ASCII output
4 participants