fix modbus write for signed values

This commit is contained in:
MichaelDvP
2024-12-18 13:44:24 +01:00
parent 4b3205fc9c
commit c8822aff64

View File

@@ -1188,7 +1188,7 @@ void EMSdevice::setValueEnum(const void * value_p, const char * const ** options
if (dv.options != options && Mqtt::ha_enabled()) { if (dv.options != options && Mqtt::ha_enabled()) {
dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED); dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED);
} }
dv.options = options; dv.options = options;
dv.options_size = Helpers::count_items(options); dv.options_size = Helpers::count_items(options);
break; break;
} }
@@ -2136,7 +2136,12 @@ int EMSdevice::modbus_value_to_json(uint8_t tag, const std::string & shortname,
return -4; return -4;
} }
jsonValue["value"] = Helpers::numericoperator2scalefactor(dv.numeric_operator) * (float)((uint16_t)modbus_data[0] << 8 | (uint16_t)modbus_data[1]); if (dv.type == DeviceValueType::INT8 || dv.type == DeviceValueType::INT16) { // handle signed
jsonValue["value"] =
Helpers::numericoperator2scalefactor(dv.numeric_operator) * (float)(int16_t)((uint16_t)modbus_data[0] << 8 | (uint16_t)modbus_data[1]);
} else {
jsonValue["value"] = Helpers::numericoperator2scalefactor(dv.numeric_operator) * (float)((uint16_t)modbus_data[0] << 8 | (uint16_t)modbus_data[1]);
}
} else if (dv.type == DeviceValueType::UINT24 || dv.type == DeviceValueType::UINT32 || dv.type == DeviceValueType::TIME) { } else if (dv.type == DeviceValueType::UINT24 || dv.type == DeviceValueType::UINT32 || dv.type == DeviceValueType::TIME) {
// these data types are 2 16 bit register // these data types are 2 16 bit register
if (modbus_data.size() != 4) { if (modbus_data.size() != 4) {