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

范例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

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


函式库参考主页面

86Duino 参考资料中的文字修改自 Arduino 参考资料,并根据 知识共享署名-许可证方式共享 3.0  授权。参考资料中的程式码范例已发布到公共领域。