From c8822aff645a76e3da4f171058ca5bcd23e13de3 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Wed, 18 Dec 2024 13:44:24 +0100 Subject: [PATCH] fix modbus write for signed values --- src/emsdevice.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index e6c3e9ba1..648b85f92 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1188,7 +1188,7 @@ void EMSdevice::setValueEnum(const void * value_p, const char * const ** options if (dv.options != options && Mqtt::ha_enabled()) { dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED); } - dv.options = options; + dv.options = options; dv.options_size = Helpers::count_items(options); break; } @@ -2136,7 +2136,12 @@ int EMSdevice::modbus_value_to_json(uint8_t tag, const std::string & shortname, 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) { // these data types are 2 16 bit register if (modbus_data.size() != 4) {