如何使用 QSocketNotifier (linux) 观看串行端口? [英] How do I watch a serial port with QSocketNotifier (linux)?

查看:48
本文介绍了如何使用 QSocketNotifier (linux) 观看串行端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果/dev/ttyS0 上发生某些事情,有人能给我一个关于如何设置 QSocketNotifier 以触发事件的示例吗?(最好在 python/pyqt4 中)

解决方案

这是一个使用 QSocketNotifier 持续读取文件的示例.只需将那个 'foo.txt' 替换为 '/dev/ttyS0' 就可以了.

<预><代码>导入操作系统从 PyQt4.QtCore 导入 QCoreApplication、QSocketNotifier、SIGNALdef readAllData(fd):缓冲区大小 = 1024为真:数据 = os.read(fd, bufferSize)如果不是数据:休息打印'数据读取:'打印代表(数据)a = QCoreApplication([])fd = os.open('foo.txt', os.O_RDONLY)通知程序 = QSocketNotifier(fd, QSocketNotifier.Read)a.connect(notifier, SIGNAL('activated(int)'), readAllData)a.exec_()

Could someone give me an example on how to setup QSocketNotifier to fire an event if something comes on /dev/ttyS0 ? (preferably in python/pyqt4)

解决方案

Here's an example that just keeps reading from a file using QSocketNotifier. Simply replace that 'foo.txt' with '/dev/ttyS0' and you should be good to go.


import os

from PyQt4.QtCore import QCoreApplication, QSocketNotifier, SIGNAL


def readAllData(fd):
        bufferSize = 1024
        while True:
                data = os.read(fd, bufferSize)
                if not data:
                        break
                print 'data read:'
                print repr(data)


a = QCoreApplication([])

fd = os.open('foo.txt', os.O_RDONLY)
notifier = QSocketNotifier(fd, QSocketNotifier.Read)
a.connect(notifier, SIGNAL('activated(int)'), readAllData)

a.exec_()

这篇关于如何使用 QSocketNotifier (linux) 观看串行端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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