inverter and meter seems to run

This commit is contained in:
Nils Reiners
2025-09-18 14:14:53 +02:00
parent 397935f51a
commit 0bcf8a2d8c
6 changed files with 296 additions and 68 deletions

View File

@@ -1,61 +1,128 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import struct
import pandas as pd
from typing import Dict, Any, List, Optional
from pymodbus.client import ModbusTcpClient
import struct, sys
MODBUS_IP = "192.168.1.112"
MODBUS_PORT = 502
UNIT_ID = 1
# ==== Nutzer-Parameter ====
IP = "192.168.1.112"
PORT = 502 # ggf. 1502
UNIT = 1
EXCEL_PATH = "modbus_registers/pv_inverter_registers.xlsx"
METER_START = 40240 # Startadresse Model 203-Felder
# Spaltennamen in deiner Excel
EXCEL_COLS = ["MB Adresse", "Beschreibung", "Variabel Typ"]
def to_i16(u16): # unsigned 16 → signed 16
# Adressfilter
MIN_ADDR = 40121 # nur ab hier
ADDRESS_SHIFT = 50 # +50 für Synergy 2-Unit
# =================== Modbus-Helfer ===================
def to_i16(u16: int) -> int:
return struct.unpack(">h", struct.pack(">H", u16))[0]
def read_regs(client, addr, count):
rr = client.read_holding_registers(address=addr, count=count, slave=UNIT_ID)
if rr.isError():
return None
return rr.registers
def f32_from_two(u16_hi: int, u16_lo: int, msw_first: bool = True) -> float:
if msw_first:
b = struct.pack(">HH", u16_hi, u16_lo)
else:
b = struct.pack(">HH", u16_lo, u16_hi)
return struct.unpack(">f", b)[0]
def read_meter_power(client):
base = METER_START
p = read_regs(client, base + 16, 1) # M_AC_Power
pa = read_regs(client, base + 17, 1) # Phase A
pb = read_regs(client, base + 18, 1) # Phase B
pc = read_regs(client, base + 19, 1) # Phase C
sf = read_regs(client, base + 20, 1) # Scale Factor
if not p or not sf:
return None
sff = to_i16(sf[0])
return {
"total": to_i16(p[0]) * (10 ** sff),
"A": to_i16(pa[0]) * (10 ** sff) if pa else None,
"B": to_i16(pb[0]) * (10 ** sff) if pb else None,
"C": to_i16(pc[0]) * (10 ** sff) if pc else None,
"sf": sff
}
def word_count_for_type(rtype: str) -> int:
rt = (rtype or "").strip().lower()
if "uint32" in rt or "real" in rt or "float" in rt or "string(32)" in rt:
return 2
return 1 # default: 16-bit
def fmt_w(v):
if v is None: return "-"
neg = v < 0
v = abs(v)
return f"{'-' if neg else ''}{v/1000:.2f} kW" if v >= 1000 else f"{'-' if neg else ''}{v:.0f} W"
class ModbusReader:
def __init__(self, ip: str, port: int, unit: int):
self.client = ModbusTcpClient(ip, port=port, timeout=3.0, retries=3)
if not self.client.connect():
print("❌ Verbindung zu Wechselrichter fehlgeschlagen.")
raise SystemExit(1)
self.unit = unit
print("✅ Verbindung zu Wechselrichter hergestellt.")
def close(self):
try:
self.client.close()
except Exception:
pass
def _try_read(self, fn_name: str, address: int, count: int) -> Optional[List[int]]:
fn = getattr(self.client, fn_name)
for kwargs in (dict(address=address, count=count, slave=self.unit),
dict(address=address, count=count)):
try:
res = fn(**kwargs)
if res is None or (hasattr(res, "isError") and res.isError()):
continue
return getattr(res, "registers", None)
except TypeError:
continue
return None
def read_any(self, address: int, count: int) -> Optional[List[int]]:
regs = self._try_read("read_holding_registers", address, count)
if regs is None:
regs = self._try_read("read_input_registers", address, count)
return regs
# =================== Hauptlogik ===================
def load_register_map(excel_path: str) -> Dict[int, Dict[str, Any]]:
xls = pd.ExcelFile(excel_path)
df = xls.parse()
df = df[EXCEL_COLS].dropna()
df["MB Adresse"] = df["MB Adresse"].astype(int)
regmap: Dict[int, Dict[str, Any]] = {}
for _, row in df.iterrows():
addr_excel = int(row["MB Adresse"])
if addr_excel < MIN_ADDR: # nur ab 40121
continue
desc = str(row["Beschreibung"]).strip()
rtype = str(row["Variabel Typ"]).strip()
regmap[addr_excel] = {"desc": desc, "type": rtype}
print(f" {len(regmap)} Register aus Excel geladen (>= {MIN_ADDR}).")
return regmap
def read_value(reader: ModbusReader, start_addr: int, rtype: str) -> Optional[float]:
words = word_count_for_type(rtype)
if words == 2:
regs = reader.read_any(start_addr, 2)
if not regs or len(regs) < 2:
return None
return f32_from_two(regs[0], regs[1])
else:
regs = reader.read_any(start_addr, 1)
if not regs:
return None
return float(to_i16(regs[0]))
def main():
client = ModbusTcpClient(MODBUS_IP, port=MODBUS_PORT)
if not client.connect():
print("❌ Verbindung fehlgeschlagen."); sys.exit(1)
regs = load_register_map(EXCEL_PATH)
reader = ModbusReader(IP, PORT, UNIT)
try:
m = read_meter_power(client)
if m:
print(f"Meter-Leistung: {fmt_w(m['total'])} "
f"(A {fmt_w(m['A'])}, B {fmt_w(m['B'])}, C {fmt_w(m['C'])}) [SF={m['sf']}]")
else:
print("Meter-Leistung konnte nicht gelesen werden.")
print(f"\n📋 Einmalige Auslesung ab {MIN_ADDR} (+{ADDRESS_SHIFT} Adressversatz) {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
for addr_excel, meta in sorted(regs.items()):
shifted_addr = addr_excel + ADDRESS_SHIFT
val = read_value(reader, shifted_addr, meta["type"])
if val is None:
continue
print(f"{addr_excel:5d}+{ADDRESS_SHIFT:2d}{shifted_addr:5d} | "
f"{meta['desc']:<40} | Wert: {val}")
finally:
client.close()
reader.close()
if __name__ == "__main__":
main()