Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Write to Slave in modbus

Write to Slave in modbus

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 961 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rezaMSLM
    wrote on last edited by rezaMSLM
    #1

    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.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      R 1 Reply Last reply
      0
      • R Offline
        R Offline
        rezaMSLM
        wrote on last edited by rezaMSLM
        #3

        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)
        */
        
        1 Reply Last reply
        0
        • dheerendraD dheerendra

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

          R Offline
          R Offline
          rezaMSLM
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved