diff --git a/esphome/components/modbus/modbus.cpp b/esphome/components/modbus/modbus.cpp index 19b5e8019e..0928b0f6f3 100644 --- a/esphome/components/modbus/modbus.cpp +++ b/esphome/components/modbus/modbus.cpp @@ -76,7 +76,12 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) { // installed, but wait, there is the CRC, and if we get a hit there is a good // chance that this is a complete message ... admittedly there is a small chance is // isn't but that is quite small given the purpose of the CRC in the first place - data_len = at; + + // Fewer than 2 bytes can't calc CRC + if (at < 2) + return true; + + data_len = at - 2; data_offset = 1; uint16_t computed_crc = crc16(raw, data_offset + data_len);