Search code examples
python64-bitbit-shiftmodbusmodbus-tcp

64 bit shifting of a modbus register in python


I am trying to get data from SMA cluster controller through a modbus protocol. The modbus specification is something like..

Addres:30517
Desc: Energy fed on all conductors.. ,
CNT(Word): 4(Number of assigned Modbus registers.) ,
Type: U64(i need to bit shift from 3 to 0?) ,
Format: FIX0( no decimal ) ,
Access: RO (Read only) ,

Now as i am getting the response in 4 different 16 bit registers and i am not sure about the bit shifting that if i am doing it correctly or not.. I am doing something like....

request = client.read_holding_registers(30513,4,unit=2)
result = request.registers
currentActivePowerH=(result[3]<<16) | result[2]
currentActivePowerL=(result[1]<<16) | result[0]
currentActivePower=(currentActivePowerH<<32) | currentActivePowerL

Also i am not sure about the sequence of bit shifting that i have to start from 3 to 0 or vice versa?

Here is the description about the register in the manuals. A Modbus register is 16 bits wide. For wider data items, connected Modbus registers are used and considered as data blocks. The number of connected Modbus registers is indicated in the assign-ment tables. The address of the first Modbus register in a data block is the start address of the data block.

Thanks in advance for your help..


Solution

  • pymodbus offers a rich set of utilities to deal with received data, in your case you can do

    from pymodbus.constants import Endian
    from pymodbus.payload import BinaryPayloadDecoder
     # client intialization
    response = client.read_holding_registers(30513,4,unit=2)
    # Use the correct word and byte orders , below is for example purpose only
    decoder = BinaryPayloadDecoder.fromRegisters(response.registers, wordorder=Endian.Big, byteorder=Endain.Big)
    value = decoder.decode_64bit_uint()
    # or decoder.decode_64bit_int()
    # or decoder.decode_64bit_float()
    

    Refer example modbus_payload.py for both payload builder and decoder usage.