Skip to content

Commit 64cb66a

Browse files
committed
#255 Fix BinaryPayloadDecoder
1 parent c1e784a commit 64cb66a

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Version 1.4.0
44
* Check for slave unit id before processing the request for serial clients
55
* Bug fix serial servers with Modbus Binary Framer
66
* Bug fix header size for ModbusBinaryFramer
7+
* Bug fix payload decoder with endian Little
78
* Support Database slave contexts (SqlStore and RedisStore)
89
* Custom handlers could be passed to Modbus TCP servers
910
* Asynchronous Server could now be stopped when running on a seperate thread (StopServer)

examples/common/modbus_payload.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def run_binary_payload_ex():
2424
# ----------------------------------------------------------------------- #
2525
# We are going to use a simple client to send our requests
2626
# ----------------------------------------------------------------------- #
27-
client = ModbusClient('127.0.0.1', port=5020)
27+
client = ModbusClient('127.0.0.1', port=5440)
2828
client.connect()
2929

3030
# ----------------------------------------------------------------------- #
@@ -41,7 +41,7 @@ def run_binary_payload_ex():
4141
# - an 8 bit int 0x12
4242
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
4343
# ----------------------------------------------------------------------- #
44-
builder = BinaryPayloadBuilder(endian=Endian.Big)
44+
builder = BinaryPayloadBuilder(endian=Endian.Little)
4545
builder.add_string('abcdefgh')
4646
builder.add_32bit_float(22.34)
4747
builder.add_16bit_uint(0x1234)
@@ -67,10 +67,10 @@ def run_binary_payload_ex():
6767
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
6868
# ----------------------------------------------------------------------- #
6969
address = 0x00
70-
count = 8
70+
count = len(payload)
7171
result = client.read_holding_registers(address, count, unit=1)
7272
decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
73-
endian=Endian.Big)
73+
endian=Endian.Little)
7474
decoded = {
7575
'string': decoder.decode_string(8),
7676
'float': decoder.decode_32bit_float(),
@@ -84,7 +84,7 @@ def run_binary_payload_ex():
8484
print("Decoded Data")
8585
print("-" * 60)
8686
for name, value in iteritems(decoded):
87-
print("%s\t" % name, value)
87+
print("%s\t" % name, hex(value) if isinstance(value, int) else value)
8888

8989
# ----------------------------------------------------------------------- #
9090
# close the client

pymodbus/payload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ def fromRegisters(klass, registers, endian=Endian.Little):
216216
:param endian: The endianess of the payload
217217
:returns: An initialized PayloadDecoder
218218
'''
219-
if isinstance(registers, list): # repack into flat binary
220-
payload = b''.join(pack(endian + 'H', x) for x in registers)
219+
if isinstance(registers, list): # repack into flat binary
220+
payload = b''.join(pack('!H', x) for x in registers)
221221
return klass(payload, endian)
222222
raise ParameterException('Invalid collection of registers supplied')
223223

0 commit comments

Comments
 (0)