Victron Energy Battery PYMODBUS error

Hi @borpin and @TrystanLea. Thanks for your help so far! I’ve been trying to debug. I don’t have Node-RED installed at the moment but I was thinking of setting up another Pi to try these things. Obviously it wouldn’t be able to connect to my Energy monitor as it wouldn’t have the comms board, but I can install Emonhub etc and then try fiddling with the code without the risk or breaking anything else!! (Been there got the scars!!).

However I was looking at the code and the error messages and this is where it is being generated…

# check if number of registers and number of names are the same
    if len(rNames) != len(registers):
        self._log.error("You have to define an equal number of registers and of names")
        return

I’m not 100% what this is trying to check for. Working backwards through the code rNames comes from …

# stores names
# fetch datacode or datacodes
if node in ehc.nodelist and 'rx' in ehc.nodelist[node]:
    rNames = ehc.nodelist[node]['rx']['names']

Looking in emonhub_coder.py which comes in as ehc we have this at the very start

import struct

# Initialize nodes data
# FIXME this shouldn't live here
nodelist = {}

Is this the issue??

One for @TrystanLea or @glyn.hudson I’m afraid.

Hello @BenWarwIck it may be that you just need a comma after the names value e.g:

names = batterysoc,

and

register = 400267

I think it may have loaded those as strings and compared the string lengths rather than array lengths…

Thanks for that @TrystanLea . I’ve tried that and it seems to have removed that error, but replaced it with another :neutral_face:
I’m now getting an error saying Connection failed on read of register: 40266
The registers I’ve been given by the supplier to try are…
40266
40267
40268
400266
400267
400268
However when I try the 400* ones I get an error about expecting a code less than 65365.
If I use an iPad ModbusTCP app, the Register number that is correct is 00266, but if I put that into the emonhub.conf I get an error!

Any thoughts??
Thanks!

Have you tried 4266 ?

No I haven’t tried that. I do wonder if 400266 is correct and the program I’m using to interrogate doesn’t need the 4 maybe? Not sure really, but happy to try 4266 and see what happens. Also going to fire up NodeRed and see if I can get any sense out of that and use it to feed Emonhub or use it to get the config on Emonhub correct!

In general, the first digit indicates either a holding register or an input register,
i.e. there’s no hard and fast “rule” regarding that WRT the slave device.

WRT the master device, it’s another matter altogether.

Have you tried using just the digits 266?

Funny you should say that, just tried 266 and same response. I suspect until I can see the message properly in NodeRed I won’t get an idea what the real issue is!

The odd thing is if I use the Mtcp iPad app then 00266 works and I get a response. Same IP address, port etc!

Have you tried “266” using that same method?

Trying to determine whether or not the 2 leading zeros are actually needed.

I mention it because none of the Modbus devices I have need leading zeros
in their addresses.

But… the software that’s iterrogating your device might need them.

That is the biggest clue.

Perhaps write your own mini Python script to access the MODBUS and see what happens.

note emonhub takes 1 off the register number to get the register address. Be careful you are not confusing the 2 (as I said above I think).

@BenWarwIck - Is the register a Holding or Input register?

From chatgpt with basic testing here, so may not be complete:

from pymodbus.client import ModbusTcpClient

# Modbus TCP server parameters
SERVER_IP = '192.168.1.10'
SERVER_PORT = 0

# Register addresses to read
REGISTERS = [40266, 40267, 40268, 400266, 400267, 400268]

# Connect to the Modbus TCP server
client = ModbusTcpClient(SERVER_IP, SERVER_PORT)
if not client.connect():
    print(f"Failed to connect to {SERVER_IP}:{SERVER_PORT}")
    exit(1)

# Read and print the values from the registers
for register in REGISTERS:
    result = client.read_holding_registers(register, 1)
    if result.isError():
        print(f"Failed to read register {register}")
    else:
        print(f"Register {register}: {result.registers[0]}")

# Close the connection
client.close()
1 Like

Register address or register number - they are not the same thing.

Assuming they are holding registers.

1 Like

Rewinding a little, out of interest what is the specific victron equipment that you are trying to read data from? do you have model numbers etc? Are you reading directly from the Victron equipment or via a 3rd party gateway of some kind? PureDrive?

I’ve been given access to the Vitron equipment in my PureDrive battery. I know the IP address and the port number and the register.

Not sure exactly the model number but think I’m querying the gateway which is Venus GX. Then I have a VE Bus System that is MultiPlus-II 48/3000/35-33 and a Battery Monitor that is Generic Can-bus BMS battery.

Not sure if any of that helps. Will try some of the other things later as I’m in work at the moment so don’t have access! :blush:

Ben

1 Like

Looking at this thread, there’s a mention of needing to set the slave address to be 0:
Modbus connect with Victron Venus GX controller

I cant find specific reference to slave address in the PyModbusTCP code but there is a unit_id:
pyModbusTCP/pyModbusTCP/client.py at master · sourceperl/pyModbusTCP · GitHub

It might be worth trying to replace:

client = ModbusTcpClient(SERVER_IP, SERVER_PORT)

with:

client = ModbusTcpClient(SERVER_IP, SERVER_PORT, 0)

There’s basic library usage details here as well: GitHub - sourceperl/pyModbusTCP: A simple Modbus/TCP library for Python

If that’s indeed the case, then the register number’s first digit should be a 3.

As mentioned earlier in the thread, in general the first digit indicates whether
the register is a holding register or input register. 4 indicates an input register,
3, a holding register.

A first digit of 4 would indicate an input register, and if emonHub is looking for
holding registers, is it ignoring addresses that have 4 as their first digit?

@TrystanLea ?

from: Modbus Functions (Schneider Electric, originally Modicon, the spec writer)