Using QModbusReply in a thread

Viewed 10

I want to call qmodbusreply related monitoring function in the std thread.

void DigitalInputController::monitoring()
{
    QModbusReply* reply = nullptr;
    QModbusDataUnit d(QModbusDataUnit::Coils, 0, 8);

    reply = m_pDevice->sendReadRequest(d, m_DeviceID);

    if (reply)
    {
        if (!reply->isFinished())
        {
            connect(reply, &QModbusReply::finished, this, &DigitalInputController::receivedDigitalInputData);
        }
        else
        {
            delete reply;
        }
    }
}

The mainwindow source is as below.

:
:
std::thread th(&MainWindow::serialCommandQueueMonitoringThread, this);
th.detach();

for (int i=0; i<4; i++)
{
    m_pPickingController[i]->m_pDigitalInputController->moveToThread(this->thread());
    m_pPickingController[i]->moveToThread(this->thread());
}    
:
:
void MainWindow::serialCommandQueueMonitoringThread()
{
    while(!m_bCheckThreadStopFlag)
    {
        if (g_queueSerialCommandTypeInfo->size() > 0)
        {
            g_mutexSerialCommand->lock();
            SerialCommandTypeInfo info = g_queueSerialCommandTypeInfo->dequeue();
            g_mutexSerialCommand->unlock();

            SerialCommandParamter p = info.parameter;

            if (info.type == MONITORING_DIGITALINPUT)
            {
                m_pPickingController[p.objectIndex]->m_pDigitalInputController->monitoring();
            }
        }
        QThread::msleep(20);
    }
}

When executed, the following warning appears and the monitoring function cannot be called.

QObject: Cannot create children for a parent that is in a different thread. (Parent is QModbusRtuSerialMaster(0x2c56de33090), parent's thread is QThread(0x2c5670790b0), current thread is QThread(0x2c56ddb6290) QObject::startTimer: Timers can only be used with threads started with QThread

How should I do?

0 Answers
Related