Python monitor serial port (RS-232) handshake signals
Asked Answered
A

1

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?

Andaman answered 5/5, 2011 at 22:37 Comment(0)
N
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(),
        )
Nefen answered 5/5, 2011 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.