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?
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(),
)
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