This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Modbus int16 negative numbers conversion
#1
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?
Reply
#2
"datatype:" field has a typo, it should be "datatype"
Reply
#3
Thanks a lot! I spent many hours solving this issue and I'm just blind...
Reply


Forum Jump: