QT Modbus ASSERT failure in processQueue: “response timer active“

博客讲述了在QT中使用Modbus进行读取操作时遇到的程序卡死问题。问题出现在点击读取按钮后,有时会触发消息提示框并导致程序挂起。关键代码涉及QModbusReply的使用、信号槽连接以及线程间的通信。问题可能是由于不正确的线程同步或资源管理导致的。
摘要由CSDN通过智能技术生成

问题:当我在QT中用modbus读取函数时,出现程序卡死的问题。

现象:每次点读取有概率出现这个情况。在这里插入图片描述
主要代码如下:

	//mythread为子线程的类,必须的跨线程不然不会触发这个问题
    connect(ui->pushButtonTest,&QPushButton::clicked,thread,&mythread::on_showMessage);
    connect(ui->pushButtonTest,&QPushButton::clicked,this,&MainWindow::on_pushButtontest_clicked);
    connect(thread,&mythread::showTips,this,&MainWindow::on_Message);

void mythread::on_showMessage()
{
    emit showTips();
}

void MainWindow::on_pushButtontest_clicked()
{
    if (!modbusDevice)
        return;
        
    statusBar()->clearMessage();

	//读取保持寄存器的值,由于是测试,这里直接写为固定的
    const QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters,0,10);
    if (auto *reply = modbusDevice->sendReadRequest(readUnit, 1))
    {
        if (!reply->isFinished())
        {
            connect(reply, &QModbusReply::finished, this, &MainWindow::readReady);
        }
        else
        {
            delete reply; // broadcast replies return immediately
        }
    }
    else
    {
        statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000);
    }
}

void MainWindow::readReady()
{
    auto reply = qobject_cast<QModbusReply *>(sender());
    if (!reply)
        return;

    if (reply->error() == QModbusDevice::NoError)
    {
        const QModbusDataUnit unit = reply->result();
        for (uint i = 0; i < unit.valueCount(); i++)
        {
            const QString entry = tr("Address: %1, Value: %2").arg(unit.startAddress() + i)
                                     .arg(QString::number(unit.value(i),
                                          unit.registerType() <= QModbusDataUnit::Coils ? 10 : 16));
            qDebug() << i << ":" << entry;
        }
    }
    else if (reply->error() == QModbusDevice::ProtocolError)
    {
        statusBar()->showMessage(tr("Read response error: %1 (Mobus exception: 0x%2)").
                                    arg(reply->errorString()).
                                    arg(reply->rawResult().exceptionCode(), -1, 16), 5000);
    }
    else
    {
        statusBar()->showMessage(tr("Read response error: %1 (code: 0x%2)").
                                    arg(reply->errorString()).
                                    arg(reply->error(), -1, 16), 5000);
    }

    reply->deleteLater();
}


void MainWindow::on_Message()
{
    QMessageBox box;
    box.setText("读取成功");
    box.setWindowTitle("提示");

    QFont font;
    font.setFamily("微软雅黑");
    font.setPixelSize(15);
    box.setFont(font);

    QPushButton* messageButton = box.addButton(tr("好的"),QMessageBox::YesRole);
    messageButton->setFixedSize(70,25);
    box.setStyleSheet("QLabel{"
                               "min-width: 200px;"
                               "min-height: 70px;"
                               "}");

    box.exec();
}

当点击读取按钮,触发读取寄存器的操作,同时跨线程触发了消息提示框的弹出(必须跨线程调用才会出现这个问题)。这个问题不是每次点击都会出现,有几率触发。
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值