DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
modbus_master.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file modbus_master.h
6 * @brief Modbus TCP master codec + register scanner (DETWS_ENABLE_MODBUS_MASTER).
7 *
8 * The master/client side of Modbus: build a read-request ADU (MBAP header + PDU)
9 * and parse the slave's response into register values, so an application can poll
10 * or auto-discover a slave's registers. Pure - no sockets, no heap - so it is
11 * host-tested as a full round-trip against the slave codec (modbus_process_adu).
12 * The app supplies the transport (send the ADU, receive the reply).
13 *
14 * Auto-discovery pattern: walk the address space one read at a time; a register
15 * exists where the response parses without a Modbus exception.
16 *
17 * @author Douglas Quigg (dstroy0)
18 * @date 2026
19 */
20
21#ifndef DETERMINISTICESPASYNCWEBSERVER_MODBUS_MASTER_H
22#define DETERMINISTICESPASYNCWEBSERVER_MODBUS_MASTER_H
23
24#include "ServerConfig.h"
25#include <stddef.h>
26#include <stdint.h>
27
28#if DETWS_ENABLE_MODBUS_MASTER
29
30/**
31 * @brief Build a read-request ADU (FC 0x03 holding or 0x04 input registers).
32 *
33 * @param fc ModbusFunction::MODBUS_FC_READ_HOLDING_REGS (0x03) or ModbusFunction::MODBUS_FC_READ_INPUT_REGS (0x04).
34 * @param txid transaction id echoed by the slave (caller's correlation token).
35 * @param unit unit / slave id.
36 * @param start first register address.
37 * @param count number of registers (1..125).
38 * @param out destination buffer.
39 * @param cap destination capacity (>= 12).
40 * @return bytes written (12), or 0 on a bad argument / too-small buffer.
41 */
42size_t modbus_build_read(uint8_t fc, uint16_t txid, uint8_t unit, uint16_t start, uint16_t count, uint8_t *out,
43 size_t cap);
44
45/**
46 * @brief Parse a read-response ADU into register values.
47 *
48 * @param adu response bytes (MBAP + PDU).
49 * @param len response length.
50 * @param regs_out destination for parsed 16-bit register values.
51 * @param max_regs capacity of @p regs_out.
52 * @param exception_out set to the Modbus exception code if the slave returned one
53 * (then the function returns 0 registers); 0 otherwise.
54 * @return number of registers parsed (>= 0), or -1 on a malformed/short frame.
55 */
56int modbus_parse_response(const uint8_t *adu, size_t len, uint16_t *regs_out, size_t max_regs, uint8_t *exception_out);
57
58#endif // DETWS_ENABLE_MODBUS_MASTER
59#endif // DETERMINISTICESPASYNCWEBSERVER_MODBUS_MASTER_H
User-facing configuration for DeterministicESPAsyncWebServer.