9static const char *
const TAG =
"modbus";
12static constexpr size_t MODBUS_MAX_LOG_BYTES = 64;
30 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - parse failed", at);
39 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - timeout", at);
57 ESP_LOGVV(TAG,
"Modbus received Byte %d (0X%x)",
byte,
byte);
62 uint8_t function_code =
raw[1];
68 uint8_t data_len =
raw[2];
69 uint8_t data_offset = 3;
89 uint16_t computed_crc =
crc16(
raw, data_offset + data_len);
90 uint16_t remote_crc = uint16_t(
raw[data_offset + data_len]) | (uint16_t(
raw[data_offset + data_len + 1]) << 8);
92 if (computed_crc != remote_crc)
95 ESP_LOGD(TAG,
"Modbus user-defined function %02X found", function_code);
113 data_len = 2 + 2 + 1 +
raw[6];
134 if (at < data_offset + data_len)
138 if (at == data_offset + data_len)
142 uint16_t computed_crc =
crc16(
raw, data_offset + data_len);
143 uint16_t remote_crc = uint16_t(
raw[data_offset + data_len]) | (uint16_t(
raw[data_offset + data_len + 1]) << 8);
144 if (computed_crc != remote_crc) {
146 ESP_LOGD(TAG,
"Modbus CRC Check failed, but ignored! %02X!=%02X", computed_crc, remote_crc);
148 ESP_LOGW(TAG,
"Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
153 std::vector<uint8_t> data(this->
rx_buffer_.begin() + data_offset, this->rx_buffer_.begin() + data_offset + data_len);
155 for (
auto *device : this->
devices_) {
156 if (device->address_ ==
address) {
160 ESP_LOGD(TAG,
"Modbus error function code: 0x%X exception: %d", function_code,
raw[2]);
165 ESP_LOGD(TAG,
"Ignoring Modbus error - not expecting a response");
172 device->on_modbus_read_registers(function_code, uint16_t(data[1]) | (uint16_t(data[0]) << 8),
173 uint16_t(data[3]) | (uint16_t(data[2]) << 8));
178 device->on_modbus_write_registers(function_code, data);
183 device->on_modbus_data(data);
189 ESP_LOGW(TAG,
"Got Modbus frame from unknown address 0x%02X! ",
address);
193 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - parse succeeded", at);
199 ESP_LOGCONFIG(TAG,
"Modbus:");
202 " Send Wait Time: %d ms\n"
211void Modbus::send(uint8_t
address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities,
212 uint8_t payload_len,
const uint8_t *payload) {
213 static const size_t MAX_VALUES = 128;
218 ESP_LOGE(TAG,
"send too many values %d max=%zu", number_of_entities, MAX_VALUES);
222 std::vector<uint8_t> data;
224 data.push_back(function_code);
226 data.push_back(start_address >> 8);
227 data.push_back(start_address >> 0);
230 data.push_back(number_of_entities >> 8);
231 data.push_back(number_of_entities >> 0);
235 if (payload !=
nullptr) {
238 data.push_back(payload_len);
242 for (
int i = 0; i < payload_len; i++) {
243 data.push_back(payload[i]);
247 auto crc =
crc16(data.data(), data.size());
248 data.push_back(crc >> 0);
249 data.push_back(crc >> 8);
261#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
270 if (payload.empty()) {
277 auto crc =
crc16(payload.data(), payload.size());
285#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
288 ESP_LOGV(TAG,
"Modbus write raw: %s",
format_hex_pretty_to(hex_buf, payload.data(), payload.size()));
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void digital_write(bool value)=0
bool parse_modbus_byte_(uint8_t byte)
void send_raw(const std::vector< uint8_t > &payload)
uint8_t waiting_for_response
uint32_t last_modbus_byte_
GPIOPin * flow_control_pin_
std::vector< ModbusDevice * > devices_
float get_setup_priority() const override
void dump_config() override
std::vector< uint8_t > rx_buffer_
void send(uint8_t address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
bool read_byte(uint8_t *data)
void write_byte(uint8_t data)
void write_array(const uint8_t *data, size_t len)
const uint8_t FUNCTION_CODE_MASK
const uint8_t FUNCTION_CODE_EXCEPTION_MASK
@ WRITE_MULTIPLE_REGISTERS
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_2_INIT
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_1_INIT
Modbus definitions from specs: https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3....
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_2_END
const uint8_t FUNCTION_CODE_USER_DEFINED_SPACE_1_END
const float BUS
For communication buses like i2c/spi.
Providing packet encoding functions for exchanging data with a remote host.
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".
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.