2009-04-25 28 views

回答

5

下面是一個只使用QSocketNotifier持續讀取文件的例子。只需用'/ dev/ttyS0'替換'foo.txt',你應該很好。


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