Skip to content

Commit

Permalink
Add command line parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
steff393 committed Jun 3, 2023
1 parent 57f1d72 commit 0f82ec0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
54 changes: 26 additions & 28 deletions cfg.ini
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
[Configuration]
cfgInverterIp = 192.168.178.94
cfgInverterAddr = 1
Typ = test
IP = 192.168.178.94
Typ = Beispiel # SolarEdge | Fronius | Kostal_mit_KSEM | Huawei | SMA | Victron | E3DC | Kostal_Plenticore |

# -------------------------------------

[test]
[Beispiel]
# Beispiel: Lese 5 Register ab 30810, dann 1 Register aus 60820 und 1 Register aus 30822
Port = 502
Addr = 1
30810 = 5
60820 = 1
30822 = 1

[SolarEdge]
# "cfgInverterType":1
[SolarEdge] # "cfgInverterType":1
Port = 1502
Addr = 1
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
[Fronius] # "cfgInverterType":2
Port = 502
Addr = 240
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
[Kostal_mit_KSEM] # "cfgInverterType":3
Port = 502
Addr = 240
40087 = 1 # Total Real Power (sum of active phases)
40091 = 1 # AC Real Power Scale Factor

[Huawei]
# "cfgInverterType":4
[Huawei] # "cfgInverterType":4
Port = 502
37113 = 1 # Active power
37765 = 1 # [Energystorage]Charge/Discharge power (optional)
Addr = 1
37113 = 2 # Active power
37765 = 2 # [Energystorage]Charge/Discharge power (optional)

[SMA]
# "cfgInverterType":5
[SMA] # "cfgInverterType":5
Port = 502
30867 = 1 # Metering.GridMs.TotWOut
30865 = 1 # Metering.GridMs.TotWIn
Addr = 1
30867 = 2 # Metering.GridMs.TotWOut
30865 = 2 # Metering.GridMs.TotWIn

[Victron]
# "cfgInverterType":6
[Victron] # "cfgInverterType":6
Port = 502
Addr = 100
820 = 3 # com.victronenergy.system --> Grid L1 - Power

[E3DC]
# "cfgInverterType":7
[E3DC] # "cfgInverterType":7
Port = 502
40074 = 1 # Leistung am Netzübergabepunkt
Addr = 1
40074 = 2 # Leistung am Netzübergabepunkt

[Kostal_Plenticore]
# "cfgInverterType":8
[Kostal_Plenticore] # "cfgInverterType":8
Port = 1502
172 = 1 # Total AC active power
Addr = 71
172 = 2 # Total AC active power
36 changes: 24 additions & 12 deletions wbecModbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@
config = configparser.ConfigParser(inline_comment_prefixes='#')
config.read('cfg.ini')

cfg = config['Configuration']
typ = config['Configuration']['Typ']

for key in cfg:
print(key.ljust(18) + ": " + cfg[key])
print('Port'.ljust(18) + ": " + config[typ]['Port'])
if len(sys.argv) == 3:
print("Übernehme IP und Typ aus Aufrufparametern...")
modbusIP = sys.argv[1]
modbusTyp = sys.argv[2]
endEnter = False
else:
print("Übernehme IP und Typ aus cfg.ini...")
modbusIP = config['Configuration']['IP']
modbusTyp = config['Configuration']['Typ']
endEnter = True

print('IP : ' + modbusIP)
print('Typ : ' + modbusTyp)
print('Port : ' + config[modbusTyp]['Port'])
print('Addr : ' + config[modbusTyp]['Addr'])
print("...bitte warten...")

try:
client = ModbusTcpClient(cfg['cfgInverterIp'], port=config[typ]['Port'])
client = ModbusTcpClient(modbusIP, port=config[modbusTyp]['Port'])
client.connect()
print("Registerwerte:")
for key in config[typ]:
for key in config[modbusTyp]:
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)
continue # If key doesn't contains a register number, then skip it

response = client.read_holding_registers(int(key), int(config[modbusTyp][key]), slave=int(config[modbusTyp]['Addr']), unit=0x01)

# Check if reading was successful
if response.isError():
Expand All @@ -37,7 +46,10 @@
print(f"Register {int(key) + i}: {register_value}")

client.close()
print("Server " + modbusIP + ", Port " + config[modbusTyp]['Port'] + " erfolgreich ausgelesen")

except ConnectionException:
print("Fehler: Server " + cfg['cfgInverterIp'] + ", Port " + config[typ]['Port'] + " nicht erreichbar")
print("Fehler: Server " + modbusIP + ", Port " + config[modbusTyp]['Port'] + " nicht erreichbar")

if endEnter:
input("<Enter> zum Beenden...")

0 comments on commit 0f82ec0

Please sign in to comment.