如何在Linux中使用QSocketNotifier监视串口?

2 投票
1 回答
2993 浏览
提问于 2025-04-15 11:14

有没有人能给我一个例子,教我怎么设置QSocketNotifier,让它在/dev/ttyS0上有数据到来时触发一个事件?(最好是用python/pyqt4来写)

1 个回答

5

这里有一个例子,它使用QSocketNotifier不断地从一个文件中读取数据。你只需要把'foo.txt'替换成'/dev/ttyS0',就可以开始使用了。


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

撰写回答