[PyQt] Wait Dialog and pyserial

Ruslan Popov ruslan.popov at gmail.com
Tue Dec 1 19:15:21 GMT 2009


Hi, all.

I am working on a part of application, the app needs to read RFID id from
label, RFID reader is accessible via COM port (I've used pyserial for that).

I have tried to do so:

[main code]
class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.rfidReader = QWaitCondition()
        self.rfidWaiter = QWaitCondition()
        self.rfidMutex = QMutex()
        self.callback = self.readedRFID
        self.reader = WaitingRFID(self)
        self.reader.start()

..

    def waitingRFID(self):
        self.rfidReader.wakeAll()

        self.dlg = DlgWaitingRFID()
        self.dlg.setModal(True)
        self.dlg.exec_()

        #self.rfidMutex.lock()
        #self.rfidWaiter.wait(self.rfidMutex)
        #self.rfidMutex.unlock()

    def readedRFID(self, rfid):
        self.rfid_id = rfid
        print 'readedRFID:', rfid
        #self.rfidWaiter.wakeAll()
        self.dlg.close()

[Thread code]
class WaitingRFID(QThread):
    def __init__(self, parent):
        self.callback = parent.callback
        self.mutex = parent.rfidMutex
        self.condition = parent.rfidReader
        QThread.__init__(self)

    def hex(self, symbol):
        return '%02X' % ord(symbol)

    def run(self):
        while True:
            port = serial.Serial(PORT['name'], PORT['rate'],
                                 bytesize = PORT['bits_in_byte'],
                                 parity = PORT['parity'],
                                 stopbits = PORT['stop_bits']
                                 )
            port.setDTR(True)
            port.setRTS(True)

            self.mutex.lock()
            self.condition.wait(self.mutex)

            buffer = []
            while True:
                symbol = port.read(1)
                if not self.hex(symbol) == '0D':
                    buffer.append(symbol)
                else:
                    if '0A' == self.hex(port.read(1)):
                        if len(buffer) == 9:
                            rfid_code = ''.join(buffer[1:]) # see manager.py
                            break
                        buffer = []

            self.callback(rfid_code)
            self.mutex.unlock()

            port.close()

Thread code works well. The problem is in showing dialog.
I need to show dialog with a label 'Put RFID label on the reader.' and run
thread's loop. The loop reads RFID id and use callback function. At this
point I need to close the dialog window. Tell me the right way to do it.

Any help appreciated,
-- 
Ruslan Popov
phone: +7 916 926 1205
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20091201/81e68e68/attachment.html


More information about the PyQt mailing list