ModbusSlaveNode.readCoil()

[Modbus]

Description

Reads the Coils register.

Syntax

node1.readCoil(address)
node2.readCoil(address, size, buffer)

Parameters

  • node1/node2: ModbusSlaveNode object.
  • address: The address of the Coils register you want to read.
  • size: The number of Coils registers you want to read.
  • buffer: Copy the read-out Coils register data to the array space pointed to by the buffer.

Returns

int: The Coil location data is returned when using address fader only, as MODBUS_COIL_ON or MODBUS_COIL_OFF.

When reading with the specified buffer, MODBUS_SUCCESS is returned, and EXCEPTION_CODE is returned in the opposite direction.

Example

#include <Modbus86.h>
 
ModbusSlave bus;
ModbusSlaveNode node;
 
uint8_t write_single_coil(  uint8_t function,
                            uint16_t address,
                            uint16_t length)
{
    uint16_t value;
     
    if (address == 0) {
        node.readCoil(address, 1, &value);
        if (value)
            digitalWrite(LED_BUILTIN, HIGH);
        else
            digitalWrite(LED_BUILTIN, LOW);
    }
 
    return MODBUS_SUCCESS;
}
 
void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
     
    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 Write Single Coil (0x05). */
    node.cbFunc[MODBUS_CB_WRITE_SINGLE_COIL]
                            = write_single_coil;
     
}
 
void loop()
{    
    node.poll();
}

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