Test for string16 added

This commit is contained in:
Paul Warren 2024-10-16 20:48:59 +11:00
parent 5db75db8cb
commit ba8aeeb3f3
2 changed files with 17 additions and 4 deletions

View file

@ -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),
)

View file

@ -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()