简体   繁体   中英

Trouble reading holding Modbus registers with float32 data

#my code goes like this for a holding register 3026 - 3027

client = ModbusClient(method ='rtu', port ='COM4', timeout=1, stopbits = 1, bytes = 8, parity = 'E', baudrate = 9600)
a = client.connect()
print(a)

while True:
    read_value = client.read_holding_registers(address = 43026,count = 2,unit = 1)
    real_decoder = BinaryPayloadDecoder.fromRegisters(read_value.registers,byteorder = Endian.Little, wordorder=Endian.Big)
    value = real_decoder.decode_32bit_float()
    print(value)
    time.sleep(100)

The result I got is this: 1.1755122874426309e-38

but my ModScan32 shows a 156.9796 in float(MSRF) mode.

can anyone here gives me a solution on how this floating point data with 32bits may be converted into a value same of that ModScan32 result...

Hope someone might notice and help me.. :( Im sorry for being newbie..

You probably have a problem with endianness . The float(MSRF) format is Little Endian (MSRF stands for most significant register first ) but in your code you set wordorder=Endian.Big .

Try changing wordorder to Endian.Little

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM