views:

880

answers:

1

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)

+2  A: 

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

PAG