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;
227 for (uint16_t
len = min_length;
len <= std::min(
size,
size_t(MAX_FRAME_SIZE));
len++) {
238 if (
size < frame_length)
246 if (frame_length == 0)
247 return size < MAX_FRAME_SIZE;
248 ESP_LOGD(TAG,
"User-defined function %02X found", function_code);
257 std::span<const uint8_t> pdu(this->
rx_buffer_.data() + 1, frame_length - 3);
269 if (
size < frame_length)
277 if (frame_length == 0)
278 return size < MAX_FRAME_SIZE;
279 ESP_LOGD(TAG,
"User-defined function %02X found", function_code);
289 uint16_t data_len = frame_length - 2 - data_offset;
290 uint8_t data_buffer[MAX_FRAME_SIZE] = {};
291 std::memcpy(data_buffer, this->
rx_buffer_.data() + data_offset, data_len);
292 std::span<const uint8_t> data(data_buffer, data_len);
295 if (
address == BROADCAST_ADDRESS) {
310 if (cmd ==
nullptr) {
312 "Received unexpected frame from address %" PRIu8
", function code 0x%X, %" PRIu32
"ms after last send",
319 const uint8_t expected_function_code = cmd->
frame.
pdu()[0];
322 "Received incorrect frame address %" PRIu8
" <> %" PRIu8
" or function code 0x%X <> 0x%X, %" PRIu32
323 "ms after last send",
337 "Ignoring response from %" PRIu8
" - transmission interrupted by previous unexpected response, %" PRIu32
338 "ms after last send",
349 uint8_t exception =
pdu[1];
350 ESP_LOGW(TAG,
"Error function code: 0x%X exception: %" PRIu8
", address: %" PRIu8
", %" PRIu32
"ms after last send",
354 ESP_LOGV(TAG,
"Ignoring response from %" PRIu8
" - no callback device set, %" PRIu32
"ms after last send",
address,
361 ESP_LOGE(TAG,
"Unexpected response from address %" PRIu8
", which is mapped to this device.",
address);
365 ESP_LOGV(TAG,
"Expected response from peer %" PRIu8
" received",
address);
367 ESP_LOGV(TAG,
"Unexpected response from peer %" PRIu8
" received",
address);
376 for (
auto *device : this->
devices_) {
377 if (device->get_address() ==
address) {
386 ESP_LOGW(TAG,
"Address out of range - start: %" PRIu16
" num: %" PRIu16, start_address, count);
395static constexpr size_t WRITE_SINGLE_VALUES_OFFSET = 2;
396static constexpr size_t WRITE_MULTIPLE_VALUES_OFFSET = 5;
398static constexpr size_t READ_WRITE_VALUES_OFFSET = 9;
402static_assert(1 + WRITE_MULTIPLE_VALUES_OFFSET +
packed_bit_bytes(MAX_NUM_OF_COILS_TO_WRITE) <= MAX_PDU_SIZE,
403 "the largest FC 0x0F coil write must fit within MAX_PDU_SIZE");
409 this->
assemble_registers_(data.subspan(WRITE_SINGLE_VALUES_OFFSET,
sizeof(uint16_t)), registers);
418 if (number_of_registers == 0 || number_of_registers > MAX_NUM_OF_REGISTERS_TO_WRITE ||
419 number_of_registers * 2 != number_of_bytes) {
420 ESP_LOGW(TAG,
"Invalid number of registers %" PRIu16
" or bytes %" PRIu8, number_of_registers, number_of_bytes);
426 this->
assemble_registers_(data.subspan(WRITE_MULTIPLE_VALUES_OFFSET, number_of_bytes), registers);
431 const LogString *entity_name, uint16_t &start_address,
437 if (count == 0 || count > max_entities) {
438 ESP_LOGW(TAG,
"Invalid number of %s %" PRIu16, LOG_STR_ARG(entity_name), count);
448 if (raw_value != 0xFF00 && raw_value != 0x0000) {
449 ESP_LOGW(TAG,
"Invalid coil value 0x%04X", raw_value);
453 value = raw_value == 0xFF00;
458 uint16_t &count, std::span<const uint8_t> &packed_bytes) {
462 if (number_of_bits == 0 || number_of_bits > MAX_NUM_OF_COILS_TO_WRITE ||
464 ESP_LOGW(TAG,
"Invalid number of coils %" PRIu16
" or bytes %" PRIu8, number_of_bits, number_of_bytes);
470 count = number_of_bits;
472 packed_bytes = data.subspan(WRITE_MULTIPLE_VALUES_OFFSET, number_of_bytes);
477 for (
size_t offset = 0; offset + 1 < values.size(); offset += 2) {
487 uint16_t start_address;
489 uint16_t coil_count = 0;
490 std::span<const uint8_t> packed_bytes;
491 uint8_t single_bit = 0;
505 single_bit = value ? 0x01 : 0x00;
507 packed_bytes = std::span<const uint8_t>(&single_bit, 1);
516 ESP_LOGV(TAG,
"Ignoring broadcast with unsupported function code %" PRIu8, function_code);
524 bool accepted =
false;
525 for (
auto *device : this->
devices_) {
529 coils ? device->on_write_coils(start_address,
PackedBits(packed_bytes, coil_count))
530 : device->on_write_registers(start_address, registers);
531 if (device_status.has_value()) {
532 ESP_LOGV(TAG,
"Device %" PRIu8
" rejected broadcast write with exception %" PRIu8, device->get_address(),
533 static_cast<uint8_t
>(device_status.value()));
538 if (!accepted && !this->devices_.empty()) {
539 const uint16_t entity_count = coils ? coil_count :
static_cast<uint16_t
>(registers.
size());
540 const LogString *
const entity_name = coils ? LOG_STR(
"coils") : LOG_STR(
"registers");
547 ESP_LOGW(TAG,
"No device accepted broadcast write of %" PRIu16
" %s at 0x%04X", entity_count,
548 LOG_STR_ARG(entity_name), start_address);
550 ESP_LOGV(TAG,
"No device accepted broadcast write of %" PRIu16
" %s at 0x%04X", entity_count,
551 LOG_STR_ARG(entity_name), start_address);
558 std::span<uint8_t> response_buffer, uint16_t &response_len) {
565 if (registers.
size() != number_of_registers) {
566 ESP_LOGE(TAG,
"Incorrect response %" PRIu16
" requested, %zu returned", number_of_registers, registers.
size());
573 if (number_of_registers > MAX_NUM_OF_REGISTERS_TO_READ) {
574 ESP_LOGE(TAG,
"Read response of %" PRIu16
" registers exceeds the limit of %" PRIu16, number_of_registers,
575 MAX_NUM_OF_REGISTERS_TO_READ);
583 const size_t required =
static_cast<size_t>(response_len) + 1 +
static_cast<size_t>(number_of_registers) * 2;
584 if (required > response_buffer.size()) {
585 ESP_LOGE(TAG,
"Read response needs %zu bytes but only %zu are available", required, response_buffer.size());
590 response_buffer[response_len++] =
static_cast<uint8_t
>(number_of_registers * 2);
591 for (
auto r : registers) {
593 response_buffer[response_len++] = register_bytes[0];
594 response_buffer[response_len++] = register_bytes[1];
600 std::span<const uint8_t> data) {
602 if (device ==
nullptr) {
604 ESP_LOGV(TAG,
"Request to peer %" PRIu8
" received",
address);
609 uint8_t response_buffer[modbus::MAX_RAW_SIZE];
610 const uint8_t *response_data = response_buffer;
611 uint16_t response_len = 0;
616 uint16_t start_address;
617 uint16_t number_of_registers;
619 number_of_registers);
631 response_buffer, response_len)) {
639 uint16_t start_address;
650 response_data = data.data();
656 uint16_t start_address;
657 uint16_t number_of_bits;
659 this->
parse_read_request_(data, MAX_NUM_OF_COILS_TO_READ, LOG_STR(
"bits"), start_address, number_of_bits);
665 const uint8_t byte_count =
static_cast<uint8_t
>(
packed_bit_bytes(number_of_bits));
666 response_buffer[response_len++] = byte_count;
669 std::span<uint8_t> packed_out = std::span<uint8_t>(response_buffer).subspan(response_len, byte_count);
670 std::fill(packed_out.begin(), packed_out.end(), 0);
680 response_len += byte_count;
686 uint16_t start_address;
692 const uint8_t single_bit = value ? 0x01 : 0x00;
694 response_data = data.data();
700 uint16_t start_address;
702 std::span<const uint8_t> packed_bytes;
708 response_data = data.data();
720 if (number_of_registers == 0 || number_of_registers > MAX_NUM_OF_REGISTERS_TO_READ ||
721 number_of_write_registers == 0 || number_of_write_registers > MAX_NUM_OF_REGISTERS_TO_WRITE_RW ||
722 number_of_write_registers * 2 != number_of_bytes) {
723 ESP_LOGW(TAG,
"Invalid number of registers (read %" PRIu16
", write %" PRIu16
") or bytes %" PRIu8,
724 number_of_registers, number_of_write_registers, number_of_bytes);
729 if (!
status.has_value()) {
739 this->
assemble_registers_(data.subspan(READ_WRITE_VALUES_OFFSET, number_of_bytes), write_registers);
752 response_buffer, response_len)) {
758 ESP_LOGW(TAG,
"Unsupported function code %" PRIu8, function_code);
763 this->
send_response_(address, function_code, response_data, response_len);
771 if (tx_delay_remaining > 0) {
793#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
796 ESP_LOGV(TAG,
"Write: %s %" PRIu32
"ms after last send, %" PRIu32
"ms after last receive",
798 now - this->last_modbus_byte_);
812 ESP_LOGV(TAG,
"Send deferred for %" PRIu8
": a frame arrived during the send delay, will retry",
823 ESP_LOGV(TAG,
"Broadcast to address 0 sent; no reply expected (fire-and-forget)");
834 " Send Wait Time: %" PRIu16
" ms\n"
835 " Turnaround Time: %" PRIu16
" ms\n"
836 " Frame Delay: %" PRIu16
" ms\n"
837 " Long Rx Buffer Delay: %" PRIu16
" ms",
845 " Frame Delay: %" PRIu16
" ms\n"
846 " Long Rx Buffer Delay: %" PRIu16
" ms",
857 uint16_t payload_len) {
860 if (payload_len + 2 > MAX_RAW_SIZE) {
861 ESP_LOGE(TAG,
"Server response too large (%" PRIu16
" bytes)",
static_cast<uint16_t
>(payload_len + 2));
864 uint8_t raw_frame[MAX_RAW_SIZE];
866 raw_frame[1] = function_code;
867 std::memcpy(raw_frame + 2, payload, payload_len);
868 this->
send_raw_(raw_frame, payload_len + 2);
878 ESP_LOGD(TAG,
"Exception %" PRIu8
" replied to function 0x%02X for address %" PRIu8,
879 static_cast<uint8_t
>(
status.value()), function_code,
address);
885 uint8_t raw_frame[3];
888 raw_frame[2] =
static_cast<uint8_t
>(exception_code);
894 if (cmd.waiting_state())
904 const auto age = [now](
const ModbusDeviceCommand &cmd) -> uint16_t {
return now - cmd.seq; };
911 (cmd.priority() == best->
priority() && older(cmd, *best))) {
921 if (this->
device ==
nullptr)
930 if (this->
device !=
nullptr)
940 if (this->
device ==
nullptr)
950 if (this->
device ==
nullptr)
973 if (this->
device ==
nullptr)
987 const size_t work_set = this->
tx_buffer_.size();
989 bool callback_ran =
true;
990 while (callback_ran) {
991 callback_ran =
false;
992 for (
size_t i = 0; i != work_set && !callback_ran; i++) {
1022 for (
size_t i = this->
tx_buffer_.size(); i-- > 0;) {
1038 ESP_LOGW(TAG,
"Empty PDU refused for address %" PRIu8,
address);
1042 if (
pdu.size() > MAX_PDU_SIZE) {
1043 ESP_LOGE(TAG,
"Frame too large, refused: %" PRIu8
":%zu bytes",
address,
pdu.size());
1058 ESP_LOGW(TAG,
"Broadcast refused for function 0x%X: a broadcast (address 0) is never answered",
pdu[0]);
1064 bool continuous =
false;
1067 ESP_LOGV(TAG,
"continuous is ignored for a mutating function (0x%X, address %" PRIu8
")",
pdu[0],
address);
1084 const bool requeueable =
1087 ESP_LOGD(TAG,
"Anonymous duplicate of active frame for %" PRIu8
" (function 0x%X), dropped",
address,
pdu[0]);
1090 "Anonymous duplicate of active frame for %" PRIu8
" (function 0x%X), dropped - register a "
1091 "device for delivery accounting",
1097 item.make_continuous(
true);
1098 ESP_LOGV(TAG,
"Frame already active for %" PRIu8
", now polled continuously",
address);
1099 }
else if (item.continuous) {
1102 item.make_continuous(
false);
1103 ESP_LOGV(TAG,
"Frame already active for %" PRIu8
", downgraded from continuous to one-shot",
address);
1104 }
else if (!item.increment_pending()) {
1107 ESP_LOGD(TAG,
"Frame already active for %" PRIu8
" with %" PRIu8
" requests pending, refused",
address,
1111 ESP_LOGV(TAG,
"Frame already active for %" PRIu8
", request absorbed (pending %" PRIu8
")",
address,
1119 if (this->tx_buffer_.size() >= MODBUS_TX_BUFFER_SIZE) {
1120#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
1123 ESP_LOGE(TAG,
"Write buffer full, refused: %" PRIu8
":%s",
address,
1127#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
1130 ESP_LOGV(TAG,
"Adding frame to tx queue: %" PRIu8
":%s",
address,
1139 if (cmd.frame.address() !=
address)
1150 if (cmd.device !=
device)
1152 cmd.silent_retire();
1157void ModbusClientHub::send_raw(
const std::vector<uint8_t> &payload,
ModbusClientDevice *device) {
1159 ESP_LOGW(TAG,
"send_raw() payload too short to contain a PDU, refused");
1170 if (
len > MAX_RAW_SIZE) {
1171 ESP_LOGE(TAG,
"Server send frame too large (%" PRIu16
" bytes)",
len);
1185 this->deferred_payload_len_ - 1);
1187 ESP_LOGE(TAG,
"Deferred server reply dropped: transmission still blocked");
1194 ESP_LOGE(TAG,
"Server reply dropped: a frame arrived during the send delay");
1199 if (bytes_to_clear > 0 && bytes >= bytes_to_clear)
1200 bytes = bytes_to_clear;
1203 ESP_LOGW(TAG,
"Clearing buffer of %zu bytes - %s %" PRIu32
"ms after last send", bytes, LOG_STR_ARG(reason),
1206 ESP_LOGV(TAG,
"Clearing buffer of %zu bytes - %s %" PRIu32
"ms after last send", bytes, LOG_STR_ARG(reason),
1217void ModbusClientDevice::dispatch_response_(std::span<const uint8_t> request_pdu, std::span<const uint8_t> response_pdu,
1219 if (request_pdu.empty())
1221 auto function_code =
static_cast<FunctionCode>(request_pdu[0]);
1224 if (request_pdu.size() < READ_PDU_SIZE) {
1249 const size_t expected_data_size =
1250 bits ?
packed_bit_bytes(count_or_value) : static_cast<size_t>(count_or_value) * 2;
1251 if (response_pdu.size() != expected_data_size + 2) {
1252 ESP_LOGD(TAG,
"Response length %zu does not match request (expected %zu) for function code 0x%X",
1253 response_pdu.size(), expected_data_size + 2, static_cast<uint8_t>(function_code));
1263 switch (function_code) {
1279 for (
size_t i = 0; i != count_or_value; i++) {
1283 std::span<const uint16_t> register_span(registers.data(), registers.size());
1301 std::span<const uint8_t> packed_bytes;
1304 packed_bytes = response_pdu.subspan(2);
1305 count = count_or_value;
1307 PackedBits bits(packed_bytes, count);
1320 const uint16_t value = (
succeeded(status) && response_pdu.size() >= WRITE_SINGLE_PDU_SIZE)
1336 for (
size_t i = 0; i != count_or_value; i++) {
1339 std::span<const uint16_t> register_span(registers.data(), registers.size());
1348 std::span<const uint8_t> packed_bytes = request_pdu.subspan(6);
1349 PackedBits bits(packed_bytes, count_or_value);
1363 const uint8_t function_code = request_pdu.empty() ? 0 : request_pdu[0];
1368 ESP_LOGW(TAG,
"Non-standard request or response for function code 0x%X. No on_custom_response handler declared",
1371 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()
std::vector< uint8_t > rx_buffer_
uint16_t find_custom_frame_end_(uint16_t min_length) const
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_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.