From ba8aeeb3f371cf656121bb10a49285110de8ebdf Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Wed, 16 Oct 2024 20:48:59 +1100 Subject: [PATCH] Test for string16 added --- src/pyfroniusreg/froniusreg.py | 11 +++++++---- tests/test_datatype.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/pyfroniusreg/froniusreg.py b/src/pyfroniusreg/froniusreg.py index ee1e9f5..2b84152 100644 --- a/src/pyfroniusreg/froniusreg.py +++ b/src/pyfroniusreg/froniusreg.py @@ -5,12 +5,13 @@ from pymodbus.constants import Endian from pymodbus.payload import BinaryPayloadDecoder, BinaryPayloadBuilder -from pymodbus.client.base import ModbusBaseClient +from pymodbus.client.base import ModbusBaseClient + class DataType: def __init__(self, width, decode, add): width: int - + self._width = width self._decode = decode self._add = add @@ -38,6 +39,7 @@ def decode_string4(decoder) -> str: except UnicodeDecodeError: return decoder.decode_string(4) + def decode_string8(decoder) -> str: try: return str(decoder.decode_string(8).decode("utf-8")) @@ -150,7 +152,8 @@ class ScaledFroniusReg: modbus_client ) - def set(self, modbus_client: ModbusBaseClient, value: [ int, float, str ]): + def set(self, modbus_client: ModbusBaseClient, value: [int, float, str]): return self.value_register.set( - modbus_client, value / 10 ** self.scale_register.get(modbus_client), + modbus_client, + value / 10 ** self.scale_register.get(modbus_client), ) diff --git a/tests/test_datatype.py b/tests/test_datatype.py index a42b037..0644248 100644 --- a/tests/test_datatype.py +++ b/tests/test_datatype.py @@ -14,5 +14,15 @@ class TestDataTypes(unittest.TestCase): output = froniusreg.int16.decode_from_register(int16_register) assert output == 0 + def test_string16(self): + string_buffer = froniusreg.string16.encode_to_buffer("Fronius") + assert string_buffer == [b"Fr", b"on", b"iu", b"s\x00"] + + def test_string_16_decode(self): + int_register = [18034, 28526, 26997, 29440, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + output = froniusreg.string16.decode_from_register(int_register) + expectation = "Fronius" + 9 * '\x00' + assert output == expectation + if __name__ == "__main__": unittest.main()