Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive/read BREAK condition on Linux serial port

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.
like image 347
Craig McQueen Avatar asked Nov 03 '22 04:11

Craig McQueen


1 Answers

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 IGNBRK nor BRKINT are set, a BREAK reads as a null byte ('\0'), except when PARMRK is 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.

like image 60
Craig McQueen Avatar answered Nov 15 '22 06:11

Craig McQueen