pythonpyqt5qt-signalsqtserialport

Is it possible to use PyQt5 QtSerialPort without GUI's or classes?


The point would be to have the functionality of reading only when there is something to be read, instead of using pyserial which doesn't have a special method for that. I guess this may go into a bigger question of whether signals and slots could be used without a GUI classes (that inherit from other objects). I could get the serial port to write, but not read, with

from PyQt5 import QtCore, QtSerialPort
serial_port = QtSerialPort.QSerialPort('COM3')
serial_port.open(QtCore.QIODevice.ReadWrite)
serial_port.write(bytes([255]))

def handle_ready_read():
    while serial_port.canReadLine():
        print(serial_port.readAll())
        print('here')
        serial_port.close()

serial_port.readyRead.connect(handle_ready_read)

Nothing prints out, even though something is read when using pyserial.


Solution

  • You do not need GUI to use Qt. There is a dedicated GUI module in Qt and what does not depend on it, doesn't need a GUI.

    However, to use slots and signals you need to run Qt's event loop. The regular way would be to use a QCoreApplication.

    app = QCoreApplication([])
    
    # setup your serial port here
    
    sys.exit(app.exec_())