Skip to content

Commit

Permalink
BinaryPayloadBuilder.to_string to BinaryPayloadBuilder.encode (#1526
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jamesbraza authored Apr 25, 2023
1 parent b28202a commit d8f2675
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions API_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Version 3.3.0
- Clients have an optional parameter: on_reconnect_callback, Function that will be called just before a reconnection attempt.
- general parameter unit= -> slave=
- move SqlSlaveContext, RedisSlaveContext to examples/contrib (due to lack of maintenance)
- :code:`BinaryPayloadBuilder.to_string` was renamed to :code:`BinaryPayloadBuilder.encode`

-------------
Version 3.2.0
Expand Down
19 changes: 8 additions & 11 deletions pymodbus/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,16 @@ def _pack_words(self, fstring, value):

return payload

def to_string(self):
"""Return the payload buffer as a string.
:returns: The payload buffer as a string
"""
def encode(self) -> bytes:
"""Get the payload buffer encoded in bytes."""
return b"".join(self._payload)

def __str__(self):
def __str__(self) -> str:
"""Return the payload buffer as a string.
:returns: The payload buffer as a string
"""
return self.to_string().decode("utf-8")
return self.encode().decode("utf-8")

def reset(self):
"""Reset the payload buffer."""
Expand Down Expand Up @@ -131,10 +128,10 @@ def build(self):
:returns: The payload buffer as a list
"""
string = self.to_string()
length = len(string)
string += b"\x00" * (length % 2)
return [string[i : i + 2] for i in range(0, length, 2)]
buffer = self.encode()
length = len(buffer)
buffer += b"\x00" * (length % 2)
return [buffer[i : i + 2] for i in range(0, length, 2)]

def add_bits(self, values):
"""Add a collection of bits to be encoded.
Expand Down
12 changes: 6 additions & 6 deletions test/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_little_endian_payload_builder(self):
builder.add_16bit_uint(1) # placeholder
builder.add_string(b"test")
builder.add_bits(self.bitstring)
assert self.little_endian_payload == builder.to_string()
assert self.little_endian_payload == builder.encode()

def test_big_endian_payload_builder(self):
"""Test basic bit message encoding/decoding"""
Expand All @@ -77,7 +77,7 @@ def test_big_endian_payload_builder(self):
builder.add_16bit_uint(1) # placeholder
builder.add_string("test")
builder.add_bits(self.bitstring)
assert self.big_endian_payload == builder.to_string()
assert self.big_endian_payload == builder.encode()

def test_payload_builder_reset(self):
"""Test basic bit message encoding/decoding"""
Expand All @@ -86,10 +86,10 @@ def test_payload_builder_reset(self):
builder.add_8bit_uint(0x34)
builder.add_8bit_uint(0x56)
builder.add_8bit_uint(0x78)
assert builder.to_string() == b"\x12\x34\x56\x78"
assert builder.encode() == b"\x12\x34\x56\x78"
assert builder.build() == [b"\x12\x34", b"\x56\x78"]
builder.reset()
assert not builder.to_string()
assert not builder.encode()
assert not builder.build()

def test_payload_builder_with_raw_payload(self):
Expand Down Expand Up @@ -166,15 +166,15 @@ def test_payload_builder_with_raw_payload(self):
builder = BinaryPayloadBuilder(
[b"\x12", b"\x34", b"\x56", b"\x78"], repack=True
)
assert builder.to_string() == b"\x12\x34\x56\x78"
assert builder.encode() == b"\x12\x34\x56\x78"
assert builder.to_registers() == [13330, 30806]
coils = builder.to_coils()
assert _coils1 == coils

builder = BinaryPayloadBuilder(
[b"\x12", b"\x34", b"\x56", b"\x78"], byteorder=Endian.Big
)
assert builder.to_string() == b"\x12\x34\x56\x78"
assert builder.encode() == b"\x12\x34\x56\x78"
assert builder.to_registers() == [4660, 22136]
assert str(builder) == "\x12\x34\x56\x78"
coils = builder.to_coils()
Expand Down

0 comments on commit d8f2675

Please sign in to comment.