Come faccio a guardare una porta seriale con QSocketNotifier (Linux)?
-
16-09-2019 - |
Domanda
Qualcuno mi potrebbe dare un esempio su come impostare QSocketNotifier al fuoco un evento se qualcosa viene su / dev / ttyS0 ? (Preferibilmente in pitone / PyQt4)
Soluzione
Ecco un esempio che continua a leggere da un file utilizzando QSocketNotifier. Semplicemente sostituire quella 'foo.txt' con '/ dev / ttyS0' e si dovrebbe essere pronti per partire.
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_()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow