11

I need to monitor the status of serial port signals (RI, DSR, CD,CTS). Looping and polling with 'serial' library, (eg. using functions getRI) is too cpu intensive and response time is not acceptable.

Is there a solutions with python?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user740818
  • 113
  • 1
  • 5

1 Answers1

14

On Linux is possible to monitor che state change of a signal pin of an RS-232 port using interrupt based notification throught the blocking syscall TIOCMIWAIT:

from serial import Serial
from fcntl import  ioctl
from termios import (
    TIOCMIWAIT,
    TIOCM_RNG,
    TIOCM_DSR,
    TIOCM_CD,
    TIOCM_CTS
)

ser = Serial('/dev/ttyUSB0')

wait_signals = (TIOCM_RNG |
                TIOCM_DSR |
                TIOCM_CD  |
                TIOCM_CTS)

if __name__ == '__main__':
    while True:
        ioctl(ser.fd, TIOCMIWAIT, wait_signals)
        print 'RI=%-5s - DSR=%-5s - CD=%-5s - CTS=%-5s' % (
            ser.getRI(),
            ser.getDSR(),
            ser.getCD(),
            ser.getCTS(),
        )
fdb
  • 1,998
  • 1
  • 19
  • 20