akosch
akosch

Reputation: 4396

How do I watch a serial port with QSocketNotifier (linux)?

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)

Upvotes: 2

Views: 3012

Answers (1)

PAG
PAG

Reputation: 1946

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

Upvotes: 5

Related Questions