Logic Machine Forum
Modbus connect with Victron Venus GX controller - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Modbus connect with Victron Venus GX controller (/showthread.php?tid=2087)



Modbus connect with Victron Venus GX controller - mooselight - 21.05.2019

Good morning !
I'm trying to read battery State Of Charge from a Victron Venus GX controller with that LUA script :

Code:
require('luamodbus')
mb = luamodbus.tcp()
mb:open('192.168.1.149', 502)
mb:connect()
r1 = mb:readregisters(843)
log (r1)
mb:close()

I think the connection is not working, because I get the following message in log :
Venus GX 21.05.2019 09:56:05
* nil

Modbus TCP communication of the Venux GX is working well from my Mac with that Python script :

Code:
#!/usr/bin/env python
from pymodbus.constants import Defaults
from pymodbus.constants import Endian
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.payload import BinaryPayloadDecoder

Defaults.Timeout = 25
Defaults.Retries = 5
client = ModbusClient('192.168.1.149', port='502')
result = client.read_input_registers(843,2)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big)
r1=decoder.decode_16bit_uint()
print("Battery SOC : %d" % (r1))

It's my first time with Modbus TCP on LogicMachine .. so I should have miss something ..
Any help is welcome !
Thanks 

PS/ Here are the Modbus register adresses and types from Victron documentation :

[Image: modbus.jpg]


RE: Modbus connect with Victron Venus GX controller - Daniel - 21.05.2019

Hi
Why didn't you try to create profile and use modbus mapper? Here are some examples. https://forum.logicmachine.net/showthread.php?tid=839
BR


RE: Modbus connect with Victron Venus GX controller - mooselight - 21.05.2019

Creating a profile seems too complicated to me .. but I'll take a look if I got time.
For now, my simple script generate the following error on the Venus GS log :

2019-05-21 09:03:09.810056500 ERROR 2019-05-21T09:03:09.809 "Error processing function code 3, unit id 255, src 192.168.1.100, start address 843, quantity 2 :" "Unit id not found"

I don't see where the set a different unit id .. and which one ?


RE: Modbus connect with Victron Venus GX controller - admin - 21.05.2019

255 is default slave ID for TCP. Most TCP devices do not care about slave ID. You can set it manually by calling mb:setslave(123) after connect, change 123 as needed.


RE: Modbus connect with Victron Venus GX controller - Daniel - 21.05.2019

And try using r1 = mb:readinputregisters(843)


RE: Modbus connect with Victron Venus GX controller - mooselight - 21.05.2019

Yes ! it works now with slave ID set to 0 :


Code:
require('luamodbus')
mb = luamodbus.tcp()
mb:open('192.168.1.149', 502)
mb:connect()
mb:setslave(0)
r1 = mb:readregisters(843)
r2 = mb:readregisters(808)
r3 = mb:readregisters(850)
log (r1, r2, r3)
mb:close()

Venus GX 21.05.2019 16:10:46
* arg: 1
 * number: 95
* arg: 2
 * number: 265
* arg: 3
 * number: 149

So I'll now be able to process those data the way I want.
Thanks for you help !