关于QModbusReply返回QModbusDataUnit不正确问题



  • 根据Qt自带实例创建了一个Modbus程序,但是读取寄存器单元时可以正常返回数据,读取线圈变量时返回错误数据,谁能帮我看看是怎么回事?



  • #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QMessageBox>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    create_modbus_client();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::create_modbus_client()
    {
    modbus_client=new QModbusRtuSerialMaster(this);
    connect(modbus_client, &QModbusClient::errorOccurred, this {
    QMessageBox::warning(this,"QModbus is error",QString("%1").arg(modbus_client->error()));
    QMessageBox::warning(this,"QModbus is error",QString("%1").arg(modbus_client->errorString()));
    return false;
    });
    modbus_client->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
    tr("COM1"));
    modbus_client->setConnectionParameter(QModbusDevice::SerialParityParameter,
    QSerialPort::EvenParity);
    modbus_client->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
    QSerialPort::Baud9600);
    modbus_client->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
    QSerialPort::Data8);
    modbus_client->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
    QSerialPort::OneStop);
    modbus_client->setTimeout(1000);
    modbus_client->setNumberOfRetries(3);
    if (!modbus_client->connectDevice()) {
    QMessageBox::warning(this,"Error",tr("Connect failed: ") + modbus_client->errorString());
    return;
    }
    else
    {

        QMessageBox::about(this,"Congratulation",QString("Create Serial Port Succefully,the name is %1,"
                                                         "the parity is %2,the bandrate is %3,the databits is %4,"
                                                         "the stopbits is %5").arg(modbus_client->connectionParameter(QModbusDevice::SerialPortNameParameter).toString()).
                                                                               arg(modbus_client->connectionParameter(QModbusDevice::SerialParityParameter).toString()).
                                                                               arg(modbus_client->connectionParameter(QModbusDevice::SerialBaudRateParameter).toInt()).
                                                                               arg(modbus_client->connectionParameter(QModbusDevice::SerialDataBitsParameter).toInt()).
                                                                               arg(modbus_client->connectionParameter(QModbusDevice::SerialStopBitsParameter).toInt()));
    
    }
    

    }

    QModbusDataUnit MainWindow::return_data_unit()
    {

    QModbusDataUnit data_unit;
    const auto table =
        static_cast<QModbusDataUnit::RegisterType> (ui->RegisterType_comboBox->currentIndex()+1);
    data_unit.setRegisterType(table);
    data_unit.setStartAddress(ui->StartAddress_spinBox->value());
    data_unit.setValueCount(ui->DataNnumber_spinBox->value());
    return data_unit;
    

    }

    void MainWindow::on_read_pushButton_clicked()
    {

    read_reply=modbus_client->sendReadRequest(return_data_unit(),ui->ServerAddress_spinBox->value());
    /*if(!read_reply->isFinished())
    {*/
         connect(read_reply,SIGNAL(finished()),this,SLOT(display_response_value()));
    //}
    

    }

    void MainWindow::display_response_value()
    {

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

    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.