简体   繁体   English

读取保存带有 float32 数据的 Modbus 寄存器时遇到问题

[英]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我得到的结果是:1.1755122874426309e-38

but my ModScan32 shows a 156.9796 in float(MSRF) mode.但我的 ModScan32 在浮点(MSRF)模式下显示 156.9796。

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...这里的任何人都可以给我一个解决方案,说明如何将这个 32 位的浮点数据转换为与 ModScan32 结果相同的值......

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 . float(MSRF)格式为 Little Endian (MSRF 代表最重要的寄存器优先),但在您的代码中设置wordorder=Endian.Big

Try changing wordorder to Endian.Little尝试将wordorder更改为Endian.Little

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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