Skip to content

Commit

Permalink
Merge dc1d82c into 0560a42
Browse files Browse the repository at this point in the history
  • Loading branch information
rtu-dataframe committed Jan 14, 2019
2 parents 0560a42 + dc1d82c commit fb9335f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ The following functions have been implemented for Modbus TCP and Modbus RTU:
Other featues:

* Support for signed and unsigned register values.
* Helper methods (data_packer & data_unpacker) to keep simple the data read/write conversion

License
-------
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

from umodbus.utils import (log_to_stream, unpack_mbap, pack_mbap,
pack_exception_pdu,
get_function_code_from_request_pdu)
get_function_code_from_request_pdu,
_short_packer,
data_packer,
data_unpacker)


def test_log_to_stream():
Expand Down Expand Up @@ -44,3 +47,15 @@ def test_pack_exception_pdu():
def test_get_function_code_from_request_pdu():
""" Get correct function code from PDU. """
assert get_function_code_from_request_pdu(b'\x01\x00d\x00\x03') == 1

def test_get_short_packer():
""" Get correct function code from PDU. """
assert _short_packer(b'\x07[\xcd\x15') == [b'\x00\x07', b'\x00[', b'\x00\xcd', b'\x00\x15']

def test_data_packer():
""" Tests if the data_packer returns the correct data as specified in the format char and indianess """
assert data_packer(123456789, '>', 'l') == [b'\x00\x07', b'\x00[', b'\x00\xcd', b'\x00\x15']

def test_data_unpacker():
""" Tests if the data_unpacker returns the correct data as specified in the format char and indianess """
assert data_unpacker((1883, 52501), '>', 'l')[0] == 123456789
29 changes: 29 additions & 0 deletions umodbus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,32 @@ def recv_exactly(recv_fn, size):
raise ValueError

return response


def _short_packer(packed_data):
""" Pack the data to a list of short (int16, 2 bytes) """
packed_bytes = []
try:
for int16_chunk in packed_data:
# Iterate over all elements contained in the packed_data and build the short
# (int16, 2 bytes) packed_bytes list
# The ModBus standard register data dimension
packed_bytes.append(struct.pack('>H', int16_chunk))
except TypeError as ex:
packed_bytes.append(struct.pack('>H', 0))
finally:
return packed_bytes


def data_packer(value, indianess='>', data_type='H'):
""" Returns the data packed, ready to be written """
packed_data = struct.pack('{0}{1}'.format(indianess, data_type), value)
packed_bytes = _short_packer(packed_data)
return packed_bytes



def data_unpacker(data, indianess='>', data_type='H'):
""" Returns the unpacked data, as the format specified """
packed_bytes = _short_packer(data)
return struct.unpack('{0}{1}'.format(indianess, data_type), b''.join(packed_bytes))

0 comments on commit fb9335f

Please sign in to comment.