void MainWindow::serialPortCtrl(){ if(ui->pushButton->isChecked()) { QThread* thread_device = new QThread; CModbus* newdevice = new CModbus; newdevice->moveToThread(thread_device); newdevice->m_device.moveToThread(thread_device); connect(thread_device, SIGNAL(started()), newdevice, SLOT(connectModbusDevice())); connect(newdevice, &CModbus::disconnectDeviceFinish, thread_device, &QThread::quit); connect(newdevice, &CModbus::disconnectDeviceFinish, this, &MainWindow::disconnectDevice); connect(thread_device, &QThread::finished, newdevice, &CModbus::deleteLater); connect(newdevice, &CModbus::connectDeviceOk, this, &MainWindow::connectStateOk); connect(newdevice, &CModbus::errorDevice, this, &MainWindow::errorDevice); connect(this, &MainWindow::signalDisconnectDevice, newdevice, &CModbus::disconnectModbusDevice); connect(ui->pushButtonSend, &QPushButton::clicked, newdevice, &CModbus::modbusRequest); thread_device->start(); } else { emit signalDisconnectDevice(); }}
QObject::startTimer: Timers cannot be started from another thread
void CModbus::modbusRequest(){ QModbusDataUnit unit(QModbusDataUnit::InputRegisters, 64, 10); QModbusReply* reply; reply = m_device.sendReadRequest(unit, 0x01); if(reply) { if(!reply->isFinished()) { connect(reply, &QModbusReply::finished, this, &CModbus::modbusReadReady); } else { delete reply; } } else emit errorDevice(m_device.errorString());}