|
1 | 1 | /* |
2 | 2 | Modbus.h - Header for Modbus Base Library |
3 | 3 | Copyright (C) 2014 André Sarmento Barbosa |
| 4 | + Copyright (C) 2023 Pascal JEAN aka epsilonrt |
4 | 5 | */ |
5 | 6 | #include "Arduino.h" |
6 | 7 |
|
7 | 8 | #pragma once |
8 | 9 |
|
9 | | -#define MAX_REGS 32 |
10 | | -#define MAX_FRAME 128 |
| 10 | +#define MAX_REGS 32 // Unused ! |
| 11 | +#define MAX_FRAME 128 // Unused ! |
11 | 12 | //#define USE_HOLDING_REGISTERS_ONLY |
12 | 13 |
|
13 | 14 | // Function Codes |
@@ -102,21 +103,29 @@ class Modbus { |
102 | 103 | @param offset register offset (PDU addressing: 0-9999) |
103 | 104 | @param value default value |
104 | 105 | */ |
105 | | - void addHreg (word offset, word value = 0); |
| 106 | + inline void addHreg (word offset, word value = 0) { |
| 107 | + this->addReg (offset + 40001, value); |
| 108 | + } |
| 109 | + |
106 | 110 | /** |
107 | 111 | @brief Change the value of a holding register |
108 | 112 | This value will be returned when bus read, the master can also modify it. |
109 | 113 | @param offset register offset (PDU addressing: 0-9999) |
110 | 114 | @param value new value |
111 | 115 | @return true, false if register not found. |
112 | 116 | */ |
113 | | - bool setHreg (word offset, word value); |
| 117 | + inline bool setHreg (word offset, word value) { |
| 118 | + return setReg (offset + 40001, value); |
| 119 | + } |
114 | 120 | /** |
115 | 121 | @brief Return the value of a holding register |
116 | 122 | @param offset register offset (PDU addressing: 0-9999) |
117 | 123 | @return register value |
118 | 124 | */ |
119 | | - word hreg (word offset); |
| 125 | + inline word hreg (word offset) { |
| 126 | + return reg (offset + 40001); |
| 127 | + } |
| 128 | + |
120 | 129 | /** |
121 | 130 | @brief Change the value of a holding register |
122 | 131 | This value will be returned when bus read, the master can also modify it. |
@@ -151,62 +160,79 @@ class Modbus { |
151 | 160 | @param offset coil offset (PDU addressing: 0-9999) |
152 | 161 | @param value default value |
153 | 162 | */ |
154 | | - void addCoil (word offset, bool value = false); |
| 163 | + void addCoil (word offset, bool value = false) { |
| 164 | + this->addReg (offset + 1, value ? 0xFF00 : 0x0000); |
| 165 | + } |
155 | 166 | /** |
156 | 167 | @brief Add a discrete input |
157 | 168 | @param offset input offset (PDU addressing: 0-9999) |
158 | 169 | @param value default value |
159 | 170 | */ |
160 | | - void addIsts (word offset, bool value = false); |
| 171 | + inline void addIsts (word offset, bool value = false) { |
| 172 | + this->addReg (offset + 10001, value ? 0xFF00 : 0x0000); |
| 173 | + } |
161 | 174 | /** |
162 | 175 | @brief Add a input register |
163 | 176 | @param offset register offset (PDU addressing: 0-9999) |
164 | 177 | @param value default value |
165 | 178 | */ |
166 | | - void addIreg (word offset, word value = 0); |
| 179 | + inline void addIreg (word offset, word value = 0) { |
| 180 | + this->addReg (offset + 30001, value); |
| 181 | + } |
167 | 182 | /** |
168 | 183 | @brief Change the value of a coil |
169 | 184 | This value will be returned when bus read, the master can also modify it. |
170 | 185 | @param offset register offset (PDU addressing: 0-9999) |
171 | 186 | @param value new value |
172 | 187 | @return true, false if coil not found. |
173 | 188 | */ |
174 | | - bool setCoil (word offset, bool value); |
| 189 | + inline bool setCoil (word offset, bool value) { |
| 190 | + return setReg (offset + 1, value ? 0xFF00 : 0x0000); |
| 191 | + } |
175 | 192 | /** |
176 | 193 | @brief Change the value of a discrete input |
177 | 194 | This value will be returned when bus read,. |
178 | 195 | @param offset input offset (PDU addressing: 0-9999) |
179 | 196 | @param value new value |
180 | 197 | @return true, false if input not found. |
181 | 198 | */ |
182 | | - bool setIsts (word offset, bool value); |
| 199 | + inline bool setIsts (word offset, bool value) { |
| 200 | + return setReg (offset + 10001, value ? 0xFF00 : 0x0000); |
| 201 | + } |
183 | 202 | /** |
184 | 203 | @brief Change the value of an input register |
185 | 204 | This value will be returned when bus read. |
186 | 205 | @param offset register offset (PDU addressing: 0-9999) |
187 | 206 | @param value new value |
188 | 207 | @return true, false if register not found. |
189 | 208 | */ |
190 | | - bool setIreg (word offset, word value); |
| 209 | + inline bool setIreg (word offset, word value) { |
| 210 | + return setReg (offset + 30001, value); |
| 211 | + } |
191 | 212 | /** |
192 | 213 | @brief Return the value of a coil |
193 | 214 | @param offset register offset (PDU addressing: 0-9999) |
194 | 215 | @return coil value |
195 | 216 | */ |
196 | | - bool coil (word offset); |
| 217 | + inline bool coil (word offset) { |
| 218 | + return (reg (offset + 1) == 0xFF00); |
| 219 | + } |
197 | 220 | /** |
198 | 221 | @brief Return the value of a discrete input |
199 | 222 | @param offset input offset (PDU addressing: 0-9999) |
200 | 223 | @return input value |
201 | 224 | */ |
202 | | - bool ists (word offset); |
| 225 | + inline bool ists (word offset) { |
| 226 | + return (reg (offset + 10001) == 0xFF00); |
| 227 | + } |
203 | 228 | /** |
204 | 229 | @brief Return the value of an input register |
205 | 230 | @param offset register offset (PDU addressing: 0-9999) |
206 | 231 | @return register value |
207 | 232 | */ |
208 | | - word ireg (word offset); |
209 | | - |
| 233 | + inline word ireg (word offset) { |
| 234 | + return reg (offset + 30001); |
| 235 | + } |
210 | 236 | /** |
211 | 237 | @brief Change the value of a coil |
212 | 238 | This value will be returned when bus read, the master can also modify it. |
|
0 commit comments