I want to find...

Search

Shares

Table of Content

ModbusSlaveNode.writeInputRegister()

[Modbus]

Description

Write to the Input Registers.

Syntax

node1.writeInputRegister(address, value)
node2.writeInputRegister(address, size, buffer)

Parameters

  • node1/node2: ModbusSlaveNode object.
  • address: The address of the Input Registers to write to.
  • value: The value of the single Input Registers that you want to write to.
  • size: The number of plural Input Registers you want to write to.
  • buffer: Copy the data of the array in the array space pointed to by the buffer to the Input Registers.

Returns

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

Example

#include <Modbus86.h>
 
ModbusSlave bus;
ModbusSlaveNode node;
 
uint8_t read_input_registers(   uint8_t function,
                                uint16_t address,
                                uint16_t length)
{
    int sensorPin = A2;
 
    if (address <= 2 && address + length > 2)
        node.writeInputRegister(2, analogRead(sensorPin));
     
    return MODBUS_SUCCESS;
}
 
void setup()
{    
    Serial485.begin(115200);
     
    /* Modbus RTU Mode via RS485. */
    bus.begin(MODBUS_RTU, Serial485);
     
    /* Slave node with ID 11. */
    node.attach(11, bus);
 
    /* Set the callback function of Read Input Registers (0x04). */
    node.cbFunc[MODBUS_CB_READ_INPUT_REGISTERS]
                            = read_input_registers;
}
 
void loop()
{
    node.poll();
}

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