readCoil()

描述

讀取 Coils 暫存器。

語法


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

參數

node1/node2ModbusSlaveNode 物件。

address:欲讀取的 Coils 暫存器位址。

size:欲讀取的 Coils 暫存器數量。

buffer:將讀取出的 Coils 暫存器陣列資料複寫至 buffer 指向的陣列空間內。

回傳

int:僅使用 address 引數時回傳該 Coil 位置資料,為 MODBUS_COIL_ONMODBUS_COIL_OFF
使用指定 buffer 讀取時,則回傳 MODBUS_SUCCESS ,反之回傳 EXCEPTION_CODE

範例

#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

writeCoil()


函式庫參考主頁面

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.