SerialPort in a different Thread
-
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 { Q_OBJECT QThread modbusThread; .... .... }
HauptDesign::HauptDesign(QWidget *parent) : QMainWindow(parent) ,ui(new Ui::HauptDesign) { ui->setupUi(this); qRegisterMetaType<QList<int> >(); qRegisterMetaType<QModbusDevice::State>(); modbusDevice = new Modbus; modbusDevice->moveToThread(&modbusThread); connect(modbusDevice, &Modbus::signalConnectionState, this, &HauptDesign::VerbindungConnection); connect(modbusDevice, &Modbus::signalNewDataRegs, this, &HauptDesign::receiveModbusRegister); ... connect(this, &HauptDesign::connectDevice, modbusDevice, &Modbus::connectDevice); modbusThread.start();
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
messages.
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 -
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.