liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
Loading...
Searching...
No Matches
master_func.h
Go to the documentation of this file.
1#ifndef LIGHTMODBUS_MASTER_FUNC_H
2#define LIGHTMODBUS_MASTER_FUNC_H
3
4#include "base.h"
5#include "master.h"
6
16#define LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(f_suffix, ...) \
17 LIGHTMODBUS_WARN_UNUSED static inline ModbusErrorInfo modbusBuildRequest##f_suffix##PDU(ModbusMaster *status, __VA_ARGS__)
18
23#define LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(f_suffix, ...) \
24 { \
25 ModbusErrorInfo err; \
26 if (!modbusIsOk(err = modbusBeginRequestPDU(status))) return err; \
27 if (!modbusIsOk(err = modbusBuildRequest##f_suffix(status, __VA_ARGS__))) return err; \
28 return modbusEndRequestPDU(status); \
29 }
30
35#define LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(f_suffix, ...) \
36 LIGHTMODBUS_WARN_UNUSED static inline ModbusErrorInfo modbusBuildRequest##f_suffix##RTU(ModbusMaster *status, uint8_t address, __VA_ARGS__)
37
42#define LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(f_suffix, ...) \
43 { \
44 ModbusErrorInfo err; \
45 if (!modbusIsOk(err = modbusBeginRequestRTU(status))) return err; \
46 if (!modbusIsOk(err = modbusBuildRequest##f_suffix(status, __VA_ARGS__))) return err; \
47 return modbusEndRequestRTU(status, address); \
48 }
49
54#define LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(f_suffix, ...) \
55 LIGHTMODBUS_WARN_UNUSED static inline ModbusErrorInfo modbusBuildRequest##f_suffix##TCP(ModbusMaster *status, uint16_t transactionID, uint8_t unitID, __VA_ARGS__)
56
61#define LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(f_suffix, ...) \
62 { \
63 ModbusErrorInfo err; \
64 if (!modbusIsOk(err = modbusBeginRequestTCP(status))) return err; \
65 if (!modbusIsOk(err = modbusBuildRequest##f_suffix(status, __VA_ARGS__))) return err; \
66 return modbusEndRequestTCP(status, transactionID, unitID); \
67 }
68
70 ModbusMaster *status,
71 uint8_t address,
72 uint8_t function,
73 const uint8_t *requestPDU,
74 uint8_t requestLength,
75 const uint8_t *responsePDU,
76 uint8_t responseLength);
77
79 ModbusMaster *status,
80 uint8_t address,
81 uint8_t function,
82 const uint8_t *requestPDU,
83 uint8_t requestLength,
84 const uint8_t *responsePDU,
85 uint8_t responseLength);
86
88 ModbusMaster *status,
89 uint8_t address,
90 uint8_t function,
91 const uint8_t *requestPDU,
92 uint8_t requestLength,
93 const uint8_t *responsePDU,
94 uint8_t responseLength);
95
97 ModbusMaster *status,
98 uint8_t address,
99 uint8_t function,
100 const uint8_t *requestPDU,
101 uint8_t requestLength,
102 const uint8_t *responsePDU,
103 uint8_t responseLength);
104
106 ModbusMaster *status,
107 uint8_t function,
108 uint16_t index,
109 uint16_t count);
110
112 ModbusMaster *status,
113 uint8_t function,
114 uint16_t index,
115 uint16_t value);
116
118 ModbusMaster *status,
119 uint16_t index,
120 uint16_t count,
121 const uint8_t *values);
122
124 ModbusMaster *status,
125 uint16_t index,
126 uint16_t count,
127 const uint16_t *values);
128
130 ModbusMaster *status,
131 uint16_t index,
132 uint16_t andmask,
133 uint16_t ormask);
134
140 ModbusMaster *status,
141 uint16_t index,
142 uint16_t count)
143{
144 return modbusBuildRequest01020304(status, 1, index, count);
145}
146
152 ModbusMaster *status,
153 uint16_t index,
154 uint16_t count)
155{
156 return modbusBuildRequest01020304(status, 2, index, count);
157}
158
164 ModbusMaster *status,
165 uint16_t index,
166 uint16_t count)
167{
168 return modbusBuildRequest01020304(status, 3, index, count);
169}
170
176 ModbusMaster *status,
177 uint16_t index,
178 uint16_t count)
179{
180 return modbusBuildRequest01020304(status, 4, index, count);
181}
182
188 ModbusMaster *status,
189 uint16_t index,
190 uint16_t count)
191{
192 return modbusBuildRequest0506(status, 5, index, count);
193}
194
200 ModbusMaster *status,
201 uint16_t index,
202 uint16_t count)
203{
204 return modbusBuildRequest0506(status, 6, index, count);
205}
206
209LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(01, uint16_t index, uint16_t count)
210LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(01, index, count)
213LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(01, uint16_t index, uint16_t count)
214LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(01, index, count)
217LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(01, uint16_t index, uint16_t count)
218LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(01, index, count)
219
222LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(02, uint16_t index, uint16_t count)
223LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(02, index, count)
226LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(02, uint16_t index, uint16_t count)
227LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(02, index, count)
230LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(02, uint16_t index, uint16_t count)
231LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(02, index, count)
232
235LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(03, uint16_t index, uint16_t count)
236LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(03, index, count)
239LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(03, uint16_t index, uint16_t count)
240LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(03, index, count)
243LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(03, uint16_t index, uint16_t count)
244LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(03, index, count)
245
248LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(04, uint16_t index, uint16_t count)
249LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(04, index, count)
252LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(04, uint16_t index, uint16_t count)
253LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(04, index, count)
256LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(04, uint16_t index, uint16_t count)
257LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(04, index, count)
258
261LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(05, uint16_t index, uint16_t count)
262LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(05, index, count)
265LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(05, uint16_t index, uint16_t count)
266LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(05, index, count)
269LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(05, uint16_t index, uint16_t count)
270LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(05, index, count)
271
274LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(06, uint16_t index, uint16_t count)
275LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(06, index, count)
278LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(06, uint16_t index, uint16_t count)
279LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(06, index, count)
282LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(06, uint16_t index, uint16_t count)
283LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(06, index, count)
284
287LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(15, uint16_t index, uint16_t count, const uint8_t *values)
288LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(15, index, count, values)
291LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(15, uint16_t index, uint16_t count, const uint8_t *values)
292LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(15, index, count, values)
295LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(15, uint16_t index, uint16_t count, const uint8_t *values)
296LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(15, index, count, values)
297
300LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(16, uint16_t index, uint16_t count, const uint16_t *values)
301LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(16, index, count, values)
304LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(16, uint16_t index, uint16_t count, const uint16_t *values)
305LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(16, index, count, values)
308LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(16, uint16_t index, uint16_t count, const uint16_t *values)
309LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(16, index, count, values)
310
313LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(22, uint16_t index, uint16_t andmask, uint16_t ormask)
314LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(22, index, andmask, ormask)
317LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(22, uint16_t index, uint16_t andmask, uint16_t ormask)
318LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(22, index, andmask, ormask)
321LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(22, uint16_t index, uint16_t andmask, uint16_t ormask)
322LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(22, index, andmask, ormask)
323
324#endif
Common types and functions (header)
#define LIGHTMODBUS_WARN_UNUSED
Definition base.h:19
#define LIGHTMODBUS_RET_ERROR
Return type for library functions returning ModbusErrorInfo that should be handled properly.
Definition base.h:49
Master's types and basic functions (header)
#define LIGHTMODBUS_DEFINE_BUILD_TCP_BODY(f_suffix,...)
Defines a body for a modbusBuildRequest*TCP() function.
Definition master_func.h:61
ModbusErrorInfo modbusParseResponse1516(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to requests 15 and 16 (write mutliple regsiters/coils)
Definition master_func.impl.h:172
#define LIGHTMODBUS_DEFINE_BUILD_RTU_BODY(f_suffix,...)
Defines a body for a modbusBuildRequest*RTU() function.
Definition master_func.h:42
ModbusErrorInfo modbusParseResponse0506(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to requests 05 and 06.
Definition master_func.impl.h:132
static ModbusErrorInfo modbusBuildRequest03(ModbusMaster *status, uint16_t index, uint16_t count)
Read multiple holding registers - a wrapper for modbusBuildRequest01020304()
Definition master_func.h:163
static ModbusErrorInfo modbusBuildRequest04(ModbusMaster *status, uint16_t index, uint16_t count)
Read multiple input registers - a wrapper for modbusBuildRequest01020304()
Definition master_func.h:175
static ModbusErrorInfo modbusBuildRequest02(ModbusMaster *status, uint16_t index, uint16_t count)
Read multiple discrete inputs - a wrapper for modbusBuildRequest01020304()
Definition master_func.h:151
#define LIGHTMODBUS_DEFINE_BUILD_TCP_HEADER(f_suffix,...)
Defines a header for a modbusBuildRequest*TCP() function.
Definition master_func.h:54
ModbusErrorInfo modbusBuildRequest0506(ModbusMaster *status, uint8_t function, uint16_t index, uint16_t value)
Write single coil/holding register.
Definition master_func.impl.h:311
static ModbusErrorInfo modbusBuildRequest06(ModbusMaster *status, uint16_t index, uint16_t count)
Write single holding register - a wrapper for modbusBuildRequest0506()
Definition master_func.h:199
ModbusErrorInfo modbusBuildRequest22(ModbusMaster *status, uint16_t index, uint16_t andmask, uint16_t ormask)
Mask write register request.
Definition master_func.impl.h:438
static ModbusErrorInfo modbusBuildRequest05(ModbusMaster *status, uint16_t index, uint16_t count)
Write single coil - a wrapper for modbusBuildRequest0506()
Definition master_func.h:187
#define LIGHTMODBUS_DEFINE_BUILD_RTU_HEADER(f_suffix,...)
Defines a header for a modbusBuildRequest*RTU() function.
Definition master_func.h:35
#define LIGHTMODBUS_DEFINE_BUILD_PDU_BODY(f_suffix,...)
Defines a body for a modbusBuildRequest*PDU() function.
Definition master_func.h:23
static ModbusErrorInfo modbusBuildRequest01(ModbusMaster *status, uint16_t index, uint16_t count)
Read multiple coils - a wrapper for modbusBuildRequest01020304()
Definition master_func.h:139
ModbusErrorInfo modbusParseResponse22(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to request 22.
Definition master_func.impl.h:226
ModbusErrorInfo modbusBuildRequest16(ModbusMaster *status, uint16_t index, uint16_t count, const uint16_t *values)
Write multiple holding registers.
Definition master_func.impl.h:400
ModbusErrorInfo modbusBuildRequest01020304(ModbusMaster *status, uint8_t function, uint16_t index, uint16_t count)
Read mutiple coils/discrete inputs/holding registers/input registers.
Definition master_func.impl.h:261
ModbusErrorInfo modbusParseResponse01020304(ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
Parses response to requests 01, 02, 03 and 04.
Definition master_func.impl.h:28
ModbusErrorInfo modbusBuildRequest15(ModbusMaster *status, uint16_t index, uint16_t count, const uint8_t *values)
Write multiple coils.
Definition master_func.impl.h:344
#define LIGHTMODBUS_DEFINE_BUILD_PDU_HEADER(f_suffix,...)
Defines a header for a modbusBuildRequest*PDU() function.
Definition master_func.h:16
Richer error represenation - source and type of error.
Definition base.h:126
Master device status.
Definition master.h:73