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 pyserial forcing nonblocking serial device on Linux and spurious null bytes #8

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jzhan357
Copy link

@jzhan357 jzhan357 commented Mar 1, 2023

On Linux, pyserial forces vmin to 0 if inter_byte_timeout is None, which is the default behavior.
When vmin is 0, reads to the device can return a minimum of 0 bytes, in other words they return immediately if no data is buffered.
This breaks tools that assume reading from the serial device will block until data is found.
See this thread for more details.

In some cases, spurious null bytes are emitted due to break conditions being interpreted as nulls.
This is fixed by enabling BRKINT and NOFLSH, which will prevent break conditions from being interpreted as null bytes.

@jzhan357 jzhan357 changed the title Fix pyserial forcing nonblocking serial device on Linux Draft: Fix pyserial forcing nonblocking serial device on Linux Mar 1, 2023
@jzhan357 jzhan357 changed the title Draft: Fix pyserial forcing nonblocking serial device on Linux Fix pyserial forcing nonblocking serial device on Linux Mar 1, 2023
@jzhan357
Copy link
Author

jzhan357 commented Mar 5, 2023

A bit more poking and I've narrowed down the problem to brkint being turned off in some circumstances.
This leads to break conditions being interpreted as 0x00, which is why we see null bytes inserted between messages.
Either brkint or ignbrk need to be set so that this doesn't happen.

@eggroll-bot
Copy link

Adding an update here from what was discussed in Slack:

ignbrk is cleared by pySerial when the serial port is opened, so this cannot be set before the serial port is opened by pySerial. brkint cannot be used because it clears the terminal's input and output buffers upon a break condition resulting in the loss of messages when a break condition occurs.

A minimally reproducible example of the spurious nulls can be found here: https://git.ucsc.edu/ucsc-ectf-2023/serial-null-bytes. I'm seeing null characters from break conditions at the beginning and end of the "abcd" message sent by this example when testing with the bridge.

@eggroll-bot
Copy link

Some further testing indicates that ignbrk and echo need to be set after the serial port is opened.

@jzhan357
Copy link
Author

jzhan357 commented Mar 9, 2023

Enabling echo is causing failures in firmware loading.

@jzhan357 jzhan357 changed the title Fix pyserial forcing nonblocking serial device on Linux Fix pyserial forcing nonblocking serial device on Linux and spurious null bytes Mar 10, 2023
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.

2 participants