Reputation: 1
I am reading some values from a machine with pymodbusTCP library, but i have wrong values when i read floats. The function to get the values is:
def ler_dado(endereco,tipo):
if tipo == "float":
valor = modbus.read_holding_registers(endereco,1*2)
return [utils.decode_ieee(f) for f in utils.word_list_to_long(valor)][0]
if tipo == "int":
return modbus.read_holding_registers(endereco,1)[0]
The true value for example is 367 but read 366. What can be wrong?
Upvotes: 0
Views: 3560
Reputation: 45
Sounds like you have an Endian conversion wrong for your system. If you use the pymodbus.payload converter, you can change your endian conversions to get the right data.
from pymodbus.constants import Endian
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.payload import BinaryPayloadDecoder
UNIT = 0x01
client = ModbusTcpClient(config_modbus['ip'],config_modbus['port'])
client.connect()
result = client.read_holding_registers(entry['address'],entry['count'],unit=UNIT)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, Endian.Big, wordorder=Endian.Big)
value = decoder.decode_32bit_float()
Upvotes: 2