You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And thats it, your sketch is modbus enabled. (see the full examples for more detail)
21
26
22
-
----
27
+
---
23
28
24
29
- [Install](#install)
25
-
- [Competabilty](#competabilty)
26
-
- [Callback vector](#callback-vector)
27
-
- [Multiple Slaves](#multiple-slaves)
28
-
- [Slots](#slots)
29
-
- [Handler function](#handler-function)
30
-
- [Function codes](#function-codes)
31
-
- [Reading and writing to the request buffer](#reading-and-writing-to-the-request-buffer)
32
-
- [Examples](#examples)
33
-
- [handle "Force Single Coil" as arduino digitalWrite](#handle-force-single-coil-as-arduino-digitalwrite)
34
-
- [handle "Read Input Registers" as arduino analogRead](#handle-read-input-registers-as-arduino-analogread)
35
-
36
-
----
30
+
- [Compatibility](#compatibility)
31
+
- [Callback vector](#callback-vector) - [Multiple Slaves](#multiple-slaves) - [Slots](#slots) - [Handler function](#handler-function) - [Function codes](#function-codes) - [Reading and writing to the request buffer](#reading-and-writing-to-the-request-buffer)
32
+
- [Examples](#examples) - [handle "Force Single Coil" as arduino digitalWrite](#handle-force-single-coil-as-arduino-digitalwrite) - [handle "Read Input Registers" as arduino analogRead](#handle-read-input-registers-as-arduino-analogread)
33
+
34
+
---
37
35
38
36
### Install
39
37
40
38
Download the zip package, and install it into your Arduino IDE. See the Arduino tutorial about installing 3rd party libraries: https://www.arduino.cc/en/Guide/Libraries#toc4
41
39
42
-
### Competabilty
40
+
### Compatibility
43
41
44
42
###### This class implements:
45
43
46
-
* FC1 "Read Coil Status"
47
-
* FC2 "Read Input Status"
48
-
* FC3 "Read Holding Registers"
49
-
* FC4 "Read Input Registers"
50
-
* FC5 "Force Single Coil"
51
-
* FC6 "Preset Single Register"
52
-
* FC15 "Force Multiple Coils"
53
-
* FC16 "Preset Multiple Registers"
44
+
- FC1 "Read Coil Status"
45
+
- FC2 "Read Input Status"
46
+
- FC3 "Read Holding Registers"
47
+
- FC4 "Read Input Registers"
48
+
- FC5 "Force Single Coil"
49
+
- FC6 "Preset Single Register"
50
+
- FC15 "Force Multiple Coils"
51
+
- FC16 "Preset Multiple Registers"
54
52
55
53
### Serial port
56
54
57
-
* The default serial port is Serial, but any class that inhirets from Stream can be used.
58
-
To set a different Serial class, explicitly set the Stream in the Modbus class constuctor.
55
+
- The default serial port is Serial, but any class that inherits from the Stream class can be used.
56
+
To set a different Serial class, explicitly pass the Stream in the Modbus class constuctor.
59
57
60
58
### Callback vector
61
59
@@ -67,127 +65,141 @@ This can be done independently for one or multiple slaves with different IDs.
67
65
68
66
###### Slots
69
67
70
-
The callback vector has 4 slots for request handlers:
68
+
The callback vector has 7 slots for request handlers:
71
69
72
-
* slave.cbVector[CB_READ_COILS] - called on FC1
73
-
* slave.cbVector[CB_READ_DISCRETE_INPUTS] - called on FC2
74
-
* slave.cbVector[CB_READ_HOLDING_REGISTERS] - called on FC3
75
-
* slave.cbVector[CB_READ_INPUT_REGISTERS] - called on FC4
76
-
* slave.cbVector[CB_WRITE_COILS] - called on FC5 and FC15
77
-
* slave.cbVector[CB_WRITE_HOLDING_REGISTERS] - called on FC6 and FC16
70
+
- slave.cbVector[CB_READ_COILS] - called on FC1
71
+
- slave.cbVector[CB_READ_DISCRETE_INPUTS] - called on FC2
72
+
- slave.cbVector[CB_READ_HOLDING_REGISTERS] - called on FC3
73
+
- slave.cbVector[CB_READ_INPUT_REGISTERS] - called on FC4
74
+
- slave.cbVector[CB_WRITE_COILS] - called on FC5 and FC15
75
+
- slave.cbVector[CB_WRITE_HOLDING_REGISTERS] - called on FC6 and FC16
76
+
- slave.cbVector[CB_READ_EXCEPTION_STATUS] - called on FC7
78
77
79
78
###### Handler function
80
79
81
-
Handler functions must return unit8_t and take:
82
-
* uint8_t fc - request function code
83
-
* uint16_t address - first register / first coil address
84
-
* uint16_t length - length of data
85
-
86
-
Return codes:
87
-
88
-
* STATUS_OK = 0,
89
-
* STATUS_ILLEGAL_FUNCTION,
90
-
* STATUS_ILLEGAL_DATA_ADDRESS,
91
-
* STATUS_ILLEGAL_DATA_VALUE,
92
-
* STATUS_SLAVE_DEVICE_FAILURE,
93
-
* STATUS_ACKNOWLEDGE,
94
-
* STATUS_SLAVE_DEVICE_BUSY,
95
-
* STATUS_NEGATIVE_ACKNOWLEDGE,
96
-
* STATUS_MEMORY_PARITY_ERROR,
97
-
* STATUS_GATEWAY_PATH_UNAVAILABLE,
98
-
* STATUS_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND
80
+
A handler functions must return an uint8_t code and take the following as parameters:
81
+
82
+
- uint8_t fc - request function code
83
+
- uint16_t address - first register / first coil address
84
+
- uint16_t length - length of data
85
+
86
+
Usable return codes:
87
+
88
+
- STATUS_OK = 0,
89
+
- STATUS_ILLEGAL_FUNCTION,
90
+
- STATUS_ILLEGAL_DATA_ADDRESS,
91
+
- STATUS_ILLEGAL_DATA_VALUE,
92
+
- STATUS_SLAVE_DEVICE_FAILURE,
93
+
- STATUS_ACKNOWLEDGE,
94
+
- STATUS_SLAVE_DEVICE_BUSY,
95
+
- STATUS_NEGATIVE_ACKNOWLEDGE,
96
+
- STATUS_MEMORY_PARITY_ERROR,
97
+
- STATUS_GATEWAY_PATH_UNAVAILABLE,
98
+
- STATUS_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND
99
99
100
100
###### Function codes
101
101
102
-
* FC_READ_COILS = 1
103
-
* FC_READ_DISCRETE_INPUT = 2
104
-
* FC_READ_REGISTERS = 3
105
-
* FC_READ_INPUT_REGISTERS = 4
106
-
* FC_WRITE_COIL = 5
107
-
* FC_WRITE_REGISTER = 6
108
-
* FC_WRITE_MULTIPLE_COILS = 15
109
-
* FC_WRITE_MULTIPLE_REGISTERS = 16
102
+
- FC_READ_COILS = 1
103
+
- FC_READ_DISCRETE_INPUT = 2
104
+
- FC_READ_REGISTERS = 3
105
+
- FC_READ_INPUT_REGISTERS = 4
106
+
- FC_WRITE_COIL = 5
107
+
- FC_WRITE_REGISTER = 6
108
+
- FC_READ_EXCEPTION_STATUS = 7
109
+
- FC_WRITE_MULTIPLE_COILS = 15
110
+
- FC_WRITE_MULTIPLE_REGISTERS = 16
110
111
111
-
----
112
+
---
112
113
113
114
###### Reading and writing to the request / response buffer
114
115
115
-
* int readCoilFromBuffer(int offset) : read one coil value from the request buffer.
116
-
* uint16_t readRegisterFromBuffer(int offset) : read one register value from the request buffer.
117
-
* void writeCoilToBuffer(int offset, int state) : write one coil state into the answer buffer.
118
-
* void writeRegisterToBuffer(int offset, uint16_t value) : write one register value into the answer buffer.
116
+
- bool readCoilFromBuffer(int offset) : read one coil value from the request buffer.
117
+
- uint16_t readRegisterFromBuffer(int offset) : read one register value from the request buffer.
118
+
- uint8_t writeExceptionStatusToBuffer(int offset, bool status) : write an exception status into the response buffer.
119
+
- uint8_t writeCoilToBuffer(int offset, int state) : write one coil state into the response buffer.
120
+
- uint8_t writeDiscreteInputToBuffer(int offset, bool state) : write one discrete input value into the response buffer.
121
+
- uint8_t writeRegisterToBuffer(int offset, uint16_t value) : write one register value into the response buffer.
122
+
- uint8_t writeArrayToBuffer(int offset, uint16_t \*str, uint8_t length); : writes an array of data into the response register.
119
123
120
-
----
124
+
---
121
125
122
126
### Examples
123
127
124
-
----
125
-
###### handle "Force Single Coil" as arduino digitalWrite
126
-
```c
128
+
---
129
+
130
+
###### Handle "Force Single Coil" and write the received value to digitalWrite()
131
+
```cpp
127
132
#include <ModbusSlave.h>
128
133
129
-
// implicitly set stream to use the Serial serialport
134
+
// Implicitly set stream to use the Serial serialport.
0 commit comments