Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Write to Slave in modbus



  • Hi
    I want to change slave ID In modbus, current slave ID is 3 and i want to change it to 4;
    the ID is stored in address 3 of holding registers; I use this code but doesn't work:

    void MainWindow::on_pushButton_WriteID_clicked()
    {
        QModbusDataUnit writeUnit = QModbusDataUnit(QModbusDataUnit::HoldingRegisters) ;//specify register type
        writeUnit.setStartAddress(3);//set start address
        writeUnit.setValueCount(1);//set number of data blocks
        writeUnit.setValue(0,4);//set value 4 in index 0 (new slave ID)
    
        if(auto *reply = ModbusObject->sendWriteRequest(writeUnit,/*device slave ID:*/ 3))//send data unit to slave ID 3
        {
            if(!reply->isFinished()){
                connect(reply,&QModbusReply::finished,this,MainWindow::WriteID);
                qDebug()<<"ok";
            }
    
            else{
                statusBar()->showMessage("error!",2000);
                delete reply;
            }
        }
        else
            statusBar()->showMessage("error!",2000);
    }
    
    void MainWindow::WriteID()
    {
    
        auto reply = qobject_cast<QModbusReply *>(sender());
        if (!reply){
            qDebug()<<"returned";
            return;
        }
    
        if(reply->error()){
            statusBar()->showMessage(tr("ERROR:%1").arg(reply->errorString()),2000);
        }
        else{
            statusBar()->showMessage("Write Successful!",2000);
        }
        reply->deleteLater();
    
    }
    

    when I Click on Write button "Write successful" is printed but the ID is not changed.


  • Qt Champions 2017

    It is good idea to look at modbus slave side for troubleshooting this issue.



  • modifying writeUnit like this also doesn't work:

        auto writeUnit = QModbusDataUnit(QModbusDataUnit::HoldingRegisters
                                         ,/*address*/3,/*new value*/4);
    /*
      writeUnit.setStartAddress(3);//set start address
      writeUnit.setValueCount(1);//set number of data blocks
      writeUnit.setValue(2,4);//set value 4 in index 0 (new slave ID)
    */
    


  • This post is deleted!

Log in to reply