67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
from pymodbus.client import ModbusTcpClient
|
|
import pandas as pd
|
|
import struct
|
|
import time
|
|
|
|
# Excel-Datei mit den Input-Registerinformationen
|
|
excel_path = "data/ModBus TCPIP 1.17(1).xlsx"
|
|
xls = pd.ExcelFile(excel_path)
|
|
df_input_registers = xls.parse('04 Input Register')
|
|
|
|
# Relevante Spalten bereinigen
|
|
df_clean = df_input_registers[['MB Adresse', 'Variable', 'Beschreibung', 'Variabel Typ']].dropna()
|
|
df_clean['MB Adresse'] = df_clean['MB Adresse'].astype(int)
|
|
|
|
# Dictionary aus Excel erzeugen
|
|
registers = {
|
|
row['MB Adresse']: {
|
|
'desc': row['Beschreibung'],
|
|
'type': 'REAL' if row['Variabel Typ'] == 'REAL' else 'INT'
|
|
}
|
|
for _, row in df_clean.iterrows()
|
|
}
|
|
# Verbindung zur Wärmepumpe
|
|
ip = '10.0.0.10'
|
|
port = 502
|
|
unit_id = 1
|
|
|
|
# # Liste von interessanten Holding-Register-Adressen
|
|
# registers = {
|
|
# 210: {'desc': 'Puffertemperatur Kühlen in °C', 'type': 'REAL'},
|
|
# 300: {'desc': 'Außentemperatur in °C', 'type': 'REAL'},
|
|
# 170: {'desc': 'Puffertemperatur Heizung in °C', 'type': 'REAL'},
|
|
# 160: {'desc': 'Trinkwarmwasserspiecher 2 oben (Ein) in °C', 'type': 'REAL'},
|
|
# 161: {'desc': 'Trinkwarmwasserspiecher 2 unten (Aus) in °C', 'type': 'REAL'},
|
|
#}
|
|
|
|
client = ModbusTcpClient(ip, port=port)
|
|
|
|
try:
|
|
if not client.connect():
|
|
print("Verbindung zur Wärmepumpe fehlgeschlagen.")
|
|
exit(1)
|
|
print("Verbindung zur Wärmepumpe erfolgreich.")
|
|
|
|
while True:
|
|
print(f"\n--- Neue Abfrage --- {time.strftime('%Y-%m-%d %H:%M:%S')} ---")
|
|
for address, info in registers.items():
|
|
reg_type = info['type']
|
|
result = client.read_input_registers(address, count=2 if reg_type == 'REAL' else 1)#, unit=unit_id)
|
|
if result.isError():
|
|
print(f"Fehler beim Lesen von Adresse {address}: {result}")
|
|
continue
|
|
|
|
if reg_type == 'REAL':
|
|
value = result.registers[0] / 10.0
|
|
else:
|
|
value = result.registers[0]
|
|
|
|
print(f"Adresse {address} - {info['desc']}: {value}")
|
|
|
|
time.sleep(10)
|
|
|
|
except KeyboardInterrupt:
|
|
print("Beendet durch Benutzer (Ctrl+C).")
|
|
finally:
|
|
client.close()
|