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:
- I can't tell what serial port it comes from, in a multiple-serial-port scenario.
- I can also get
SIGINT
from a user pressing Ctrl-C, when running the program at the terminal. - If I'm running my program as a daemon, then the proviso "if the terminal is the controlling terminal of a foreground process group" wouldn't be true, would it?
- It's not possible to know how long the BREAK condition continues, and when it stops.
BRKINT
at thattermios
manpage you're linking to.BRKINT
(generatingSIGINT
) is useful.TTY_BREAK
, which is a flag typically stuffed into the receive buffer when abnormal receive conditions (e.g. parity error, framing error, overrun or break) occur. However the line discipline (the next stage of processing before userland gets the Rx data) will typically filter out the flags from the Rx data. I vaguely recall one version of a line discipline that inserted 20 NULs for a break condition. Or maybe 3 bytes of0xff
and0x00
and0x00
-- lxr.free-electrons.com/source/drivers/char/…