ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
modbus_master.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_MODBUS_MASTER_H
5#define PROTOCORE_MODBUS_MASTER_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file modbus_master.h
13 * @brief Modbus TCP master codec + register scanner (PROTOCORE_ENABLE_MODBUS_MASTER).
14 *
15 * The master/client side of Modbus: build a read-request ADU (MBAP header + PDU)
16 * and parse the slave's response into register values, so an application can poll
17 * or auto-discover a slave's registers. Pure - no sockets, no heap - so it is
18 * host-tested as a full round-trip against the slave codec (Modbus.process_adu).
19 * The app supplies the transport (send the ADU, receive the reply).
20 *
21 * Auto-discovery pattern: walk the address space one read at a time; a register
22 * exists where the response parses without a Modbus exception.
23 *
24 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
25 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
26 * a caller drives every namespace the same way.
27 *
28 * @author Douglas Quigg (dstroy0)
29 * @date 2026
30 */
31
32// PROTOCORE_MODBUS_MASTER_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
33// it into its arena. Its size and its offset are each a static_assert, so a feature
34// combination that does not fit fails to compile rather than overrunning at run time.
35
36/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
37typedef struct
38{
39 size_t (*build_read)(const uint8_t *, uint8_t, uint16_t, uint8_t, uint16_t, uint16_t, uint8_t *, size_t);
40 int (*parse_response)(const uint8_t *, const uint8_t *, size_t, uint16_t *, size_t, uint8_t *);
41 size_t (*build_read_bits)(const uint8_t *, uint8_t, uint16_t, uint8_t, uint16_t, uint16_t, uint8_t *, size_t);
42 int (*parse_read_bits_response)(const uint8_t *, const uint8_t *, size_t, uint16_t, uint8_t *, size_t, uint8_t *);
43 size_t (*build_write_single_coil)(const uint8_t *, uint16_t, uint8_t, uint16_t, embed_bool, uint8_t *, size_t);
44 size_t (*build_write_multiple_coils)(const uint8_t *, uint16_t, uint8_t, uint16_t, const uint8_t *, uint16_t,
45 uint8_t *, size_t);
46 size_t (*build_write_single)(const uint8_t *, uint16_t, uint8_t, uint16_t, uint16_t, uint8_t *, size_t);
47 size_t (*build_write_multiple)(const uint8_t *, uint16_t, uint8_t, uint16_t, const uint16_t *, uint16_t, uint8_t *,
48 size_t);
49 int (*parse_write_response)(const uint8_t *, const uint8_t *, size_t, uint16_t *, uint8_t *);
50 size_t (*build_mask_write)(const uint8_t *, uint16_t, uint8_t, uint16_t, uint16_t, uint16_t, uint8_t *, size_t);
51 size_t (*build_read_write_multiple)(const uint8_t *, uint16_t, uint8_t, uint16_t, uint16_t, uint16_t,
52 const uint16_t *, uint16_t, uint8_t *, size_t);
53 int (*parse_mask_write_response)(const uint8_t *, const uint8_t *, size_t, uint16_t *, uint16_t *, uint16_t *,
54 uint8_t *);
56PROTOCORE_NS_LAYOUT(ModbusMasterNs, build_read, parse_response, build_read_bits, parse_read_bits_response,
57 build_write_single_coil, build_write_multiple_coils, build_write_single, build_write_multiple,
58 parse_write_response, build_mask_write, build_read_write_multiple, parse_mask_write_response);
59
60/**
61 * @brief Build a read-request ADU (FC 0x03 holding or 0x04 input registers).
62 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
63 * @param fc MODBUS_FC_READ_HOLDING_REGS (0x03) or MODBUS_FC_READ_INPUT_REGS (0x04)
64 * @param txid transaction id echoed by the slave (caller's correlation token)
65 * @param unit unit / slave id
66 * @param start first register address
67 * @param count number of registers (1..125)
68 * @param out destination buffer
69 * @param cap destination capacity (>= 12)
70 * @return The size_t.
71 */
72size_t protocore_modbus_master_build_read(const uint8_t *work, uint8_t fc, uint16_t txid, uint8_t unit, uint16_t start,
73 uint16_t count, uint8_t *out, size_t cap);
74/**
75 * @brief Parse a read-response ADU into register values.
76 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
77 * @param adu response bytes (MBAP + PDU)
78 * @param len response length
79 * @param regs_out destination for parsed 16-bit register values
80 * @param max_regs capacity of regs_out
81 * @param exception_out set to the Modbus exception code if the slave returned one (then the function returns 0
82 * @return The int.
83 */
84int protocore_modbus_master_parse_response(const uint8_t *work, const uint8_t *adu, size_t len, uint16_t *regs_out,
85 size_t max_regs, uint8_t *exception_out);
86/**
87 * @brief Build a read-bits request ADU (FC 0x01 coils or 0x02 discrete .
88 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
89 * @param fc MODBUS_FC_READ_COILS (0x01) or ::MODBUS_FC_READ_DISCRETE_INPUTS (0x02)
90 * @param txid transaction id echoed by the slave
91 * @param unit unit / slave id
92 * @param start first bit address
93 * @param count number of bits (1..2000)
94 * @param out destination buffer
95 * @param cap destination capacity (>= 12)
96 * @return The size_t.
97 */
98size_t protocore_modbus_master_build_read_bits(const uint8_t *work, uint8_t fc, uint16_t txid, uint8_t unit,
99 uint16_t start, uint16_t count, uint8_t *out, size_t cap);
100/**
101 * @brief Parse a read-bits response ADU (FC 0x01 / 0x02) into one byte (0/1).
102 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
103 * @param adu Adu
104 * @param len Len
105 * @param count the number of bits requested (1..2000)
106 * @param bits_out destination for count unpacked bits (nullable to just validate)
107 * @param max_bits capacity of bits_out
108 * @param exception_out set to the Modbus exception code if the slave returned one (then 0 is returned)
109 * @return The int.
110 */
111int protocore_modbus_master_parse_read_bits_response(const uint8_t *work, const uint8_t *adu, size_t len,
112 uint16_t count, uint8_t *bits_out, size_t max_bits,
113 uint8_t *exception_out);
114/**
115 * @brief Build a Write Single Coil request ADU (FC 0x05).
116 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
117 * @param txid Txid
118 * @param unit Unit
119 * @param addr Addr
120 * @param on the coil value; encoded on the wire as 0xFF00 (on) or 0x0000 (off) per the Modbus spec
121 * @param out Out
122 * @param cap destination capacity (>= 12)
123 * @return The size_t.
124 */
125size_t protocore_modbus_master_build_write_single_coil(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t addr,
126 embed_bool on, uint8_t *out, size_t cap);
127/**
128 * @brief Build a Write Multiple Coils request ADU (FC 0x0F).
129 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
130 * @param txid Txid
131 * @param unit Unit
132 * @param start Start
133 * @param bits one byte (0/1) per coil to write; packed LSB-first into the wire bytes
134 * @param count number of coils (1..1968)
135 * @param out Out
136 * @param cap destination capacity (>= 14 + ceil(count/8))
137 * @return The size_t.
138 */
139size_t protocore_modbus_master_build_write_multiple_coils(const uint8_t *work, uint16_t txid, uint8_t unit,
140 uint16_t start, const uint8_t *bits, uint16_t count,
141 uint8_t *out, size_t cap);
142/**
143 * @brief Build a Write Single Register request ADU (FC 0x06).
144 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
145 * @param txid transaction id echoed by the slave
146 * @param unit unit / slave id
147 * @param addr register address
148 * @param value 16-bit value to write
149 * @param out destination buffer
150 * @param cap destination capacity (>= 12)
151 * @return The size_t.
152 */
153size_t protocore_modbus_master_build_write_single(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t addr,
154 uint16_t value, uint8_t *out, size_t cap);
155/**
156 * @brief Build a Write Multiple Registers request ADU (FC 0x10).
157 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
158 * @param txid transaction id echoed by the slave
159 * @param unit unit / slave id
160 * @param start first register address
161 * @param values the count register values
162 * @param count number of registers (1..123)
163 * @param out destination buffer
164 * @param cap destination capacity (>= 13 + 2*count)
165 * @return The size_t.
166 */
167size_t protocore_modbus_master_build_write_multiple(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t start,
168 const uint16_t *values, uint16_t count, uint8_t *out, size_t cap);
169/**
170 * @brief Parse a write-response ADU (FC 0x05, 0x06, 0x0F, or 0x10). A normal .
171 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
172 * @param adu response bytes (MBAP + PDU)
173 * @param len response length
174 * @param addr_out set to the echoed address / start (nullable)
175 * @param exception_out set to the Modbus exception code if the slave returned one (then 0 is returned)
176 * @return The int.
177 */
178int protocore_modbus_master_parse_write_response(const uint8_t *work, const uint8_t *adu, size_t len,
179 uint16_t *addr_out, uint8_t *exception_out);
180/**
181 * @brief Build a Mask Write Register request ADU (FC 0x16). The slave .
182 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
183 * @param txid Txid
184 * @param unit Unit
185 * @param addr Addr
186 * @param and_mask And mask
187 * @param or_mask Or mask
188 * @param out Out
189 * @param cap destination capacity (>= 14)
190 * @return The size_t.
191 */
192size_t protocore_modbus_master_build_mask_write(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t addr,
193 uint16_t and_mask, uint16_t or_mask, uint8_t *out, size_t cap);
194/**
195 * @brief Build a Read/Write Multiple Registers request ADU (FC 0x17): write.
196 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
197 * @param txid Txid
198 * @param unit Unit
199 * @param read_start / read_count the registers to read back (1..125)
200 * @param read_count Read count
201 * @param write_start / write_count the registers to write (1..121); values holds write_count words
202 * @param values Values
203 * @param write_count Write count
204 * @param out Out
205 * @param cap destination capacity (>= 17 + 2*write_count)
206 * @return The size_t.
207 */
208size_t protocore_modbus_master_build_read_write_multiple(const uint8_t *work, uint16_t txid, uint8_t unit,
209 uint16_t read_start, uint16_t read_count, uint16_t write_start,
210 const uint16_t *values, uint16_t write_count, uint8_t *out,
211 size_t cap);
212/**
213 * @brief Parse a Mask Write Register response (FC 0x16), which echoes the .
214 * @param work PROTOCORE_MODBUS_MASTER_BORROW bytes the caller took. Not held past the call.
215 * @param adu Adu
216 * @param len Len
217 * @param addr_out / and_out / or_out receive the echoed fields (each nullable)
218 * @param and_out And out
219 * @param or_out Or out
220 * @param exception_out set to the Modbus exception code if the slave returned one
221 * @return The int.
222 */
223int protocore_modbus_master_parse_mask_write_response(const uint8_t *work, const uint8_t *adu, size_t len,
224 uint16_t *addr_out, uint16_t *and_out, uint16_t *or_out,
225 uint8_t *exception_out);
226
227/** @brief Module namespace. */
241
242EMBED_END_DECLS
243
244#endif // PROTOCORE_MODBUS_MASTER_H
int protocore_modbus_master_parse_response(const uint8_t *work, const uint8_t *adu, size_t len, uint16_t *regs_out, size_t max_regs, uint8_t *exception_out)
Parse a read-response ADU into register values.
int protocore_modbus_master_parse_read_bits_response(const uint8_t *work, const uint8_t *adu, size_t len, uint16_t count, uint8_t *bits_out, size_t max_bits, uint8_t *exception_out)
Parse a read-bits response ADU (FC 0x01 / 0x02) into one byte (0/1).
int protocore_modbus_master_parse_write_response(const uint8_t *work, const uint8_t *adu, size_t len, uint16_t *addr_out, uint8_t *exception_out)
Parse a write-response ADU (FC 0x05, 0x06, 0x0F, or 0x10). A normal .
size_t protocore_modbus_master_build_write_single_coil(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t addr, embed_bool on, uint8_t *out, size_t cap)
Build a Write Single Coil request ADU (FC 0x05).
size_t protocore_modbus_master_build_read_bits(const uint8_t *work, uint8_t fc, uint16_t txid, uint8_t unit, uint16_t start, uint16_t count, uint8_t *out, size_t cap)
Build a read-bits request ADU (FC 0x01 coils or 0x02 discrete .
int protocore_modbus_master_parse_mask_write_response(const uint8_t *work, const uint8_t *adu, size_t len, uint16_t *addr_out, uint16_t *and_out, uint16_t *or_out, uint8_t *exception_out)
Parse a Mask Write Register response (FC 0x16), which echoes the .
size_t protocore_modbus_master_build_write_single(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t addr, uint16_t value, uint8_t *out, size_t cap)
Build a Write Single Register request ADU (FC 0x06).
size_t protocore_modbus_master_build_mask_write(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t addr, uint16_t and_mask, uint16_t or_mask, uint8_t *out, size_t cap)
Build a Mask Write Register request ADU (FC 0x16). The slave .
size_t protocore_modbus_master_build_write_multiple_coils(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t start, const uint8_t *bits, uint16_t count, uint8_t *out, size_t cap)
Build a Write Multiple Coils request ADU (FC 0x0F).
size_t protocore_modbus_master_build_write_multiple(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t start, const uint16_t *values, uint16_t count, uint8_t *out, size_t cap)
Build a Write Multiple Registers request ADU (FC 0x10).
size_t protocore_modbus_master_build_read(const uint8_t *work, uint8_t fc, uint16_t txid, uint8_t unit, uint16_t start, uint16_t count, uint8_t *out, size_t cap)
Build a read-request ADU (FC 0x03 holding or 0x04 input registers).
size_t protocore_modbus_master_build_read_write_multiple(const uint8_t *work, uint16_t txid, uint8_t unit, uint16_t read_start, uint16_t read_count, uint16_t write_start, const uint16_t *values, uint16_t write_count, uint8_t *out, size_t cap)
Build a Read/Write Multiple Registers request ADU (FC 0x17): write.
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
Dispatch table. Addressed by offset, so the layout is asserted below.
size_t(* build_mask_write)(const uint8_t *, uint16_t, uint8_t, uint16_t, uint16_t, uint16_t, uint8_t *, size_t)
size_t(* build_read_write_multiple)(const uint8_t *, uint16_t, uint8_t, uint16_t, uint16_t, uint16_t, const uint16_t *, uint16_t, uint8_t *, size_t)
int(* parse_mask_write_response)(const uint8_t *, const uint8_t *, size_t, uint16_t *, uint16_t *, uint16_t *, uint8_t *)
size_t(* build_read_bits)(const uint8_t *, uint8_t, uint16_t, uint8_t, uint16_t, uint16_t, uint8_t *, size_t)
int(* parse_write_response)(const uint8_t *, const uint8_t *, size_t, uint16_t *, uint8_t *)
size_t(* build_write_single)(const uint8_t *, uint16_t, uint8_t, uint16_t, uint16_t, uint8_t *, size_t)
int(* parse_response)(const uint8_t *, const uint8_t *, size_t, uint16_t *, size_t, uint8_t *)
size_t(* build_write_multiple)(const uint8_t *, uint16_t, uint8_t, uint16_t, const uint16_t *, uint16_t, uint8_t *, size_t)
size_t(* build_write_single_coil)(const uint8_t *, uint16_t, uint8_t, uint16_t, embed_bool, uint8_t *, size_t)
size_t(* build_read)(const uint8_t *, uint8_t, uint16_t, uint8_t, uint16_t, uint16_t, uint8_t *, size_t)
int(* parse_read_bits_response)(const uint8_t *, const uint8_t *, size_t, uint16_t, uint8_t *, size_t, uint8_t *)
size_t(* build_write_multiple_coils)(const uint8_t *, uint16_t, uint8_t, uint16_t, const uint8_t *, uint16_t, uint8_t *, size_t)