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)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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_()