start of register writing

This commit is contained in:
Paul Warren 2024-10-08 07:36:19 +11:00
parent 5d56df7c0e
commit 10e03f927d
2 changed files with 25 additions and 14 deletions

View file

@ -76,20 +76,27 @@ class FroniusReg:
self.description=description
def getValue(self, modbusClient):
return self.__getRegisterValue(modbusClient,
self.address,
self.datatype,
self.unit)
return self.__getRegisterValue(modbusClient)
def __getRegisterValue(self, modbusClient, address, dataType, unit):
modbusValue = modbusClient.read_holding_registers(address-1,
dataType.getRegisterLength(),
slave=unit)
def setValue(self, modbusClient):
return self.__setRegisterValue(modbusClient)
def __getRegisterValue(self, modbusClient):
modbusValue = modbusClient.read_holding_registers(self.address-1,
self.datatype.getRegisterLength(),
slave=self.unit)
if(modbusValue.isError()):
raise registerReadError("Unable to read from Fronius Register: %d, %s" % (self.id, self.description))
if(modbusValue is None):
raise registerReadError("It's NONE!")
return dataType.decode(modbusValue)
return self.datatype.decode(modbusValue)
def __setRegisterValue(self, modbusClient):
modbusValue = modbusClient.write_holding_registers(self.address-1,
self.datatype.getRegisterLength(),
slave=self.unit)
return modbusValue
class ScaledFroniusReg:
def __init__(self, valueReg, scaleReg):
@ -99,7 +106,8 @@ class ScaledFroniusReg:
def getValue(self, modbusClient):
return self.valueReg.getValue(modbusClient) * 10 ** self.scaleReg.getValue(modbusClient)
def setValue(self, modbusClient):
return self.valueReg.setValue(modbusClient, self.scaleReg.getValue(modbusClient))
@ -108,3 +116,8 @@ class ScaledFroniusReg:
storageStateOfCharge = FroniusReg(40362, DataType.UInt16, 1, "Storage State of Charge")
storageStateOfChargeSF = FroniusReg(40376, DataType.Int16, 1, "Storage State of Charge Scaling Factor")
scaledStateOfCharge = ScaledFroniusReg(storageStateOfCharge, storageStateOfChargeSF)
OutWRte = FroniusReg(40367, DataType.Int16, 1, "DischargeRate, Negative means Charging")
WRteSF = FroniusReg(40379, DataType.Int16, 1, "ScalingFactor for storage Watts")
scaledDischarge = ScaledFroniusReg(OutWRte, WRteSF)

View file

@ -7,10 +7,8 @@ from pymodbus.client.tcp import ModbusTcpClient
fronius1 = ModbusTcpClient("172.19.107.211", port=502, timeout=10)
fronius1.connect()
raw_soc = FroniusReg.storageStateOfCharge.getValue(fronius1)
print(" raw: %s" % raw_soc)
soc = FroniusReg.scaledStateOfCharge.getValue(fronius1)
print(" SOC: %s%%" % soc)
discharge = FroniusReg.scaledDischarge.getValue(fronius1)
print(" Rate: %dW" % discharge)