Allow ignoring of failed Modbus response CRC (#3930)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Jan Grewe 2022-12-22 08:14:11 +01:00 committed by GitHub
parent ecbbf2d3f4
commit f2df542cb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 4 deletions

View File

@ -6,6 +6,7 @@ from esphome.const import (
CONF_FLOW_CONTROL_PIN,
CONF_ID,
CONF_ADDRESS,
CONF_DISABLE_CRC,
)
from esphome import pins
@ -27,6 +28,7 @@ CONFIG_SCHEMA = (
cv.Optional(
CONF_SEND_WAIT_TIME, default="250ms"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_DISABLE_CRC, default=False): cv.boolean,
}
)
.extend(cv.COMPONENT_SCHEMA)
@ -45,8 +47,8 @@ async def to_code(config):
pin = await gpio_pin_expression(config[CONF_FLOW_CONTROL_PIN])
cg.add(var.set_flow_control_pin(pin))
if CONF_SEND_WAIT_TIME in config:
cg.add(var.set_send_wait_time(config[CONF_SEND_WAIT_TIME]))
cg.add(var.set_send_wait_time(config[CONF_SEND_WAIT_TIME]))
cg.add(var.set_disable_crc(config[CONF_DISABLE_CRC]))
def modbus_device_schema(default_address):

View File

@ -102,8 +102,12 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
uint16_t computed_crc = crc16(raw, data_offset + data_len);
uint16_t remote_crc = uint16_t(raw[data_offset + data_len]) | (uint16_t(raw[data_offset + data_len + 1]) << 8);
if (computed_crc != remote_crc) {
ESP_LOGW(TAG, "Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
return false;
if (this->disable_crc_) {
ESP_LOGD(TAG, "Modbus CRC Check failed, but ignored! %02X!=%02X", computed_crc, remote_crc);
} else {
ESP_LOGW(TAG, "Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
return false;
}
}
}
std::vector<uint8_t> data(this->rx_buffer_.begin() + data_offset, this->rx_buffer_.begin() + data_offset + data_len);
@ -139,6 +143,7 @@ void Modbus::dump_config() {
ESP_LOGCONFIG(TAG, "Modbus:");
LOG_PIN(" Flow Control Pin: ", this->flow_control_pin_);
ESP_LOGCONFIG(TAG, " Send Wait Time: %d ms", this->send_wait_time_);
ESP_LOGCONFIG(TAG, " CRC Disabled: %s", YESNO(this->disable_crc_));
}
float Modbus::get_setup_priority() const {
// After UART bus

View File

@ -30,12 +30,14 @@ class Modbus : public uart::UARTDevice, public Component {
void set_flow_control_pin(GPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
uint8_t waiting_for_response{0};
void set_send_wait_time(uint16_t time_in_ms) { send_wait_time_ = time_in_ms; }
void set_disable_crc(bool disable_crc) { disable_crc_ = disable_crc; }
protected:
GPIOPin *flow_control_pin_{nullptr};
bool parse_modbus_byte_(uint8_t byte);
uint16_t send_wait_time_{250};
bool disable_crc_;
std::vector<uint8_t> rx_buffer_;
uint32_t last_modbus_byte_{0};
uint32_t last_send_{0};

View File

@ -175,6 +175,7 @@ CONF_DIO_PIN = "dio_pin"
CONF_DIR_PIN = "dir_pin"
CONF_DIRECTION = "direction"
CONF_DIRECTION_OUTPUT = "direction_output"
CONF_DISABLE_CRC = "disable_crc"
CONF_DISABLED_BY_DEFAULT = "disabled_by_default"
CONF_DISCONNECT_DELAY = "disconnect_delay"
CONF_DISCOVERY = "discovery"