SerialPort in a different Thread

  • Moderators

    H everyone,

    So I've written a class that handles the serialport and data-transfer via ModBus.

    The idea was to move the class to an other thread and run the data exchange idependet from the UI.

    Everything seemed to work, but a Qt::BlockingQueuedConnection resulted in a deadlock -> Obviously I did something wrong. So I reworked it to this:

    class HauptDesign : public QMainWindow
        QThread modbusThread;
    HauptDesign::HauptDesign(QWidget *parent) :
        ,ui(new Ui::HauptDesign)
        qRegisterMetaType<QList<int> >();
        modbusDevice    = new Modbus;
    connect(modbusDevice, &Modbus::signalConnectionState, this, &HauptDesign::VerbindungConnection);
    connect(modbusDevice, &Modbus::signalNewDataRegs, this, &HauptDesign::receiveModbusRegister);
    connect(this, &HauptDesign::connectDevice, modbusDevice, &Modbus::connectDevice);

    It works, I can successfully exchange data, but my Debug screen is flooded with

    QObject::killTimer: timers cannot be stopped from another thread
    QObject::startTimer: timers cannot be started from another thread

    I don't use Timers in that class, I have not even defined one in the' modbusDevice' class. QModbusClient obviously has a connection timeout what might cause this issue!?

    I'm at a loss how to do this the correct way.
    Any help is appreciated

  • Moderators

    Never mind,

    I had code in my the constructor of the to be threaded class, that caused the conflict...

    connect(&modbusThread, &QThread::started, modbusDevice, &Modbus::setupClass);

    fixed the issue.