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  授權。參考資料中的程式碼範例已發佈到公共領域。