From 57f1d72b3dd5042fa0ce857975240e5ce91fc455 Mon Sep 17 00:00:00 2001
From: steff393 <stefan.ferstl@gmail.com>
Date: Sat, 3 Jun 2023 14:28:17 +0200
Subject: [PATCH] Add individual port

---
 cfg.ini       | 64 ++++++++++++++++++++++++++++++++++++++++++++-------
 wbecModbus.py | 23 ++++++++++--------
 2 files changed, 70 insertions(+), 17 deletions(-)

diff --git a/cfg.ini b/cfg.ini
index 2d37d12..acf2df5 100644
--- a/cfg.ini
+++ b/cfg.ini
@@ -1,15 +1,63 @@
-[Section 1]
+[Configuration]
 cfgInverterIp   = 192.168.178.94
-cfgInverterPort = 502
 cfgInverterAddr = 1
-cfgInverterType = 1
+Typ = test
 
-[Section 2]
-register        = 40206
-registerCount   = 5
+# -------------------------------------
 
-[Register]
-# Beispiel: Lese 5 Register ab 30810
+[test]
+# Beispiel: Lese 5 Register ab 30810, dann 1 Register aus 60820 und 1 Register aus 30822
+Port  = 502
 30810 = 5
 60820 = 1
 30822 = 1
+
+[SolarEdge]
+# "cfgInverterType":1
+Port  = 1502
+40071 = 1 # I_AC_Current
+40083 = 2 # AC Power value + AC Power scale factor
+40206 = 1 # Total Real Power (sum of active phases)
+40210 = 1 # AC Real Power Scale Factor
+
+[Fronius]
+# "cfgInverterType":2
+# SlaveID=240
+Port  = 502
+40083 = 2 # AC Power value + AC Power scale factor
+40087 = 1 # Total Real Power (sum of active phases)
+40091 = 1 # AC Real Power Scale Factor
+
+[Kostal_mit_KSEM]
+# "cfgInverterType":3
+# SlaveID=240
+Port  = 502
+40087 = 1 # Total Real Power (sum of active phases)
+40091 = 1 # AC Real Power Scale Factor
+
+[Huawei]
+# "cfgInverterType":4
+Port  = 502
+37113 = 1 # Active power
+37765 = 1 # [Energystorage]Charge/Discharge power (optional)
+
+[SMA]
+# "cfgInverterType":5
+Port  = 502
+30867 = 1 # Metering.GridMs.TotWOut
+30865 = 1 # Metering.GridMs.TotWIn
+
+[Victron]
+# "cfgInverterType":6
+Port  = 502
+820   = 3 # com.victronenergy.system --> Grid L1 - Power
+
+[E3DC]
+# "cfgInverterType":7
+Port  = 502
+40074 = 1 # Leistung am Netzübergabepunkt
+
+[Kostal_Plenticore]
+# "cfgInverterType":8
+Port  = 1502
+172   = 1 # Total AC active power
diff --git a/wbecModbus.py b/wbecModbus.py
index d40cbb3..2c9f3f8 100644
--- a/wbecModbus.py
+++ b/wbecModbus.py
@@ -8,25 +8,30 @@
 
 print("wbec Modbus-TCP-Kompatibilitätsprüfung")
 
-config = configparser.ConfigParser()
+config = configparser.ConfigParser(inline_comment_prefixes='#')
 config.read('cfg.ini')
 
-cfg = config['Section 1']
+cfg = config['Configuration']
+typ = config['Configuration']['Typ']
 
-for key in config['Section 1']:
-	print(key.ljust(18) + ": " + config['Section 1'][key])
+for key in cfg:
+	print(key.ljust(18) + ": " + cfg[key])
+print('Port'.ljust(18) + ": " + config[typ]['Port'])
 print("...bitte warten...")
 
 try:
-	client = ModbusTcpClient(cfg['cfgInverterIp'], port=cfg['cfgInverterPort'])
+	client = ModbusTcpClient(cfg['cfgInverterIp'], port=config[typ]['Port'])
 	client.connect()
 	print("Registerwerte:")
-	for key in config['Register']:
-		response = client.read_holding_registers(int(key), int(config['Register'][key]), slave=int(cfg['cfgInverterAddr']), unit=0x01)
+	for key in config[typ]:
+		if not key.isdigit():
+			continue
+		# If key contains a register number, then read it
+		response = client.read_holding_registers(int(key), int(config[typ][key]), slave=int(cfg['cfgInverterAddr']), unit=0x01)
 
 		# Check if reading was successful
 		if response.isError():
-			print(f"Register {int(key) + i}: Fehler: ", response)
+			print(f"Register {int(key)}: Fehler: ", response)
 		else:
 			for i, register_value in enumerate(response.registers):
 				print(f"Register {int(key) + i}: {register_value}")
@@ -34,5 +39,5 @@
 	client.close()
 
 except ConnectionException:
-	print("Fehler: Server " + cfg['cfgInverterIp'] + ", Port " + cfg['cfgInverterPort'] + " nicht erreichbar")
+	print("Fehler: Server " + cfg['cfgInverterIp'] + ", Port " + config[typ]['Port'] + " nicht erreichbar")