How to read byte order endian BIG with Python?

81 views Asked by At

Hello I try to read data from power meter using pymodbus. After I register the address, I get list of bytes like this result_ups.registers = [3468, 204, 0, 2080] I try to convert it to same value as show on power meter like this 20434682080 This is my project for monitoring system.

This is my code

from pymodbus.client import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
import time


client_tcp = ModbusTcpClient('10.123.133.100',port=502)

while True:
    try:
        client_tcp.connect()
        while True:
            result_ups = client_tcp.read_holding_registers(1716,4,units=1,slave=1)
            decoder_ups = BinaryPayloadDecoder.fromRegisters(result_ups.registers,byteorder=Endian.BIG,wordorder=Endian.LITTLE)
            kwh_ups = decoder_ups.decode_32bit_int()
            print(kwh_ups)
            time.sleep(1)
         
    except Exception as e:
        print(e)
        time.sleep(1)
1

There are 1 answers

2
Barmar On

Just multiply the numbers by the appropriate powers of 10 to combine them as you show.

registers = [3468, 204, 0, 2080]
result = registers[3] + registers[0] * 10**4 + registers[1] * 10**8 + registers[2] * 10**12
print(result)

I'm not really sure why the results are in this order, it's not little-endian or big-endian.