问题:当我在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();
}
当点击读取按钮,触发读取寄存器的操作,同时跨线程触发了消息提示框的弹出(必须跨线程调用才会出现这个问题)。这个问题不是每次点击都会出现,有几率触发。