views:

734

answers:

1
+1  Q: 

pyqt4 and pyserial

I want to do an app constantly watching the serial port and changing the user interface according to the input received from the port. I've managed to read lines from the port with pyserial under Linux, but I'm not sure how to do this in a regular fashion: create a separate thread and check for input on a timer event? How do i make sure I don't miss anything? (implementing some kind of handshake/protocol seems like an overkill for this...) And most importantly: How do I do it with the facilities of qt4?

Edit: This is what I'm doing now (I want to do this periodically with the rest of the app running and not waiting)

class MessageBox(QtGui.QWidget): def init(self, parent=None): QtGui.QWidget.init(self, parent)

    ser = serial.Serial('/dev/ttyS0', 9600, bytesize=serial.EIGHTBITS,
    parity=serial.PARITY_NONE,     
    stopbits=serial.STOPBITS_ONE, 
    timeout=None,           
    xonxoff=0,              
    rtscts=0,
    interCharTimeout=None)

    self.label = QtGui.QLabel(ser.readline(), self)
    self.label.move(15, 10)
    ser.close()
    self.setGeometry(300, 300, 250, 150)
    self.setWindowTitle('Authentication')

    self.color = QtGui.QColor(0, 0, 0) 

    self.square = QtGui.QWidget(self)
    self.square.setGeometry(120, 20, 100, 100)
    self.square.setStyleSheet("QWidget { background-color: %s }" % self.color.name())
+2  A: 

You won't miss any bytes, any pending input is buffered.

You have several options:

  1. use a thread that polls the serial port with PySerial/inWaiting()

  2. Use a timer in the main thread that polls the serial port with PySerial/inWaiting.

  3. find the handle of the port and pass it to QSocketNotifier. This works only on linux but in that case, QSocketNotifier will watch the file associated with your serial port and send a signal when there's something available.

Method 2 and 3 are better because you don't need a thread.

Bluebird75