Logic Machine Forum
Modbus int16 negative numbers conversion - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: Modbus int16 negative numbers conversion (/showthread.php?tid=3939)



Modbus int16 negative numbers conversion - flajzi - 20.03.2022

Hello,

I'm running home solar system. I'm trying to read electricity consumption (positive number) or supplying electricity to grid (negative number) from registry over Modbus TCP connection. Victron manual says it is signed int16.
Positive numbers are shown correctly, but I have problems with negative numbers using profile in Modbus section.

When I repeately run script reading Victron registry, manually convert uint to int16, it's ok.
Code:
if not mb then
  require('luamodbus')
  mb = luamodbus.tcp()
  mb:open('10.76.215.10', 502)
  mb:connect()
end
mb:setslave(100)
value = mb:readinputregisters(820)
log(value)
value = lmcore.inttohex(value,2)
log(value)
value = knxdatatype.decode(value, dt.int16)
log(value)
grp.update('32/1/2', value)

Log correctly shows:
Code:
modbus1 20.03.2022 14:05:01
* number: 65498
modbus1 20.03.2022 14:05:01
* string: FFDA
modbus1 20.03.2022 14:05:01
* number: -38

Then I have created this profile:
Code:
{
  "manufacturer": "Victron Energy",
  "description": "Cerbo GX system",
  "mapping": [
    { "name": "Grid L1",
      "bus_datatype": "int16",
      "type": "inputregister",
      "address": 820,
      "datatype:": "int16",
      "units": " W" }
  ]
}

But using this profile when it reads negative number, it mostly shows 32767 (0x7FFFF) in object value.

I tried all read_swap options but with no success.

Does anynone has any idea, what is wrong with this profile?


RE: Modbus int16 negative numbers conversion - admin - 21.03.2022

"datatype:" field has a typo, it should be "datatype"


RE: Modbus int16 negative numbers conversion - flajzi - 21.03.2022

Thanks a lot! I spent many hours solving this issue and I'm just blind...