Skip to content

Commit

Permalink
1. #138 Add support to manipulate wordorder with BinaryPayloadDecoder…
Browse files Browse the repository at this point in the history
… and BinaryPayloadBuilder

2. #138 Updated tests and examples (#190)
3. #209 Bump version to 1.4.0
  • Loading branch information
dhoomakethu committed Jan 3, 2018
1 parent d4dcdc0 commit b4f3987
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 196 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Version 1.4.0
* Bug fix serial servers with Modbus Binary Framer
* Bug fix header size for ModbusBinaryFramer
* Bug fix payload decoder with endian Little
* Payload builder and decoder can now deal with the wordorder as well of 32/64 bit data.
* Support Database slave contexts (SqlStore and RedisStore)
* Custom handlers could be passed to Modbus TCP servers
* Asynchronous Server could now be stopped when running on a seperate thread (StopServer)
Expand Down
50 changes: 44 additions & 6 deletions examples/common/modbus_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,47 @@ def run_binary_payload_ex():
# - another 16 bit unsigned int 0x5678
# - an 8 bit int 0x12
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
# - an 32 bit uint 0x12345678
# - an 32 bit signed int -0x1234
# - an 64 bit signed int 0x12345678

# The packing can also be applied to the word (wordorder) and bytes in each
# word (byteorder)

# The wordorder is applicable only for 32 and 64 bit values
# Lets say we need to write a value 0x12345678 to a 32 bit register

# The following combinations could be used to write the register

# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# Word Order - Big Byte Order - Big
# word1 =0x1234 word2 = 0x5678

# Word Order - Big Byte Order - Little
# word1 =0x3412 word2 = 0x7856

# Word Order - Little Byte Order - Big
# word1 = 0x5678 word2 = 0x1234

# Word Order - Little Byte Order - Little
# word1 =0x7856 word2 = 0x3412
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #

# ----------------------------------------------------------------------- #
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder = BinaryPayloadBuilder(byteorder=Endian.Little,
wordorder=Endian.Big)
builder.add_string('abcdefgh')
builder.add_32bit_float(22.34)
builder.add_16bit_uint(0x1234)
builder.add_16bit_uint(0x5678)
builder.add_8bit_int(0x12)
builder.add_bits([0,1,0,1,1,0,1,0])
builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0])
builder.add_32bit_uint(0x12345678)
builder.add_32bit_int(-0x1234)
builder.add_64bit_int(0x1234567890ABCDEF)
payload = builder.build()
address = 0
result = client.write_registers(address, payload, skip_encode=True, unit=1)

client.write_registers(address, payload, skip_encode=True, unit=1)
# ----------------------------------------------------------------------- #
# If you need to decode a collection of registers in a weird layout, the
# payload decoder can help you as well.
Expand All @@ -69,15 +98,24 @@ def run_binary_payload_ex():
address = 0x00
count = len(payload)
result = client.read_holding_registers(address, count, unit=1)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
endian=Endian.Little)
print("-" * 60)
print("Registers")
print("-" * 60)
print(result.registers)
print("\n")
decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
byteorder=Endian.Little,
wordorder=Endian.Big)
decoded = {
'string': decoder.decode_string(8),
'float': decoder.decode_32bit_float(),
'16uint': decoder.decode_16bit_uint(),
'ignored': decoder.skip_bytes(2),
'8int': decoder.decode_8bit_int(),
'bits': decoder.decode_bits(),
"32uints": decoder.decode_32bit_uint(),
"32ints": decoder.decode_32bit_int(),
"64ints": decoder.decode_64bit_int(),
}

print("-" * 60)
Expand Down
2 changes: 1 addition & 1 deletion examples/common/modbus_payload_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def run_payload_server():
# ----------------------------------------------------------------------- #
# build your payload
# ----------------------------------------------------------------------- #
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder = BinaryPayloadBuilder(byteorder=Endian.Little)
# builder.add_string('abcdefgh')
# builder.add_32bit_float(22.34)
# builder.add_16bit_uint(4660)
Expand Down
6 changes: 3 additions & 3 deletions examples/contrib/sunspec_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ class SunspecDecoder(BinaryPayloadDecoder):
binary format.
"""

def __init__(self, payload, endian):
def __init__(self, payload, byteorder):
""" Initialize a new instance of the SunspecDecoder
.. note:: This is always set to big endian byte order
as specified in the protocol.
"""
endian = Endian.Big
BinaryPayloadDecoder.__init__(self, payload, endian)
byteorder = Endian.Big
BinaryPayloadDecoder.__init__(self, payload, byteorder)

def decode_string(self, size=1):
""" Decodes a string from the buffer
Expand Down
Loading

0 comments on commit b4f3987

Please sign in to comment.