Retrieving / reading a BREAK condition for a Linux serial port

I would like to be able to detect a BREAK condition on a serial port in Linux. How it's done?

I would like to know when BREAK starts and when it stops.

I was hoping that if I did:

int serial_status; ioctl(serial_fd, TIOCMGET, &serial_status); 

then there will be a bit showing the BREAK condition, but it looks like there is no such thing.

I found tcsendbreak() in termios.h to post a break. I also found the tty_ioctl man page that describes how to post a break. But what about getting a break?

Note: BRKINT been suggested (which generates a SIGINT signal when a break occurs). But getting SIGINT not such a useful API, for several reasons:

  • I canโ€™t tell which serial port this comes from in a scenario with multiple serial ports.
  • I can also get SIGINT from the user pressing Ctrl-C when starting the program on the terminal.
  • If I run my program as a daemon, then the condition "if the terminal is the control terminal of the foreground process group" will not be true, will it?
  • It is not possible to know how long the BREAK condition lasts, and when it stops.
+4
source share
1 answer

The best answer I've been able to find so far is to describe the tcsendbreak() page of IGNBRK and BRKINT constants for c_iflag in the termios structure. It says:

If neither IGNBRK nor BRKINT are set, BREAK is read as zero byte ( '\0' ), unless PARMRK set, in which case it is read as sequence \377 \0 \0 .

(i.e. 0xFF 0x00 0x00 )

Therefore, probably, I should install PARMRK and be ready to process the read bytes a little. This gives clear information about parity / framing errors (although this is still not entirely clear whether 0xFF 0x00 0x00 BREAK or some other parity / framing error).

Please note, however, that I found this patch for PARMRK , which apparently means that in older kernels there is a danger that consecutive bytes may be when using PARMRK .

It is also unclear whether these bytes will be sent continuously while the BREAK condition is met, or if it is sent only once at the beginning of the BREAK condition. Thus, it is unclear whether the end of the BREAK clause can be detected using this method.

+1
source

All Articles