tests actually working

This commit is contained in:
Paul Warren 2024-10-11 08:47:23 +11:00
parent 4b65df2949
commit 9ae239bc18
2 changed files with 6 additions and 10 deletions

View file

@ -41,7 +41,7 @@ class DataType(Enum):
elif (self == DataType.Int32) or (self == DataType.UInt32) or (self == DataType.Float32):
return int(2)
def decode(self, value):
def decode_from_register(self, value):
decoder = BinaryPayloadDecoder.fromRegisters(value.registers, byteorder=Endian.BIG, wordorder=Endian.BIG)
if (self == DataType.String8) or (self == DataType.String16) or (self == DataType.String32):
@ -65,7 +65,7 @@ class DataType(Enum):
else:
return str(decoder.decode_bits())
def encode(self, value):
def encode_to_buffer(self, value):
encoder = BinaryPayloadBuilder(byteorder=Endian.BIG, wordorder=Endian.BIG)
if (self == DataType.String8) or (self == DataType.String16) or (self == DataType.String32):
@ -129,11 +129,11 @@ class FroniusReg:
raise registerReadError("Unable to read from Fronius Register: %d, %s" % (self.id, self.description))
if(modbusValue is None):
raise registerReadError("It's NONE!")
return self.datatype.decode(modbusValue)
return self.datatype.decode_from_register(modbusValue)
def __setRegisterValue(self, modbusClient, value):
modbusValue = modbusClient.write_registers(self.address-1,
self.datatype.encode(value),
self.datatype.encode_to_buffer(value),
count=self.datatype.getRegisterLength(),
slave=self.unit,
skip_encode=True)

View file

@ -3,15 +3,11 @@ import unittest
import pyfroniusreg.froniusreg as froniusreg
class RegisterValue():
def __init__(self, value):
self.registers = value
class TestDataTypes(unittest.TestCase):
def test_int16(self):
value = RegisterValue(froniusreg.int16.encode(1024)[0])
assert froniusreg.int16.decode(value) == 1024
int16_buffer = froniusreg.int16.encode_to_buffer(1024)
assert int16_buffer == [b'\x04\x00']
if __name__ == '__main__':