Fix offset bug in modbus text sensor. (#3006)

This commit is contained in:
stegm 2022-01-06 16:40:22 +01:00 committed by GitHub
parent e62c3e00c1
commit 84a830195f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,18 +14,18 @@ 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 max_items = this->response_bytes;
uint8_t index = this->offset;
char buffer[4]; char buffer[4];
bool add_comma = false; while ((max_items != 0) && index < data.size()) {
for (auto b : data) { uint8_t b = data[index];
switch (this->encode_) { switch (this->encode_) {
case RawEncoding::HEXBYTES: case RawEncoding::HEXBYTES:
sprintf(buffer, "%02x", b); sprintf(buffer, "%02x", b);
output << buffer; output << buffer;
break; break;
case RawEncoding::COMMA: case RawEncoding::COMMA:
sprintf(buffer, add_comma ? ",%d" : "%d", b); sprintf(buffer, index != this->offset ? ",%d" : "%d", b);
output << buffer; output << buffer;
add_comma = true;
break; break;
// Anything else no encoding // Anything else no encoding
case RawEncoding::NONE: case RawEncoding::NONE:
@ -33,9 +33,8 @@ void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
output << (char) b; output << (char) b;
break; break;
} }
if (--max_items == 0) {
break; index++;
}
} }
auto result = output.str(); auto result = output.str();