minimalmodbus read multiple register on same device

Question:

Is there a way where I can read a set of register on a device with the python library minimalmodbus? Or is it one by one I need to read_register like this?

instrument.read_register(0x0033,1)
Asked By: jvels

||

Answers:

Look at the minimalmodbus documentation for read_registers(). You shouldn’t need to change the functioncode argument.

Assuming you want the first one hundred and twenty-five registers, starting at register zero:

registers = instrument.read_registers(0, 125)

If you wanted to print those registers:

for idx in range(len(registers)):
    print(f"Register {idx:3d} : {registers[idx]:5d} ({hex(registers[idx])})")

This will output something like:

Register   0 :     0 (0x0)
Register   1 :     1 (0x1)
Register   2 :     2 (0x2)
Register   3 :     3 (0x3)
Register   4 :     4 (0x4)
…

EDIT: Looking at page 9 of the specification document, there are sixteen and thirty-two bit registers co-mingled. It will be easier to read them explicitly. Otherwise you’ll need to shift and combine the two sixteen-bit registers, which is annoying and minimalmodbus has functions to make it easier and less error prone for you.

E.g.

# 0000h 2 V L1-N INT32 Value weight: Volt*10
# read_long(registeraddress: int, functioncode: int = 3, signed: bool = False, byteorder: int = 0) → int
L1_N_voltage = instrument.read_long(0, signed=True) / 10

# 0028h 2 W sys INT32 Value weight: Watt*10
sys_wattage = instrument.read_long(0x28, signed=True) / 10

Note that read_long() doesn’t have number_of_decimals support, so you need to manually divide the reading by ten. For the current and power factor you’ll need to divide by 1,000.

Answered By: import random
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.