ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
modbus_server.h
Go to the documentation of this file.
1#pragma once
2
4
9
10#include <utility>
11
12namespace esphome::modbus_server {
13
16
18 bool enabled{false};
19 uint16_t register_last_address{0xFFFF};
20 uint16_t register_value{0};
21};
22
23class ServerRegister final {
24 public:
25 template<typename T> using ReadFn = optional<T> (*)(uint16_t address);
26 template<typename T> using WriteFn = bool (*)(uint16_t address, T value);
27
30
33 template<SensorValueType VALUE_TYPE> void set_read_lambda(ReadFn<RegisterValueType<VALUE_TYPE>> read_fn) {
34 this->read_fn_ = reinterpret_cast<ErasedFn>(read_fn);
35 }
36 template<SensorValueType VALUE_TYPE> void set_write_lambda(WriteFn<RegisterValueType<VALUE_TYPE>> write_fn) {
37 this->write_fn_ = reinterpret_cast<ErasedFn>(write_fn);
38 }
39
40 bool is_readable() const { return this->read_fn_ != nullptr; }
41 bool is_writable() const { return this->write_fn_ != nullptr; }
43 optional<int64_t> read() const;
45 bool write(int64_t number) const;
46
47 // max 44: float with %.1f can be up to 42 chars (3.4e38 → 39 integer digits + sign + decimal + 1 digit)
48 // plus null terminator = 43, rounded to 44 for 4-byte alignment
49 static constexpr size_t FORMAT_VALUE_BUF_SIZE = 44;
50
51 // Formats a raw value as verbose log text for the value type, into a caller-provided buffer.
52 // Returns buf for convenience.
53 const char *format_value(int64_t value, char *buf, size_t buf_size) const;
54
55 void set_allow_partial_read(bool allow_partial_read) { this->allow_partial_read = allow_partial_read; }
56
57 uint16_t address{0};
59 uint8_t register_count{0};
60 // When true, a read may cover only part of this multi-register value; otherwise it must read the whole value.
61 bool allow_partial_read{false};
62
63 private:
64 using ErasedFn = void (*)();
65 // Typed by value_type, which is why these are private.
66 ErasedFn read_fn_{nullptr};
67 ErasedFn write_fn_{nullptr};
68};
69
73class ServerBit final {
74 public:
77 using ReadFn = optional<bool> (*)(uint16_t address);
78 using WriteFn = bool (*)(uint16_t address, bool value);
79
80 explicit ServerBit(uint16_t address) : address(address) {}
81 void set_read_lambda(ReadFn read_fn) { this->read_lambda = read_fn; }
82 void set_write_lambda(WriteFn write_fn) { this->write_lambda = write_fn; }
83
86 uint16_t address{0};
87};
88
90 public:
91 void dump_config() override;
92
93 SLOT_LIST(ServerRegister, server_registers_, add_server_register, MODBUS_SERVER_REGISTER_COUNT)
94 SLOT_LIST(ServerBit, server_bits_, add_server_bit, MODBUS_SERVER_BIT_COUNT)
96 modbus::ResponseStatus on_read_registers(uint16_t start_address, uint16_t number_of_registers,
97 modbus::RegisterValues &registers) final;
99 modbus::ResponseStatus on_write_registers(uint16_t start_address, const modbus::RegisterValues &registers) final;
102 modbus::ResponseStatus on_read_bits(uint16_t start_address, modbus::MutablePackedBits bits) final;
104 modbus::ResponseStatus on_write_coils(uint16_t start_address, modbus::PackedBits bits) final;
106 void set_server_courtesy_response(const ServerCourtesyResponse &server_courtesy_response) {
107 this->server_courtesy_response_ = server_courtesy_response;
108 }
111
112 protected:
116 ServerBit *find_bit_(uint16_t address) const;
119 .enabled = false, .register_last_address = 0xFFFF, .register_value = 0};
120};
121
122} // namespace esphome::modbus_server
modbus::ResponseStatus on_write_registers(uint16_t start_address, const modbus::RegisterValues &registers) final
called when a modbus request (function code 0x06 or 0x10) was parsed without errors
modbus::ResponseStatus on_read_bits(uint16_t start_address, modbus::MutablePackedBits bits) final
called when a modbus request (function code 0x01 or 0x02) was parsed without errors; both are served ...
SLOT_LIST(ServerRegister, server_registers_, add_server_register, MODBUS_SERVER_REGISTER_COUNT) SLOT_LIST(ServerBit
void set_server_courtesy_response(const ServerCourtesyResponse &server_courtesy_response)
Called by esphome generated code to set the server courtesy response object.
ServerCourtesyResponse get_server_courtesy_response() const
Get the server courtesy response object.
ServerRegister * find_containing_register_(uint32_t address) const
Find the registered value whose register span contains address, or nullptr if none does.
MODBUS_SERVER_BIT_COUNT modbus::ResponseStatus on_read_registers(uint16_t start_address, uint16_t number_of_registers, modbus::RegisterValues &registers) final
called when a modbus request (function code 0x03 or 0x04) was parsed without errors
ServerBit * find_bit_(uint16_t address) const
Find the registered bit at address, or nullptr if none is.
modbus::ResponseStatus on_write_coils(uint16_t start_address, modbus::PackedBits bits) final
called when a modbus request (function code 0x05 or 0x0F) was parsed without errors
ServerCourtesyResponse server_courtesy_response_
Server courtesy response.
A single bit in the server's coil/discrete-input table.
bool(*)(uint16_t address, bool value) WriteFn
void set_read_lambda(ReadFn read_fn)
optional< bool >(*)(uint16_t address) ReadFn
Returning an empty optional declines the read: the whole request is answered with a SERVICE_DEVICE_FA...
void set_write_lambda(WriteFn write_fn)
void set_allow_partial_read(bool allow_partial_read)
void set_read_lambda(ReadFn< RegisterValueType< VALUE_TYPE > > read_fn)
VALUE_TYPE must be this register's value_type; read() and write() cast back by it.
bool(*)(uint16_t address, T value) WriteFn
void set_write_lambda(WriteFn< RegisterValueType< VALUE_TYPE > > write_fn)
const char * format_value(int64_t value, char *buf, size_t buf_size) const
optional< T >(*)(uint16_t address) ReadFn
ServerRegister(uint16_t address, SensorValueType value_type, uint8_t register_count)
static constexpr size_t FORMAT_VALUE_BUF_SIZE
bool write(int64_t number) const
Converts a raw number (floats as their IEEE 754 bits) to the register's type and calls the write lamb...
optional< int64_t > read() const
Calls the read lambda and returns its value as a raw number (floats as their IEEE 754 bits).
decltype(registers_to_value< VALUE_TYPE >(static_cast< const uint16_t * >(nullptr))) RegisterValueType
The type registers_to_value() yields for a given value type.
uint32_t value
Definition hal.cpp:59
static void uint32_t