How do I watch a serial port with QSocketNotifier (linux)?
-
16-09-2019 - |
Question
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)
Solution
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_()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow