9static const char *
const TAG =
"modbus";
27 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - parse failed", at);
36 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - timeout", at);
54 ESP_LOGVV(TAG,
"Modbus received Byte %d (0X%x)",
byte,
byte);
59 uint8_t function_code =
raw[1];
65 uint8_t data_len =
raw[2];
66 uint8_t data_offset = 3;
86 uint16_t computed_crc =
crc16(
raw, data_offset + data_len);
87 uint16_t remote_crc = uint16_t(
raw[data_offset + data_len]) | (uint16_t(
raw[data_offset + data_len + 1]) << 8);
89 if (computed_crc != remote_crc)
92 ESP_LOGD(TAG,
"Modbus user-defined function %02X found", function_code);
110 data_len = 2 + 2 + 1 +
raw[6];
131 if (at < data_offset + data_len)
135 if (at == data_offset + data_len)
139 uint16_t computed_crc =
crc16(
raw, data_offset + data_len);
140 uint16_t remote_crc = uint16_t(
raw[data_offset + data_len]) | (uint16_t(
raw[data_offset + data_len + 1]) << 8);
141 if (computed_crc != remote_crc) {
143 ESP_LOGD(TAG,
"Modbus CRC Check failed, but ignored! %02X!=%02X", computed_crc, remote_crc);
145 ESP_LOGW(TAG,
"Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
150 std::vector<uint8_t> data(this->
rx_buffer_.begin() + data_offset, this->rx_buffer_.begin() + data_offset + data_len);
152 for (
auto *device : this->
devices_) {
153 if (device->address_ ==
address) {
157 ESP_LOGD(TAG,
"Modbus error function code: 0x%X exception: %d", function_code,
raw[2]);
162 ESP_LOGD(TAG,
"Ignoring Modbus error - not expecting a response");
169 device->on_modbus_read_registers(function_code, uint16_t(data[1]) | (uint16_t(data[0]) << 8),
170 uint16_t(data[3]) | (uint16_t(data[2]) << 8));
175 device->on_modbus_write_registers(function_code, data);
180 device->on_modbus_data(data);
186 ESP_LOGW(TAG,
"Got Modbus frame from unknown address 0x%02X! ",
address);
190 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - parse succeeded", at);
196 ESP_LOGCONFIG(TAG,
"Modbus:");
199 " Send Wait Time: %d ms\n"
208void Modbus::send(uint8_t
address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities,
209 uint8_t payload_len,
const uint8_t *payload) {
210 static const size_t MAX_VALUES = 128;
215 ESP_LOGE(TAG,
"send too many values %d max=%zu", number_of_entities, MAX_VALUES);
219 std::vector<uint8_t> data;
221 data.push_back(function_code);
223 data.push_back(start_address >> 8);
224 data.push_back(start_address >> 0);
227 data.push_back(number_of_entities >> 8);
228 data.push_back(number_of_entities >> 0);
232 if (payload !=
nullptr) {
235 data.push_back(payload_len);
239 for (
int i = 0; i < payload_len; i++) {
240 data.push_back(payload[i]);
244 auto crc =
crc16(data.data(), data.size());
245 data.push_back(crc >> 0);
246 data.push_back(crc >> 8);
264 if (payload.empty()) {
271 auto crc =
crc16(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.
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.