I'd like to be able to detect a BREAK condition on a serial port in Linux. How is this done?
I'd like to detect when a BREAK condition starts and when it stops.
I hoped that if I did:
int serial_status;
ioctl(serial_fd, TIOCMGET, &serial_status);
then there would be a bit value showing a BREAK condition—but it seems there is no such thing.
I found tcsendbreak() in termios.h for sending a break. I also found the tty_ioctl man page which describes how to send a break. But what about receiving a break?
Note: BRKINT has been suggested (which generates the signal SIGINT when a break occurs). But getting a SIGINT isn't such a useful API, for a few reasons:
SIGINT from a user pressing Ctrl-C, when running the program at the terminal.The best answer I've been able to find so far is from the tcsendbreak() man page description of IGNBRK and BRKINT constants for c_iflag in the termios structure. It says:
When neither
IGNBRKnorBRKINTare set, a BREAK reads as a null byte ('\0'), except whenPARMRKis set, in which case it reads as the sequence\377 \0 \0.
(that is, 0xFF 0x00 0x00)
So I guess I should probably set PARMRK and be prepared to do a little processing of the read bytes. This provides explicit information on parity/framing errors (although it's still not totally unambiguous whether 0xFF 0x00 0x00 represents a BREAK or some other parity/framing error).
Note however, I found this patch for PARMRK, which seems to imply there is a danger in older kernels that serial bytes could be dropped when PARMRK is being used.
It's also not clear if these bytes are sent continuously as long as the BREAK condition lasts, or if it's only sent once at the beginning of the BREAK condition. So it's not clear if the end of a BREAK condition can be detected via this method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With