diff --git a/API_changes.rst b/API_changes.rst index 4dc9bccef..6e3ade98e 100644 --- a/API_changes.rst +++ b/API_changes.rst @@ -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 diff --git a/pymodbus/payload.py b/pymodbus/payload.py index 00439cbc1..776f91e3f 100644 --- a/pymodbus/payload.py +++ b/pymodbus/payload.py @@ -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.""" @@ -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. diff --git a/test/test_payload.py b/test/test_payload.py index 140a00ee6..5f411f903 100644 --- a/test/test_payload.py +++ b/test/test_payload.py @@ -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""" @@ -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""" @@ -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): @@ -166,7 +166,7 @@ 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 @@ -174,7 +174,7 @@ def test_payload_builder_with_raw_payload(self): 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()