fix: modbus_textsensor response is too long in some cases (#6333)

This commit is contained in:
NewoPL 2024-03-10 23:15:32 +01:00 committed by GitHub
parent 3e2ce363a2
commit 0cdd0b295e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,10 +13,10 @@ void ModbusTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Modbus Controller Te
void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) { void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
std::ostringstream output; std::ostringstream output;
uint8_t max_items = this->response_bytes; uint8_t items_left = this->response_bytes;
uint8_t index = this->offset; uint8_t index = this->offset;
char buffer[4]; char buffer[4];
while ((max_items != 0) && index < data.size()) { while ((items_left > 0) && index < data.size()) {
uint8_t b = data[index]; uint8_t b = data[index];
switch (this->encode_) { switch (this->encode_) {
case RawEncoding::HEXBYTES: case RawEncoding::HEXBYTES:
@ -33,7 +33,7 @@ void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
output << (char) b; output << (char) b;
break; break;
} }
items_left--;
index++; index++;
} }