major update

This commit is contained in:
2026-09-08 12:51:56 +02:00
parent dd9b69411e
commit f3c484c130
+36 -21
View File
@@ -10,8 +10,11 @@ import matplotlib.dates as mdates
AMS_NET_ID = "5.17.17.136.1.1"
PLC_PORT = 851 # Standard SPS-Port in TwinCAT 3
TEMP_SYMBOL = 'MAIN.temperature' # Pfad zur Variable in der SPS
LOG_FILENAME = "temperature_log.csv"
# SPS-Symbole
TEMP_SYMBOL = 'MAIN.temperature' # Typ: INT
BRAKE_SYMBOL = 'MAIN.brakeManualValue' # Typ: BOOL
LOG_FILENAME = "temperature_and_brake_log_underload.csv"
READ_INTERVAL = 1.0 # Ausleseintervall in Sekunden
# ==========================================
@@ -20,7 +23,7 @@ READ_INTERVAL = 1.0 # Ausleseintervall in Sekunden
try:
with open(LOG_FILENAME, 'a', encoding='utf-8') as f:
if f.tell() == 0:
f.write("Zeitstempel;Temperatur_C\n")
f.write("Zeitstempel;Temperatur_C;Bremse_Manuell\n")
except IOError as e:
print(f"Fehler beim Erstellen der Log-Datei: {e}")
@@ -32,13 +35,17 @@ timestamps = []
temp_values = []
line, = ax.plot([], [], 'r-o', linewidth=2, label="Temperatur (°C)")
ax.set_title("TwinCAT SPS Live-Temperaturüberwachung")
ax.set_title("TwinCAT SPS Überwachung: Temperatur & Bremse")
ax.set_xlabel("Uhrzeit")
ax.set_ylabel("Temperatur [°C]")
ax.grid(True)
ax.legend(loc="upper left")
# Datumsformatierung für die X-Achse auf HH:MM:SS festlegen
# Textanzeige für den aktuellen Bremsenstatus im Diagramm
status_text = ax.text(0.98, 0.92, '', transform=ax.transAxes,
ha='right', va='top', fontsize=11, fontweight='bold',
bbox=dict(boxstyle='round,pad=0.5', facecolor='white', alpha=0.8))
ax.legend(loc="upper left")
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
fig.autofmt_xdate()
@@ -51,11 +58,11 @@ def main():
try:
plc.open()
print(f"Verbindung zu TwinCAT PLC ({AMS_NET_ID}:{PLC_PORT}) erfolgreich hergestellt.")
print(f"Lese Symbol '{TEMP_SYMBOL}' (PLCTYPE_INT) aus...")
print(f"Lese Symbole:\n - '{TEMP_SYMBOL}' (INT)\n - '{BRAKE_SYMBOL}' (BOOL)...")
print("Schließe das Plot-Fenster oder drücke Strg+C zum Beenden.\n")
print(f"{'Zeitstempel':<20} | {'Temperatur (°C)':<15}")
print("-" * 38)
print(f"{'Zeitstempel':<20} | {'Temperatur (°C)':<15} | {'Bremse (0/1)':<15}")
print("-" * 56)
while plt.fignum_exists(fig.number):
loop_start = time.time()
@@ -63,32 +70,40 @@ def main():
time_str = now.strftime("%Y-%m-%d %H:%M:%S")
try:
# 1. Wert als INT aus der SPS lesen (2-Byte Datentyp)
# 1. Messwerte aus der SPS auslesen
raw_temp = plc.read_by_name(TEMP_SYMBOL, pyads.PLCTYPE_INT)
temp_val = float(raw_temp/10.0)
# Falls deine SPS Zehntel-Grad ausgibt (z.B. 215 = 21.5 °C), hier durch 10.0 teilen:
temp_val = float(raw_temp)
brake_val = plc.read_by_name(BRAKE_SYMBOL, pyads.PLCTYPE_BOOL)
brake_int = 1 if brake_val else 0 # Konvertierung Bool zu 0 / 1
# 2. In Log-Datei schreiben
# 2. In Log-Datei schreiben (speichert 0 oder 1)
with open(LOG_FILENAME, 'a', encoding='utf-8') as f:
f.write(f"{time_str};{temp_val:.2f}\n")
f.write(f"{time_str};{temp_val:.2f};{brake_int}\n")
# 3. Konsolenausgabe
print(f"{time_str:<20} | {temp_val:<15.2f}")
print(f"{time_str:<20} | {temp_val:<15.2f} | {brake_int:<15}")
# 4. datetime-Objekt direkt für den Plot nutzen
# 4. Daten für Plot aufbereiten
timestamps.append(now)
temp_values.append(temp_val)
# Maximal 60 Messpunkte anzeigen (rollendes Fenster)
if len(timestamps) > 60:
timestamps.pop(0)
temp_values.pop(0)
# if len(timestamps) > 60:
# timestamps.pop(0)
# temp_values.pop(0)
# Plot aktualisieren
# Plot-Daten aktualisieren
line.set_xdata(timestamps)
line.set_ydata(temp_values)
# Status-Anzeige im Plot
if brake_val:
status_text.set_text("Bremse: GELÖST (1)")
status_text.set_color('green')
else:
status_text.set_text("Bremse: EINGEFALLEN (0)")
status_text.set_color('red')
ax.relim()
ax.autoscale_view()
fig.canvas.draw()