Python监听串口(RS-232)握手信号 [英] Python monitor serial port (RS-232) handshake signals

查看:155
本文介绍了Python监听串口(RS-232)握手信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要监控串口信号(RI、DSR、CD、CTS)的状态.使用串行"库进行循环和轮询(例如,使用 getRI 函数)过于占用 CPU,并且响应时间不可接受.

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.

python 有解决方案吗?

Is there a solutions with python?

推荐答案

在 Linux 上可以通过阻塞系统调用 TIOCMIWAIT 使用基于中断的通知来监视 RS-232 端口信号引脚的状态变化:

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

这篇关于Python监听串口(RS-232)握手信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆