Modbus RTU Tutorial: How to Write Multiple Holding Registers with M-Duino PLC
Take advantage of the Industry by using Modbus RTU with your PLCs
19 octubre, 2021 por
Modbus RTU Tutorial: How to Write Multiple Holding Registers with M-Duino PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez

INTRODUCTION

The Write Multiple Holding Registers Modbus RTU function (Modbus Function Code: 16), is used to write a block of contiguous registers (1 to 123 registers) in a remote device.

In this blog post, we will be writing multiple holding registers from the PLC set as a master, to the PLC set as a slave with random values between 0 and 256!

Latest Posts

MODBUS RTU

To know more about Modbus RTU and how the library works, please visit the following blog post:

Know more >

HARDWARE REQUIREMENTS

  • Power Supply >

  • B type cable , to program the Arduino based PLC

  • Two twisted pair cables, for RS485.

CONNECTIONS

Now, in order to set a Modbus RTU communication, we are going to do the following:

1. Power the PLC between 12 and 24V.

2. Wire the cables through RS-485. This is based on a twisted pair cables, on cable from A+ to A+, and the other one from B- to B-.

3. Set the red switch to the Half-Duplex: HD

Odoo • Image and Text

MODBUS RTU MASTER WRITE MULTIPLE HOLDING REGISTERS

In order to set an M-Duino as a master and the other one as a slave, we will have to program both to execute each code. So, in order to program the master, open up a new file of Arduino IDE, and paste the code below:

/*
   Copyright (c) 2018 Boot&Work Corp., S.L. All rights reserved

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <ModbusRTUMaster.h>

// Define the ModbusRTUMaster object, using the RS-485 or RS-232 port (depending on availability)
#if defined HAVE_RS485_HARD
#include <RS485.h>
ModbusRTUMaster master(RS485);

#elif defined HAVE_RS232_HARD
#include <RS232.h>
ModbusRTUMaster master(RS232);

#else
ModbusRTUMaster master(Serial1);
#endif

uint32_t lastSentTime = 0UL;
const uint32_t baudrate = 38400UL;

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600UL);

  // Start the serial port
#if defined HAVE_RS485_HARD
  RS485.begin(baudrate, HALFDUPLEX, SERIAL_8E1);
#elif defined HAVE_RS232_HARD
  RS232.begin(baudrate, SERIAL_8E1);
#else
  Serial1.begin(baudrate, SERIAL_8E1);
#endif

  // Start the modbus master object.
  // It is possible to define the port rate (default: 19200)
  master.begin(baudrate);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Send a request every 1000ms
  if (millis() - lastSentTime > 1000) {

    // Set random values
    uint16_t values[3];
    for (int i = 0; i < 3; ++i) {
      values[i] = random(0, 256);
    }

    // Send a Write Multiple Register request to the slave with address 31
    // It requests for setting 3 registers with random values between 0 and 256 starting in address 0
    // IMPORTANT: all read and write functions start a Modbus transmission, but they are not
    // blocking, so you can continue the program while the Modbus functions work. To check for
    // available responses, call modbus.available() function often.
    if (!master.writeMultipleRegisters(31, 0, values, 3)) {
      // Failure treatment
      Serial.println("Request fail");
    }

    lastSentTime = millis();
  }

  // Check available responses often
  if (master.isWaitingResponse()) {
    ModbusResponse response = master.available();
    if (response) {
      if (response.hasError()) {
        // Response failure treatment. You can use response.getErrorCode()
        // to get the error code.
        Serial.print("Error ");
        Serial.println(response.getErrorCode());
      } else {
        // Get the discrete inputs values from the response
        if (response.hasError()) {
          // Response failure treatment. You can use response.getErrorCode()
          // to get the error code.
          Serial.print("Error ");
          Serial.println(response.getErrorCode());
        } else {
          Serial.println("Done");
        }
      }
    }
  }
}

1. Once the sketch is opened, click on Tools > Board > Industrial Shields boards > And select the M-Duino family.

2. Then, select the PLC model by going to Tools > Model > And selecting the model. In our case: M-Duino 21+

3. Select the port by going to Tools > Port > And select the port of the Arduino board.

4. Finally, either click on the arrow to upload the sketch, or go to Sketch > Upload.

MODBUS RTU SLAVE

Once the master is already programmed, we are going to do the same as with the Master PLC, but choosing the ModbusRTUSlave sketch. So:

1. Change the B type cable and connect it to the other M-Duino PLC.

2. Go to the top bar, and click on: File > Examples > Tools40 > Modbus > and select the ModbusRTUSlave sketch.

3. Once the sketch is opened, click on Tools > Board > Industrial Shields boards > And select the M-Duino family.

4. Then, select the PLC model by going to Tools > Model > And selecting the model. In our case: M-Duino 21+

5. Select the port by going to Tools > Port > And select the port of the Arduino board.

6. Finally, either click on the arrow to upload the sketch, or go to Sketch > Upload.


 Now, you will be able to see how the PLC set as a slave displays random values in the Q0.5, Q0.6 and Q0.7 outputs!

Modbus RTU Tutorial: How to Write Multiple Holding Registers with M-Duino PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez
19 octubre, 2021
Compartir
Archivar

¿Buscas tu controlador lógico programable ideal?

Echa un vistazo a esta comparativa de producto de varios controladores industriales basados en Arduino.

Comparamos entradas, salidas, comunicaciones y otras especificaciones con las de los equipos de otras marcas destacadas.


Comparación PLC industrial>>>

¿Quieres más información?

¡Rellena el formulario!

¡Cuéntame más!