ModbusSlaveNode.writeInputRegister()

[Modbus]

描述

Write to the Input Registers.

語法

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

參數

  • 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.

回傳

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

範例

#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();
}

函式庫參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール