8 static const char *
const TAG =
"modbus";
16 const uint32_t now =
millis();
26 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - parse failed", at);
35 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - timeout", at);
53 ESP_LOGVV(TAG,
"Modbus received Byte %d (0X%x)", byte, byte);
58 uint8_t function_code = raw[1];
64 uint8_t data_len = raw[2];
65 uint8_t data_offset = 3;
68 if (((function_code >= 65) && (function_code <= 72)) || ((function_code >= 100) && (function_code <= 110))) {
82 uint16_t computed_crc =
crc16(raw, data_offset + data_len);
83 uint16_t remote_crc = uint16_t(raw[data_offset + data_len]) | (uint16_t(raw[data_offset + data_len + 1]) << 8);
85 if (computed_crc != remote_crc)
88 ESP_LOGD(TAG,
"Modbus user-defined function %02X found", function_code);
98 if (function_code == 0x5 || function_code == 0x06 || function_code == 0xF || function_code == 0x10) {
105 if ((function_code & 0x80) == 0x80) {
111 if (at < data_offset + data_len)
115 if (at == data_offset + data_len)
119 uint16_t computed_crc =
crc16(raw, data_offset + data_len);
120 uint16_t remote_crc = uint16_t(raw[data_offset + data_len]) | (uint16_t(raw[data_offset + data_len + 1]) << 8);
121 if (computed_crc != remote_crc) {
123 ESP_LOGD(TAG,
"Modbus CRC Check failed, but ignored! %02X!=%02X", computed_crc, remote_crc);
125 ESP_LOGW(TAG,
"Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
130 std::vector<uint8_t> data(this->
rx_buffer_.begin() + data_offset, this->
rx_buffer_.begin() + data_offset + data_len);
132 for (
auto *device : this->
devices_) {
133 if (device->address_ == address) {
135 if ((function_code & 0x80) == 0x80) {
136 ESP_LOGD(TAG,
"Modbus error function code: 0x%X exception: %d", function_code, raw[2]);
138 device->on_modbus_error(function_code & 0x7F, raw[2]);
141 ESP_LOGD(TAG,
"Ignoring Modbus error - not expecting a response");
144 device->on_modbus_read_registers(function_code, uint16_t(data[1]) | (uint16_t(data[0]) << 8),
145 uint16_t(data[3]) | (uint16_t(data[2]) << 8));
147 device->on_modbus_data(data);
155 ESP_LOGW(TAG,
"Got Modbus frame from unknown address 0x%02X! ", address);
159 ESP_LOGV(TAG,
"Clearing buffer of %d bytes - parse succeeded", at);
165 ESP_LOGCONFIG(TAG,
"Modbus:");
168 ESP_LOGCONFIG(TAG,
" CRC Disabled: %s", YESNO(this->
disable_crc_));
175 void Modbus::send(uint8_t
address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities,
176 uint8_t payload_len,
const uint8_t *payload) {
177 static const size_t MAX_VALUES = 128;
181 if (number_of_entities > MAX_VALUES && function_code <= 0x10) {
182 ESP_LOGE(TAG,
"send too many values %d max=%zu", number_of_entities, MAX_VALUES);
186 std::vector<uint8_t> data;
187 data.push_back(address);
188 data.push_back(function_code);
190 data.push_back(start_address >> 8);
191 data.push_back(start_address >> 0);
192 if (function_code != 0x5 && function_code != 0x6) {
193 data.push_back(number_of_entities >> 8);
194 data.push_back(number_of_entities >> 0);
198 if (payload !=
nullptr) {
200 data.push_back(payload_len);
204 for (
int i = 0; i < payload_len; i++) {
205 data.push_back(payload[i]);
209 auto crc =
crc16(data.data(), data.size());
210 data.push_back(
crc >> 0);
211 data.push_back(
crc >> 8);
229 if (payload.empty()) {
236 auto crc =
crc16(payload.data(), payload.size());
virtual void digital_write(bool value)=0
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
void write_array(const uint8_t *data, size_t len)
std::vector< uint8_t > rx_buffer_
bool parse_modbus_byte_(uint8_t byte)
void write_byte(uint8_t data)
void send_raw(const std::vector< uint8_t > &payload)
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)
std::vector< ModbusDevice * > devices_
float get_setup_priority() const override
GPIOPin * flow_control_pin_
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.
uint32_t IRAM_ATTR HOT millis()
void dump_config() override
const float BUS
For communication buses like i2c/spi.
bool read_byte(uint8_t *data)
uint32_t last_modbus_byte_
uint8_t waiting_for_response
Implementation of SPI Controller mode.