11static const char *
const TAG =
"modbus";
14static constexpr size_t MODBUS_MAX_LOG_BYTES = 64;
17static constexpr uint32_t MODBUS_BITS_PER_CHAR = 11;
19static constexpr uint32_t MS_PER_SEC = 1000;
22static constexpr uint32_t UNACCEPTED_BROADCAST_WARN_INTERVAL_MS = 60 * MS_PER_SEC;
37 static constexpr uint16_t DEFAULT_LONG_RX_BUFFER_DELAY_MS = 50;
42 : DEFAULT_LONG_RX_BUFFER_DELAY_MS;
83 ESP_LOGW(TAG,
"Stop waiting for response from %" PRIu8
" %" PRIu32
"ms after last send", cmd->
frame.
address(),
84 this->last_receive_check_ - this->last_send_);
98 const uint16_t timeout = std::max(
100 (uint16_t) (this->
rx_buffer_.size() >= this->parent_->get_rx_full_threshold() ? this->long_rx_buffer_delay_ms_
113 return std::max({(int32_t) 0,
120 return std::max({(int32_t) 0,
167 if (buffer_size == 0) {
168 ESP_LOGV(TAG,
"Received first byte %" PRIu8
" (0X%x) of %zu bytes %" PRIu32
"ms after last send",
181 }
while (!this->
rx_buffer_.empty() && size > this->rx_buffer_.size());
190 ESP_LOGVV(TAG,
"Parsing frames buffer size = %" PRIu32,
size);
191 bool retry_as_client =
false;
193 const bool is_broadcast = this->
rx_buffer_[0] == BROADCAST_ADDRESS;
198 ESP_LOGV(TAG,
"Stop expecting peer response from %" PRIu8
" due to parse failure, and retry parse",
201 retry_as_client =
true;
205 "Stop expecting peer response from %" PRIu8
" due to timeout after partial response, and retry parse",
208 retry_as_client =
true;
228 const auto max_len =
static_cast<uint16_t
>(std::min(
size,
size_t(MAX_FRAME_SIZE)));
229 if (min_length > max_len)
234 uint16_t crc =
crc16(
raw, min_length);
237 for (uint16_t
len = min_length;
len < max_len;
len++) {
249 if (
size < frame_length)
257 if (frame_length == 0)
258 return size < MAX_FRAME_SIZE;
259 ESP_LOGD(TAG,
"Unknown-length function %02X found", function_code);
268 std::span<const uint8_t> pdu(this->
rx_buffer_.data() + 1, frame_length - 3);
280 if (
size < frame_length)
288 if (frame_length == 0)
289 return size < MAX_FRAME_SIZE;
290 ESP_LOGD(TAG,
"Unknown-length function %02X found", function_code);
300 uint16_t data_len = frame_length - 2 - data_offset;
301 uint8_t data_buffer[MAX_FRAME_SIZE] = {};
302 std::memcpy(data_buffer, this->
rx_buffer_.data() + data_offset, data_len);
303 std::span<const uint8_t> data(data_buffer, data_len);
306 if (
address == BROADCAST_ADDRESS) {
321 if (cmd ==
nullptr) {
323 "Received unexpected frame from address %" PRIu8
", function code 0x%X, %" PRIu32
"ms after last send",
330 const uint8_t expected_function_code = cmd->
frame.
pdu()[0];
333 "Received incorrect frame address %" PRIu8
" <> %" PRIu8
" or function code 0x%X <> 0x%X, %" PRIu32
334 "ms after last send",
348 "Ignoring response from %" PRIu8
" - transmission interrupted by previous unexpected response, %" PRIu32
349 "ms after last send",
360 uint8_t exception =
pdu[1];
361 ESP_LOGW(TAG,
"Error function code: 0x%X exception: %" PRIu8
", address: %" PRIu8
", %" PRIu32
"ms after last send",
365 ESP_LOGV(TAG,
"Ignoring response from %" PRIu8
" - no callback device set, %" PRIu32
"ms after last send",
address,
372 ESP_LOGE(TAG,
"Unexpected response from address %" PRIu8
", which is mapped to this device.",
address);
376 ESP_LOGV(TAG,
"Expected response from peer %" PRIu8
" received",
address);
378 ESP_LOGV(TAG,
"Unexpected response from peer %" PRIu8
" received",
address);
387 for (
auto *device : this->
devices_) {
388 if (device->get_address() ==
address) {
397 ESP_LOGW(TAG,
"Address out of range - start: %" PRIu16
" num: %" PRIu16, start_address, count);
406static constexpr size_t WRITE_SINGLE_VALUES_OFFSET = 2;
407static constexpr size_t WRITE_MULTIPLE_VALUES_OFFSET = 5;
409static constexpr size_t READ_WRITE_VALUES_OFFSET = 9;
413static_assert(1 + WRITE_MULTIPLE_VALUES_OFFSET +
packed_bit_bytes(MAX_NUM_OF_COILS_TO_WRITE) <= MAX_PDU_SIZE,
414 "the largest FC 0x0F coil write must fit within MAX_PDU_SIZE");
420 this->
assemble_registers_(data.subspan(WRITE_SINGLE_VALUES_OFFSET,
sizeof(uint16_t)), registers);
429 if (number_of_registers == 0 || number_of_registers > MAX_NUM_OF_REGISTERS_TO_WRITE ||
430 number_of_registers * 2 != number_of_bytes) {
431 ESP_LOGW(TAG,
"Invalid number of registers %" PRIu16
" or bytes %" PRIu8, number_of_registers, number_of_bytes);
437 this->
assemble_registers_(data.subspan(WRITE_MULTIPLE_VALUES_OFFSET, number_of_bytes), registers);
442 const LogString *entity_name, uint16_t &start_address,
448 if (count == 0 || count > max_entities) {
449 ESP_LOGW(TAG,
"Invalid number of %s %" PRIu16, LOG_STR_ARG(entity_name), count);
459 if (raw_value != 0xFF00 && raw_value != 0x0000) {
460 ESP_LOGW(TAG,
"Invalid coil value 0x%04X", raw_value);
464 value = raw_value == 0xFF00;
469 uint16_t &count, std::span<const uint8_t> &packed_bytes) {
473 if (number_of_bits == 0 || number_of_bits > MAX_NUM_OF_COILS_TO_WRITE ||
475 ESP_LOGW(TAG,
"Invalid number of coils %" PRIu16
" or bytes %" PRIu8, number_of_bits, number_of_bytes);
481 count = number_of_bits;
483 packed_bytes = data.subspan(WRITE_MULTIPLE_VALUES_OFFSET, number_of_bytes);
488 for (
size_t offset = 0; offset + 1 < values.size(); offset += 2) {
498 uint16_t start_address;
500 uint16_t coil_count = 0;
501 std::span<const uint8_t> packed_bytes;
502 uint8_t single_bit = 0;
516 single_bit = value ? 0x01 : 0x00;
518 packed_bytes = std::span<const uint8_t>(&single_bit, 1);
527 ESP_LOGV(TAG,
"Ignoring broadcast with unsupported function code %" PRIu8, function_code);
535 bool accepted =
false;
536 for (
auto *device : this->
devices_) {
540 coils ? device->on_write_coils(start_address,
PackedBits(packed_bytes, coil_count))
541 : device->on_write_registers(start_address, registers);
542 if (device_status.has_value()) {
543 ESP_LOGV(TAG,
"Device %" PRIu8
" rejected broadcast write with exception %" PRIu8, device->get_address(),
544 static_cast<uint8_t
>(device_status.value()));
549 if (!accepted && !this->devices_.empty()) {
550 const uint16_t entity_count = coils ? coil_count :
static_cast<uint16_t
>(registers.
size());
551 const LogString *
const entity_name = coils ? LOG_STR(
"coils") : LOG_STR(
"registers");
558 ESP_LOGW(TAG,
"No device accepted broadcast write of %" PRIu16
" %s at 0x%04X", entity_count,
559 LOG_STR_ARG(entity_name), start_address);
561 ESP_LOGV(TAG,
"No device accepted broadcast write of %" PRIu16
" %s at 0x%04X", entity_count,
562 LOG_STR_ARG(entity_name), start_address);
569 std::span<uint8_t> response_buffer, uint16_t &response_len) {
576 if (registers.
size() != number_of_registers) {
577 ESP_LOGE(TAG,
"Incorrect response %" PRIu16
" requested, %zu returned", number_of_registers, registers.
size());
584 if (number_of_registers > MAX_NUM_OF_REGISTERS_TO_READ) {
585 ESP_LOGE(TAG,
"Read response of %" PRIu16
" registers exceeds the limit of %" PRIu16, number_of_registers,
586 MAX_NUM_OF_REGISTERS_TO_READ);
594 const size_t required =
static_cast<size_t>(response_len) + 1 +
static_cast<size_t>(number_of_registers) * 2;
595 if (required > response_buffer.size()) {
596 ESP_LOGE(TAG,
"Read response needs %zu bytes but only %zu are available", required, response_buffer.size());
601 response_buffer[response_len++] =
static_cast<uint8_t
>(number_of_registers * 2);
602 for (
auto r : registers) {
604 response_buffer[response_len++] = register_bytes[0];
605 response_buffer[response_len++] = register_bytes[1];
611 std::span<const uint8_t> data) {
613 if (device ==
nullptr) {
615 ESP_LOGV(TAG,
"Request to peer %" PRIu8
" received",
address);
620 uint8_t response_buffer[modbus::MAX_RAW_SIZE];
621 const uint8_t *response_data = response_buffer;
622 uint16_t response_len = 0;
627 uint16_t start_address;
628 uint16_t number_of_registers;
630 number_of_registers);
642 response_buffer, response_len)) {
650 uint16_t start_address;
661 response_data = data.data();
667 uint16_t start_address;
668 uint16_t number_of_bits;
670 this->
parse_read_request_(data, MAX_NUM_OF_COILS_TO_READ, LOG_STR(
"bits"), start_address, number_of_bits);
676 const uint8_t byte_count =
static_cast<uint8_t
>(
packed_bit_bytes(number_of_bits));
677 response_buffer[response_len++] = byte_count;
680 std::span<uint8_t> packed_out = std::span<uint8_t>(response_buffer).subspan(response_len, byte_count);
681 std::fill(packed_out.begin(), packed_out.end(), 0);
691 response_len += byte_count;
697 uint16_t start_address;
703 const uint8_t single_bit = value ? 0x01 : 0x00;
705 response_data = data.data();
711 uint16_t start_address;
713 std::span<const uint8_t> packed_bytes;
719 response_data = data.data();
731 if (number_of_registers == 0 || number_of_registers > MAX_NUM_OF_REGISTERS_TO_READ ||
732 number_of_write_registers == 0 || number_of_write_registers > MAX_NUM_OF_REGISTERS_TO_WRITE_RW ||
733 number_of_write_registers * 2 != number_of_bytes) {
734 ESP_LOGW(TAG,
"Invalid number of registers (read %" PRIu16
", write %" PRIu16
") or bytes %" PRIu8,
735 number_of_registers, number_of_write_registers, number_of_bytes);
740 if (!
status.has_value()) {
750 this->
assemble_registers_(data.subspan(READ_WRITE_VALUES_OFFSET, number_of_bytes), write_registers);
763 response_buffer, response_len)) {
769 ESP_LOGW(TAG,
"Unsupported function code %" PRIu8, function_code);
774 this->
send_response_(address, function_code, response_data, response_len);
782 if (tx_delay_remaining > 0) {
804#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
807 ESP_LOGV(TAG,
"Write: %s %" PRIu32
"ms after last send, %" PRIu32
"ms after last receive",
809 now - this->last_modbus_byte_);
823 ESP_LOGV(TAG,
"Send deferred for %" PRIu8
": a frame arrived during the send delay, will retry",
834 ESP_LOGV(TAG,
"Broadcast to address 0 sent; no reply expected (fire-and-forget)");
845 " Send Wait Time: %" PRIu16
" ms\n"
846 " Turnaround Time: %" PRIu16
" ms\n"
847 " Frame Delay: %" PRIu16
" ms\n"
848 " Long Rx Buffer Delay: %" PRIu16
" ms",
856 " Frame Delay: %" PRIu16
" ms\n"
857 " Long Rx Buffer Delay: %" PRIu16
" ms",
868 uint16_t payload_len) {
871 if (payload_len + 2 > MAX_RAW_SIZE) {
872 ESP_LOGE(TAG,
"Server response too large (%" PRIu16
" bytes)",
static_cast<uint16_t
>(payload_len + 2));
875 uint8_t raw_frame[MAX_RAW_SIZE];
877 raw_frame[1] = function_code;
878 std::memcpy(raw_frame + 2, payload, payload_len);
879 this->
send_raw_(raw_frame, payload_len + 2);
889 ESP_LOGD(TAG,
"Exception %" PRIu8
" replied to function 0x%02X for address %" PRIu8,
890 static_cast<uint8_t
>(
status.value()), function_code,
address);
896 uint8_t raw_frame[3];
899 raw_frame[2] =
static_cast<uint8_t
>(exception_code);
905 if (cmd.waiting_state())
915 const auto age = [now](
const ModbusDeviceCommand &cmd) -> uint16_t {
return now - cmd.seq; };
922 (cmd.priority() == best->
priority() && older(cmd, *best))) {
932 if (this->
device ==
nullptr)
941 if (this->
device !=
nullptr)
951 if (this->
device ==
nullptr)
961 if (this->
device ==
nullptr)
984 if (this->
device ==
nullptr)
998 const size_t work_set = this->
tx_buffer_.size();
1000 bool callback_ran =
true;
1001 while (callback_ran) {
1002 callback_ran =
false;
1003 for (
size_t i = 0; i != work_set && !callback_ran; i++) {
1005 switch (cmd.
state) {
1033 for (
size_t i = this->
tx_buffer_.size(); i-- > 0;) {
1049 ESP_LOGW(TAG,
"Empty PDU refused for address %" PRIu8,
address);
1053 if (
pdu.size() > MAX_PDU_SIZE) {
1054 ESP_LOGE(TAG,
"Frame too large, refused: %" PRIu8
":%zu bytes",
address,
pdu.size());
1069 ESP_LOGW(TAG,
"Broadcast refused for function 0x%X: a broadcast (address 0) is never answered",
pdu[0]);
1075 bool continuous =
false;
1078 ESP_LOGV(TAG,
"continuous is ignored for a mutating function (0x%X, address %" PRIu8
")",
pdu[0],
address);
1095 const bool requeueable =
1098 ESP_LOGD(TAG,
"Anonymous duplicate of active frame for %" PRIu8
" (function 0x%X), dropped",
address,
pdu[0]);
1101 "Anonymous duplicate of active frame for %" PRIu8
" (function 0x%X), dropped - register a "
1102 "device for delivery accounting",
1108 item.make_continuous(
true);
1109 ESP_LOGV(TAG,
"Frame already active for %" PRIu8
", now polled continuously",
address);
1110 }
else if (item.continuous) {
1113 item.make_continuous(
false);
1114 ESP_LOGV(TAG,
"Frame already active for %" PRIu8
", downgraded from continuous to one-shot",
address);
1115 }
else if (!item.increment_pending()) {
1118 ESP_LOGD(TAG,
"Frame already active for %" PRIu8
" with %" PRIu8
" requests pending, refused",
address,
1122 ESP_LOGV(TAG,
"Frame already active for %" PRIu8
", request absorbed (pending %" PRIu8
")",
address,
1130 if (this->tx_buffer_.size() >= MODBUS_TX_BUFFER_SIZE) {
1131#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
1134 ESP_LOGE(TAG,
"Write buffer full, refused: %" PRIu8
":%s",
address,
1138#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
1141 ESP_LOGV(TAG,
"Adding frame to tx queue: %" PRIu8
":%s",
address,
1150 if (cmd.frame.address() !=
address)
1161 if (cmd.device !=
device)
1163 cmd.silent_retire();
1168void ModbusClientHub::send_raw(
const std::vector<uint8_t> &payload,
ModbusClientDevice *device) {
1170 ESP_LOGW(TAG,
"send_raw() payload too short to contain a PDU, refused");
1181 if (
len > MAX_RAW_SIZE) {
1182 ESP_LOGE(TAG,
"Server send frame too large (%" PRIu16
" bytes)",
len);
1196 this->deferred_payload_len_ - 1);
1198 ESP_LOGE(TAG,
"Deferred server reply dropped: transmission still blocked");
1205 ESP_LOGE(TAG,
"Server reply dropped: a frame arrived during the send delay");
1210 if (bytes_to_clear > 0 && bytes >= bytes_to_clear)
1211 bytes = bytes_to_clear;
1214 ESP_LOGW(TAG,
"Clearing buffer of %zu bytes - %s %" PRIu32
"ms after last send", bytes, LOG_STR_ARG(reason),
1217 ESP_LOGV(TAG,
"Clearing buffer of %zu bytes - %s %" PRIu32
"ms after last send", bytes, LOG_STR_ARG(reason),
1228void ModbusClientDevice::dispatch_response_(std::span<const uint8_t> request_pdu, std::span<const uint8_t> response_pdu,
1230 if (request_pdu.empty())
1232 auto function_code =
static_cast<FunctionCode>(request_pdu[0]);
1235 if (request_pdu.size() < READ_PDU_SIZE) {
1260 const size_t expected_data_size =
1261 bits ?
packed_bit_bytes(count_or_value) : static_cast<size_t>(count_or_value) * 2;
1262 if (response_pdu.size() != expected_data_size + 2) {
1263 ESP_LOGD(TAG,
"Response length %zu does not match request (expected %zu) for function code 0x%X",
1264 response_pdu.size(), expected_data_size + 2, static_cast<uint8_t>(function_code));
1274 switch (function_code) {
1290 for (
size_t i = 0; i != count_or_value; i++) {
1294 std::span<const uint16_t> register_span(registers.data(), registers.size());
1312 std::span<const uint8_t> packed_bytes;
1315 packed_bytes = response_pdu.subspan(2);
1316 count = count_or_value;
1318 PackedBits bits(packed_bytes, count);
1331 const uint16_t value = (
succeeded(status) && response_pdu.size() >= WRITE_SINGLE_PDU_SIZE)
1347 for (
size_t i = 0; i != count_or_value; i++) {
1350 std::span<const uint16_t> register_span(registers.data(), registers.size());
1359 std::span<const uint8_t> packed_bytes = request_pdu.subspan(6);
1360 PackedBits bits(packed_bytes, count_or_value);
1374 const uint8_t function_code = request_pdu.empty() ? 0 : request_pdu[0];
1379 ESP_LOGW(TAG,
"Non-standard request or response for function code 0x%X. No on_custom_response handler declared",
1382 ESP_LOGV(TAG,
"Non-standard request or response for function code 0x%X (unhandled)", function_code);
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
virtual void digital_write(bool value)=0
Minimal static vector - saves memory by avoiding std::vector overhead.
void push_back(const T &value)
virtual void on_response(std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu)
Low-level response hook: called with the request PDU this device sent and the response PDU received T...
virtual void on_write_multiple_coils(uint16_t start_address, PackedBits bits, ResponseStatus status)
virtual void on_read_holding_registers(uint16_t start_address, std::span< const uint16_t > registers, ResponseStatus status)
virtual void on_write_multiple_registers(uint16_t start_address, std::span< const uint16_t > registers, ResponseStatus status)
virtual void on_sent(std::span< const uint8_t > request_pdu)
Called when this device's frame is actually written to the wire.
virtual void on_write_single_register(uint16_t address, uint16_t value, ResponseStatus status)
Write acknowledgements.
virtual bool on_no_response(std::span< const uint8_t > request_pdu)
Called when no matching, uninterrupted response arrived; return true to have the hub re-queue the fra...
virtual void on_custom_response(std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu, ResponseStatus status)
Catch-all for custom function codes and anything that is not a standard-conformant transaction (see d...
virtual void on_read_discrete_inputs(uint16_t start_address, PackedBits bits, ResponseStatus status)
virtual void on_error(std::span< const uint8_t > request_pdu, ExceptionCode exception_code)
Low-level error hook: called with the request PDU and the modbus exception code from the error respon...
virtual void on_not_sent(std::span< const uint8_t > request_pdu)
Called when an accepted request was dropped before transmission by clear_tx_queue_for_address().
virtual void on_read_coils(uint16_t start_address, PackedBits bits, ResponseStatus status)
virtual void on_write_single_coil(uint16_t address, bool value, ResponseStatus status)
bool custom_response_warned_
virtual void on_read_input_registers(uint16_t start_address, std::span< const uint16_t > registers, ResponseStatus status)
void clear_tx_queue_for_device(ModbusClientDevice *device)
void parse_modbus_frames() override
ModbusDeviceCommand * find_waiting_()
std::span< const uint8_t > pdu
void dump_config() override
uint16_t turnaround_delay_ms_
uint8_t uint16_t uint16_t uint8_t const uint8_t ModbusClientDevice * device
bool waiting_for_response_
ModbusDeviceCommand * select_next_ready_()
uint8_t uint16_t uint16_t uint8_t const uint8_t * payload
int32_t tx_delay_remaining() override
std::deque< ModbusDeviceCommand > tx_buffer_
bool tx_blocked() override
bool queue_pdu(uint8_t address, std::span< const uint8_t > pdu, ModbusClientDevice *device=nullptr, CommandOptions options={})
Queue a request.
void clear_tx_queue_for_address(uint8_t address)
void process_modbus_server_frame(uint8_t address, std::span< const uint8_t > pdu) override
virtual void process_modbus_server_frame(uint8_t address, std::span< const uint8_t > pdu)=0
bool parse_modbus_server_frame_()
virtual void parse_modbus_frames()=0
bool send_frame_(const ModbusFrame &frame)
uint32_t last_modbus_byte_
GPIOPin * flow_control_pin_
uint32_t last_send_tx_offset_
virtual bool tx_blocked()
void clear_rx_buffer_(const LogString *reason, bool warn=false, size_t bytes_to_clear=0)
float get_setup_priority() const override
uint16_t long_rx_buffer_delay_ms_
virtual int32_t tx_delay_remaining()
uint16_t find_frame_end_by_crc_(uint16_t min_length) const
std::vector< uint8_t > rx_buffer_
uint32_t last_receive_check_
virtual ResponseStatus on_read_coils(uint16_t start_address, MutablePackedBits bits)
virtual ResponseStatus on_write_registers(uint16_t start_address, const RegisterValues ®isters)
virtual ResponseStatus on_read_holding_registers(uint16_t start_address, uint16_t number_of_registers, RegisterValues ®isters)
virtual ResponseStatus on_read_discrete_inputs(uint16_t start_address, MutablePackedBits bits)
virtual ResponseStatus on_write_coils(uint16_t start_address, PackedBits bits)
Coil writes deliver the values as a PackedBits view over the hub's receive buffer (only valid during ...
virtual ResponseStatus on_read_input_registers(uint16_t start_address, uint16_t number_of_registers, RegisterValues ®isters)
std::vector< ModbusServerDevice * > devices_
ResponseStatus check_address_range_(uint16_t start_address, uint16_t count)
ResponseStatus parse_read_request_(std::span< const uint8_t > data, uint16_t max_entities, const LogString *entity_name, uint16_t &start_address, uint16_t &count)
void process_modbus_client_frame_(uint8_t address, uint8_t function_code, std::span< const uint8_t > data)
void parse_modbus_frames() override
void process_modbus_server_frame(uint8_t address, std::span< const uint8_t > pdu) override
ResponseStatus parse_write_multiple_coils_(std::span< const uint8_t > data, uint16_t &start_address, uint16_t &count, std::span< const uint8_t > &packed_bytes)
void process_broadcast_frame_(uint8_t function_code, std::span< const uint8_t > data)
uint16_t deferred_payload_len_
ModbusServerDevice * find_device_(uint8_t address)
uint32_t last_unaccepted_broadcast_warn_
ResponseStatus parse_write_single_coil_(std::span< const uint8_t > data, uint16_t &start_address, bool &value)
bool build_or_reject_read_response_(uint8_t address, uint8_t function_code, ResponseStatus status, uint16_t number_of_registers, const RegisterValues ®isters, std::span< uint8_t > response_buffer, uint16_t &response_len)
void send_exception_(uint8_t address, uint8_t function_code, ExceptionCode exception_code)
void assemble_registers_(std::span< const uint8_t > values, RegisterValues ®isters)
void send_raw_(const uint8_t *payload, uint16_t len)
bool parse_modbus_client_frame_()
void dump_config() override
void send_response_(uint8_t address, uint8_t function_code, const uint8_t *payload, uint16_t payload_len)
ResponseStatus parse_write_multiple_(std::span< const uint8_t > data, uint16_t &start_address, RegisterValues ®isters)
bool rejected_(uint8_t address, uint8_t function_code, ResponseStatus status)
ResponseStatus parse_write_single_(std::span< const uint8_t > data, uint16_t &start_address, RegisterValues ®isters)
std::array< uint8_t, MAX_RAW_SIZE > deferred_payload_
uint8_t expecting_peer_response_
Mutable counterpart of PackedBits: set() writes bits in place (deliberately no proxy operator[]=).
Read-only view of Modbus-packed bits: bit 0 of byte 0 is the first bit (LSB first),...
uint32_t get_baud_rate() const
static constexpr size_t RX_FULL_THRESHOLD_UNSET
size_t get_rx_full_threshold()
optional< std::array< uint8_t, N > > read_array()
void write_array(const uint8_t *data, size_t len)
bool address_range_fits(uint16_t start_address, size_t count)
bool is_function_code_read_only(uint8_t function_code)
uint8_t client_frame_data_offset(const uint8_t *, size_t)
T get_data(const uint8_t *data, size_t buffer_offset)
Extract data from modbus response buffer.
bool is_function_code_read(uint8_t function_code)
uint16_t client_frame_length(const uint8_t *frame, size_t size)
bool is_server_pdu_standard(const uint8_t *pdu, size_t size)
uint16_t server_frame_length(const uint8_t *frame, size_t size)
bool is_function_code_unknown_length(uint8_t function_code)
True for any function code whose frame length the parsers cannot predict - everything the server_pdu_...
bool is_function_code_custom(uint8_t function_code)
bool is_client_pdu_standard(const uint8_t *pdu, size_t size)
bool is_function_code_exception(uint8_t function_code)
const uint8_t FUNCTION_CODE_MASK
StaticVector< uint16_t, MAX_NUM_OF_REGISTERS_TO_READ > RegisterValues
const uint8_t FUNCTION_CODE_EXCEPTION_MASK
std::optional< ExceptionCode > ResponseStatus
bool succeeded(ResponseStatus status)
True when a transaction carried no exception.
@ WRITE_MULTIPLE_REGISTERS
@ READ_WRITE_MULTIPLE_REGISTERS
constexpr size_t packed_bit_bytes(size_t bits)
Bits pack 8 per data byte, rounded up to whole bytes.
constexpr float BUS
For communication buses like i2c/spi.
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
void HOT delay(uint32_t ms)
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
uint32_t IRAM_ATTR HOT millis()
bool response(std::span< const uint8_t > response_pdu)
void complete_broadcast()
CommandPriority priority() const
static CommandPriority classify(uint8_t function_code)
bool error(ExceptionCode exception_code)
bool waiting_state() const
void requeue(uint16_t seq)
ModbusClientDevice * device
SmallInlineBuffer< MODBUS_FRAME_INLINE_SIZE > data
std::span< const uint8_t > pdu() const
The PDU: function code + data, without address or CRC.