I want to find...

Search

Shares

Table of Content

ModbusMasterNode.readWriteMultipleRegisters()

[Modbus]

Description

Send a packet command to the Slave node to read and write the Holding Registers.

Syntax

node1.readWriteMultipleRegisters(read_address, read_size, write_address, write_size)
node2.readWriteMultipleRegisters(read_address, read_size)

(See the examples to illustrate the different functions and usage of the parameters)

Parameters

  • node1/node2: ModbusMasterNode Object.
  • read_address: The starting address of the Holding Registers to be read.
  • read_size: The size of the Holding Registers to be read.
  • write_address: The starting address of the Holding Registers you want to write to.
  • write_size: The size of the Holding Registers that you want to write to.

Returns

Int: If it succeeds, send back MODBUS_SUCCESS and vice versa, send back EXCEPTION_CODE.

Example

#include <Arduino.h>
#include <Modbus86.h>
 
ModbusMaster bus1;
ModbusMasterNode node1;
 
uint8_t reg[2];
uint8_t result;
uint32_t receiveData;
 
void setup()
{
    while(!Serial);
    Serial1.begin(115200);
    bus1.begin(MODBUS_RTU, Serial1);
    node1.attach(16, bus1); 
 
    reg[0] = 0x1234;
    reg[1] = 0xabcd;
    node1.setTransmitBuffer(0, reg[0], MODBUS_DATAMODE_UINT16);
    node1.setTransmitBuffer(1, reg[1], MODBUS_DATAMODE_UINT16);
    result = node1.readWriteMultipleRegisters(5, 2, 10, 2);
 
    if (result != MODBUS_SUCCESS) {
        Serial.print("readWriteMultipleRegisters => ErrorCode: ");
        Serial.println(result);
    } else {
        Serial.print("From Holding Register receiveData: ");
        receiveData = node1.getResponseBuffer(0)
                    | (node1.getResponseBuffer(1) << 16);
        Serial.println(receiveData);
    }
}
 
void loop()
{
}
#include <Arduino.h>
#include <Modbus86.h>
 
ModbusMaster bus1;
ModbusMasterNode node2;
 
uint8_t reg[2];
uint8_t result;
uint32_t receiveData;
 
void setup()
{
    while(!Serial);
    bus1.begin(MODBUS_RTU,Serial1);
    node2.attach(16, bus1); 
 
    reg[0] = 0x1234;
    reg[1] = 0xabcd;
    node2.beginTransmission(10);
    node2.send(reg[0]);
    node2.send(reg[1]);
    result = node2.readWriteMultipleRegisters(5, 2);
 
    if (result != MODBUS_SUCCESS) {
        Serial.print("readWriteMultipleRegisters => ErrorCode: ");
        Serial.println(result);
    } else {
        Serial.print("From Holding Register receiveData: ");
        receiveData = node2.getResponseBuffer(0)
                    | (node2.getResponseBuffer(1) << 16);
        Serial.println(receiveData);
    }
}
 
void loop()
{
}

See Also


Libraries Reference Home

The text of the 86Duino reference is a modification of the Arduino reference and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

Leave a Comment

Scroll to Top