BinaryPayloadDecoder.decode_string(n) does not take care of byteorder and wordorder #1630
-
On a Modbus Ethernet device, I have 'stri' coded on 2 registers. from pymodbus.client import ModbusTcpClient
from pymodbus.framer.socket_framer import ModbusSocketFramer
framer = ModbusSocketFramer
client = ModbusTcpClient(host='192.168.1.20', port=502, framer=framer, reconnect_delay=0)
client.connect()
response = client.read_holding_registers(address=12468, count=2)
client.close()
regs = response.registers regs contains [29811, 26994] I wrote a small test case: from pymodbus.payload import (BinaryPayloadDecoder, BinaryPayloadBuilder)
def show_as_hex(registers):
ret = ''
for value in registers:
ret += hex(value)[2:]
return ret
regs = [29811, 26994]
print(f"show registers in hex: {show_as_hex(regs)}\n")
decoder = BinaryPayloadDecoder.fromRegisters(regs, '>', '>')
iBB = decoder.decode_32bit_int()
decoder = BinaryPayloadDecoder.fromRegisters(regs, '<', '>')
iLB = decoder.decode_32bit_int()
decoder = BinaryPayloadDecoder.fromRegisters(regs, '>', '<')
iBL = decoder.decode_32bit_int()
decoder = BinaryPayloadDecoder.fromRegisters(regs, '<', '<')
iLL = decoder.decode_32bit_int()
decoder = BinaryPayloadDecoder.fromRegisters(regs, '>', '>')
sBB = decoder.decode_string(4)
decoder = BinaryPayloadDecoder.fromRegisters(regs, '<', '>')
sLB = decoder.decode_string(4)
decoder = BinaryPayloadDecoder.fromRegisters(regs, '>', '<')
sBL = decoder.decode_string(4)
decoder = BinaryPayloadDecoder.fromRegisters(regs, '<', '<')
sLL = decoder.decode_string(4)
print(f"** BB **\nint32: {hex(iBB)[2:]}\nstring: {sBB.hex()}\n")
print(f"** LB **\nint32: {hex(iLB)[2:]}\nstring: {sLB.hex()}\n")
print(f"** BL **\nint32: {hex(iBL)[2:]}\nstring: {sBL.hex()}\n")
print(f"** LL **\nint32: {hex(iLL)[2:]}\nstring: {sLL.hex()}\n") It returns:
The strings are all the same, the My problem is that |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
Did you read the documentation? you can specify the order you want. Please have a look at our examples too, they can show you how to use the methods we provide. |
Beta Was this translation helpful? Give feedback.
-
I think your suggestion is overly complicated, and in will cause conflict with the from_register call....all you need is the parameter convert: bool. |
Beta Was this translation helpful? Give feedback.
-
I am also having this issue. It seems to be happening with the BinaryPayloadBuilder as well as the BinaryPayloadDecoder. Here is some code you can run to test how the BinaryPayloadBuilder and BinaryPayloadDecoder handle endianness of strings:
When I run this, it returns:
Is this the correct behavior? |
Beta Was this translation helpful? Give feedback.
Did you read the documentation? you can specify the order you want.
Please have a look at our examples too, they can show you how to use the methods we provide.