Skip to content
Advertisement

Serial socket – ignore POLLHUP received in non-canonical mode?

I have a Raspberry Pi connected via UART to a microcontroller. The code on the RPI is trying to read incoming non-canonical UART data, but randomly receives POLLHUP. I have been able to recover by closing and reopening the file, but this is less than ideal.

Is there a way to disable the disconnect detection behavior of termios in Linux? I am not sure why the POLLHUP is being raised in the first place. I suspect that some control characters are still being interpreted despite my call to cfmakeraw(). The cable is unlikely to be the problem as canonical debug output works fine (admittedly over different pins, but same baud and same type of cable).

Sample code, setup:

JavaScript

Sample code, Rx:

JavaScript

TL;DR: The code above has a branch which handles POLLHUP by closing and reopening the serial device. I am talking to a device that sends raw bytes and would prefer it if termios in Linux does not make the file descriptor unusable in case of POLLHUP. Ideally they should also entirely ignore whatever control character is causing this, if it is a control character. Is there a way to do this?

Advertisement

Answer

The POLLHUP issue was resolved by setting the baud rate correctly.

My original code had a call to cfsetispeed(&portSettings, 115200);. This is wrong, B115200 needs to be passed instead. B115200 is a constant that usually resolves to something unpredictable (example).

I recommend not copying from my code but rather using this example for a basic raw tty setup.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement