Test for string16 added
This commit is contained in:
parent
5db75db8cb
commit
ba8aeeb3f3
2 changed files with 17 additions and 4 deletions
|
@ -7,6 +7,7 @@ from pymodbus.constants import Endian
|
||||||
from pymodbus.payload import BinaryPayloadDecoder, BinaryPayloadBuilder
|
from pymodbus.payload import BinaryPayloadDecoder, BinaryPayloadBuilder
|
||||||
from pymodbus.client.base import ModbusBaseClient
|
from pymodbus.client.base import ModbusBaseClient
|
||||||
|
|
||||||
|
|
||||||
class DataType:
|
class DataType:
|
||||||
def __init__(self, width, decode, add):
|
def __init__(self, width, decode, add):
|
||||||
width: int
|
width: int
|
||||||
|
@ -38,6 +39,7 @@ def decode_string4(decoder) -> str:
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
return decoder.decode_string(4)
|
return decoder.decode_string(4)
|
||||||
|
|
||||||
|
|
||||||
def decode_string8(decoder) -> str:
|
def decode_string8(decoder) -> str:
|
||||||
try:
|
try:
|
||||||
return str(decoder.decode_string(8).decode("utf-8"))
|
return str(decoder.decode_string(8).decode("utf-8"))
|
||||||
|
@ -150,7 +152,8 @@ class ScaledFroniusReg:
|
||||||
modbus_client
|
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(
|
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),
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,5 +14,15 @@ class TestDataTypes(unittest.TestCase):
|
||||||
output = froniusreg.int16.decode_from_register(int16_register)
|
output = froniusreg.int16.decode_from_register(int16_register)
|
||||||
assert output == 0
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in a new issue