Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python monitor serial port (RS-232) handshake signals

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?

like image 780
user740818 Avatar asked May 05 '11 22:05

user740818


1 Answers

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(),
        )
like image 180
fdb Avatar answered Oct 14 '22 07:10

fdb