Skip to content

Commit d8f2675

Browse files
authored
BinaryPayloadBuilder.to_string to BinaryPayloadBuilder.encode (#1526)
1 parent b28202a commit d8f2675

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

API_changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Version 3.3.0
88
- Clients have an optional parameter: on_reconnect_callback, Function that will be called just before a reconnection attempt.
99
- general parameter unit= -> slave=
1010
- move SqlSlaveContext, RedisSlaveContext to examples/contrib (due to lack of maintenance)
11+
- :code:`BinaryPayloadBuilder.to_string` was renamed to :code:`BinaryPayloadBuilder.encode`
1112

1213
-------------
1314
Version 3.2.0

pymodbus/payload.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,16 @@ def _pack_words(self, fstring, value):
8181

8282
return payload
8383

84-
def to_string(self):
85-
"""Return the payload buffer as a string.
86-
87-
:returns: The payload buffer as a string
88-
"""
84+
def encode(self) -> bytes:
85+
"""Get the payload buffer encoded in bytes."""
8986
return b"".join(self._payload)
9087

91-
def __str__(self):
88+
def __str__(self) -> str:
9289
"""Return the payload buffer as a string.
9390
9491
:returns: The payload buffer as a string
9592
"""
96-
return self.to_string().decode("utf-8")
93+
return self.encode().decode("utf-8")
9794

9895
def reset(self):
9996
"""Reset the payload buffer."""
@@ -131,10 +128,10 @@ def build(self):
131128
132129
:returns: The payload buffer as a list
133130
"""
134-
string = self.to_string()
135-
length = len(string)
136-
string += b"\x00" * (length % 2)
137-
return [string[i : i + 2] for i in range(0, length, 2)]
131+
buffer = self.encode()
132+
length = len(buffer)
133+
buffer += b"\x00" * (length % 2)
134+
return [buffer[i : i + 2] for i in range(0, length, 2)]
138135

139136
def add_bits(self, values):
140137
"""Add a collection of bits to be encoded.

test/test_payload.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_little_endian_payload_builder(self):
5959
builder.add_16bit_uint(1) # placeholder
6060
builder.add_string(b"test")
6161
builder.add_bits(self.bitstring)
62-
assert self.little_endian_payload == builder.to_string()
62+
assert self.little_endian_payload == builder.encode()
6363

6464
def test_big_endian_payload_builder(self):
6565
"""Test basic bit message encoding/decoding"""
@@ -77,7 +77,7 @@ def test_big_endian_payload_builder(self):
7777
builder.add_16bit_uint(1) # placeholder
7878
builder.add_string("test")
7979
builder.add_bits(self.bitstring)
80-
assert self.big_endian_payload == builder.to_string()
80+
assert self.big_endian_payload == builder.encode()
8181

8282
def test_payload_builder_reset(self):
8383
"""Test basic bit message encoding/decoding"""
@@ -86,10 +86,10 @@ def test_payload_builder_reset(self):
8686
builder.add_8bit_uint(0x34)
8787
builder.add_8bit_uint(0x56)
8888
builder.add_8bit_uint(0x78)
89-
assert builder.to_string() == b"\x12\x34\x56\x78"
89+
assert builder.encode() == b"\x12\x34\x56\x78"
9090
assert builder.build() == [b"\x12\x34", b"\x56\x78"]
9191
builder.reset()
92-
assert not builder.to_string()
92+
assert not builder.encode()
9393
assert not builder.build()
9494

9595
def test_payload_builder_with_raw_payload(self):
@@ -166,15 +166,15 @@ def test_payload_builder_with_raw_payload(self):
166166
builder = BinaryPayloadBuilder(
167167
[b"\x12", b"\x34", b"\x56", b"\x78"], repack=True
168168
)
169-
assert builder.to_string() == b"\x12\x34\x56\x78"
169+
assert builder.encode() == b"\x12\x34\x56\x78"
170170
assert builder.to_registers() == [13330, 30806]
171171
coils = builder.to_coils()
172172
assert _coils1 == coils
173173

174174
builder = BinaryPayloadBuilder(
175175
[b"\x12", b"\x34", b"\x56", b"\x78"], byteorder=Endian.Big
176176
)
177-
assert builder.to_string() == b"\x12\x34\x56\x78"
177+
assert builder.encode() == b"\x12\x34\x56\x78"
178178
assert builder.to_registers() == [4660, 22136]
179179
assert str(builder) == "\x12\x34\x56\x78"
180180
coils = builder.to_coils()

0 commit comments

Comments
 (0)