Exception class, adjust test script slightly

This commit is contained in:
Paul Warren 2024-10-07 14:44:12 +11:00
parent 24e1cdded1
commit 6f1b8927f2
2 changed files with 23 additions and 47 deletions

View file

@ -3,6 +3,16 @@
# Copyright 2024, Paul Warren <pwarren@pwarren.id.au> # Copyright 2024, Paul Warren <pwarren@pwarren.id.au>
# Licensed under AGPLv3, See LICENSE.md for terms # Licensed under AGPLv3, See LICENSE.md for terms
from enum import Enum
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.client.tcp import ModbusTcpClient as ModbusClient
#from pymodbus.diag_message import *
#from pymodbus.file_message import *
#from pymodbus.other_message import *
#from pymodbus.mei_message import *
class DataType(Enum): class DataType(Enum):
String8 = 1 String8 = 1
String16 = 2 String16 = 2
@ -32,7 +42,7 @@ class DataType(Enum):
return int(2) return int(2)
def decode(self, value): def decode(self, value):
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big, wordorder=Endian.Big) decoder = BinaryPayloadDecoder.fromRegisters(value.registers, byteorder=Endian.Big, wordorder=Endian.Big)
if (self == DataType.String8) or (self == DataType.String16) or (self == DataType.String32): if (self == DataType.String8) or (self == DataType.String16) or (self == DataType.String32):
return str(decoder.decode_string(16).decode('utf-8')) return str(decoder.decode_string(16).decode('utf-8'))
@ -55,6 +65,9 @@ class DataType(Enum):
else: else:
return str(decoder.decode_bits()) return str(decoder.decode_bits())
class registerReadError(Exception):
pass
class FroniusReg: class FroniusReg:
def __init__(self, address, datatype, unit, description): def __init__(self, address, datatype, unit, description):
self.address = address self.address = address
@ -68,12 +81,12 @@ class FroniusReg:
self.datatype, self.datatype,
self.unit) self.unit)
def __getRegisterValue(modbusClient, address, dataType, unit): def __getRegisterValue(self, modbusClient, address, dataType, unit):
modbusValue = modbusClient.read_holding_registers(address-1, modbusValue = modbusClient.read_holding_registers(address-1,
dataType.getRegisterLength(), dataType.getRegisterLength(),
slave=unit) slave=unit)
if(modbusValue.isError()): if(modbusValue.isError()):
raise registerReadError() raise registerReadError("Unable to read from Fronius Register: %d, %s" % (self.id, self.description))
return dataType.decode(modbusValue) return dataType.decode(modbusValue)

View file

@ -98,8 +98,6 @@ def main():
# [REGISTERADDRESS, DATATYPE, MODBUSUNITNUMBER] # [REGISTERADDRESS, DATATYPE, MODBUSUNITNUMBER]
# the MODBUSUNITNUMBER is commonly 1 for the inverter and a user defined value for the primary smart meter (common values are 200 or 240) # the MODBUSUNITNUMBER is commonly 1 for the inverter and a user defined value for the primary smart meter (common values are 200 or 240)
print ("Register Overview:")
reg_map = ( reg_map = (
[40092, DataType.Float32, 1], # Inverter AC Output in W [40092, DataType.Float32, 1], # Inverter AC Output in W
[40285, DataType.UInt16, 1], # PV String 1 Output in W (scaled) [40285, DataType.UInt16, 1], # PV String 1 Output in W (scaled)
@ -126,50 +124,15 @@ def main():
"PWSF": [40268, DataType.Int16, 1, "Power Scaling Factor"] "PWSF": [40268, DataType.Int16, 1, "Power Scaling Factor"]
} }
soc = getRegisterValue(modbusClient, num_avgs = 1
target_map["soc"][0], count = 0
target_map["soc"][1],
target_map["soc"][2])
socSF = getRegisterValue(modbusClient,
target_map["socSF"][0],
target_map["socSF"][1],
target_map["socSF"][2])
print(" SoC: " + str(soc))
print("SoCSF: " + str(socSF))
print(" SoC: %.2f %%" % (float(soc * 10 ** socSF)))
PWSF = getRegisterValue(modbusClient,
target_map["PWSF"][0],
target_map["PWSF"][1],
target_map["PWSF"][2])
print(" PWSF: " + str(PWSF))
to_bat = getRegisterValue(modbusClient,
target_map["to_bat"][0],
target_map["to_bat"][1],
target_map["to_bat"][2])
print("To Bat: " + str(to_bat))
print("To Bat: %.2f" % (to_bat * 10 ** (int(PWSF))))
from_bat = getRegisterValue(modbusClient,
target_map["from_bat"][0],
target_map["from_bat"][1],
target_map["from_bat"][2])
print("Fr Bat: " + str(from_bat * 10 ** (int(PWSF))))
num_avgs = 5
count = 1
while count < num_avgs: while count < num_avgs:
print("To AVG: " + str(get_avg_scaled(modbusClient, target_map["to_bat"], target_map["PWSF"]))) print("To AVG: %.2f" % get_avg_scaled(modbusClient, target_map["to_bat"], target_map["PWSF"]))
print("Fr AVG: " + str(get_avg_scaled(modbusClient, target_map["from_bat"], target_map["PWSF"]))) print("Fr AVG: %.2f" % get_avg_scaled(modbusClient, target_map["from_bat"], target_map["PWSF"]))
count += 1 count += 1
soc = get_avg_scaled(modbusClient, target_map["soc"], target_map['socSF'], points=2)
print(" SOC: %.2f%%" % soc)
modbusClient.close() modbusClient.close()