mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 10:00:36 +01:00
Allow ignoring of failed Modbus response CRC (#3930)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
ecbbf2d3f4
commit
f2df542cb1
@ -6,6 +6,7 @@ from esphome.const import (
|
|||||||
CONF_FLOW_CONTROL_PIN,
|
CONF_FLOW_CONTROL_PIN,
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_ADDRESS,
|
CONF_ADDRESS,
|
||||||
|
CONF_DISABLE_CRC,
|
||||||
)
|
)
|
||||||
from esphome import pins
|
from esphome import pins
|
||||||
|
|
||||||
@ -27,6 +28,7 @@ CONFIG_SCHEMA = (
|
|||||||
cv.Optional(
|
cv.Optional(
|
||||||
CONF_SEND_WAIT_TIME, default="250ms"
|
CONF_SEND_WAIT_TIME, default="250ms"
|
||||||
): cv.positive_time_period_milliseconds,
|
): cv.positive_time_period_milliseconds,
|
||||||
|
cv.Optional(CONF_DISABLE_CRC, default=False): cv.boolean,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
@ -45,8 +47,8 @@ async def to_code(config):
|
|||||||
pin = await gpio_pin_expression(config[CONF_FLOW_CONTROL_PIN])
|
pin = await gpio_pin_expression(config[CONF_FLOW_CONTROL_PIN])
|
||||||
cg.add(var.set_flow_control_pin(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):
|
def modbus_device_schema(default_address):
|
||||||
|
@ -102,8 +102,12 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
|
|||||||
uint16_t computed_crc = crc16(raw, data_offset + data_len);
|
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);
|
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) {
|
if (computed_crc != remote_crc) {
|
||||||
ESP_LOGW(TAG, "Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
|
if (this->disable_crc_) {
|
||||||
return false;
|
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);
|
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:");
|
ESP_LOGCONFIG(TAG, "Modbus:");
|
||||||
LOG_PIN(" Flow Control Pin: ", this->flow_control_pin_);
|
LOG_PIN(" Flow Control Pin: ", this->flow_control_pin_);
|
||||||
ESP_LOGCONFIG(TAG, " Send Wait Time: %d ms", this->send_wait_time_);
|
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 {
|
float Modbus::get_setup_priority() const {
|
||||||
// After UART bus
|
// After UART bus
|
||||||
|
@ -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; }
|
void set_flow_control_pin(GPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
|
||||||
uint8_t waiting_for_response{0};
|
uint8_t waiting_for_response{0};
|
||||||
void set_send_wait_time(uint16_t time_in_ms) { send_wait_time_ = time_in_ms; }
|
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:
|
protected:
|
||||||
GPIOPin *flow_control_pin_{nullptr};
|
GPIOPin *flow_control_pin_{nullptr};
|
||||||
|
|
||||||
bool parse_modbus_byte_(uint8_t byte);
|
bool parse_modbus_byte_(uint8_t byte);
|
||||||
uint16_t send_wait_time_{250};
|
uint16_t send_wait_time_{250};
|
||||||
|
bool disable_crc_;
|
||||||
std::vector<uint8_t> rx_buffer_;
|
std::vector<uint8_t> rx_buffer_;
|
||||||
uint32_t last_modbus_byte_{0};
|
uint32_t last_modbus_byte_{0};
|
||||||
uint32_t last_send_{0};
|
uint32_t last_send_{0};
|
||||||
|
@ -175,6 +175,7 @@ CONF_DIO_PIN = "dio_pin"
|
|||||||
CONF_DIR_PIN = "dir_pin"
|
CONF_DIR_PIN = "dir_pin"
|
||||||
CONF_DIRECTION = "direction"
|
CONF_DIRECTION = "direction"
|
||||||
CONF_DIRECTION_OUTPUT = "direction_output"
|
CONF_DIRECTION_OUTPUT = "direction_output"
|
||||||
|
CONF_DISABLE_CRC = "disable_crc"
|
||||||
CONF_DISABLED_BY_DEFAULT = "disabled_by_default"
|
CONF_DISABLED_BY_DEFAULT = "disabled_by_default"
|
||||||
CONF_DISCONNECT_DELAY = "disconnect_delay"
|
CONF_DISCONNECT_DELAY = "disconnect_delay"
|
||||||
CONF_DISCOVERY = "discovery"
|
CONF_DISCOVERY = "discovery"
|
||||||
|
Loading…
Reference in New Issue
Block a user