Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinaryPayloadBuilder.to_string to BinaryPayloadBuilder.encode #1526

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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