Repository navigation
Expand file tree
/
Copy pathpluto_spectrum_analyzer.py
More file actions
646 lines (531 loc) · 24.5 KB
/
Copy pathpluto_spectrum_analyzer.py
File metadata and controls
646 lines (531 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#!/usr/bin/env python3
"""
ADALM-PLUTO Wideband Spectrum Analyzer
=======================================
Digitalisiert lückenlos grosse Frequenzbereiche durch sequentielles
Scannen mit maximaler Abtastrate und anschliessender Zusammensetzung.
Abhängigkeiten:
pip install pyadi-iio adi numpy scipy matplotlib
Verwendung:
python pluto_spectrum_analyzer.py [--uri ip:192.168.2.1] [--fmin 100e6] [--fmax 2100e6]
"""
import numpy as np
import argparse
import sys
import time
import json
import threading
import queue
from dataclasses import dataclass, field
from typing import Optional
# ──────────────────────────────────────────────────────────────────────────────
# Konstanten & Konfiguration
# ──────────────────────────────────────────────────────────────────────────────
# ADALM-PLUTO Gerätegrenzen
PLUTO_SAMPLE_RATE_MAX = 61_440_000 # 61.44 MSPS (theoretisch; stabil: ~56 MSPS)
PLUTO_SAMPLE_RATE_USE = 56_000_000 # Stabile maximale Samplingrate
PLUTO_LO_MIN = 70_000_000 # 70 MHz
PLUTO_LO_MAX = 6_000_000_000 # 6 GHz
PLUTO_BANDWIDTH_MAX = 56_000_000 # Analoge Filterbandbreite
# Nutzbare Basisbandbandbreite
# Durch Nyquist und Filterrolloff ca. 80% der Samplingrate nutzbar
USABLE_BW_FACTOR = 0.80
USABLE_BW = int(PLUTO_SAMPLE_RATE_USE * USABLE_BW_FACTOR) # ~44.8 MHz
# FFT
FFT_SIZE = 8192 # Hohe Auflösung
AVERAGES = 4 # Mittelwert-Mittelung pro Segment
WINDOW_FUNC = "blackman" # Gut für Spektrumanalyse
# Überlappung zwischen Segmenten (verhindert Lücken durch Filterabfall)
OVERLAP_FACTOR = 0.10 # 10% Überlappung
OVERLAP_HZ = int(USABLE_BW * OVERLAP_FACTOR)
@dataclass
class ScanConfig:
"""Konfiguration für einen Scan-Durchlauf."""
freq_min: float = 100e6
freq_max: float = 2100e6
sample_rate: int = PLUTO_SAMPLE_RATE_USE
usable_bw: int = USABLE_BW
overlap_hz: int = OVERLAP_HZ
fft_size: int = FFT_SIZE
averages: int = AVERAGES
gain_db: int = 40 # Manueller Gewinn (AGC deaktiviert)
uri: str = "ip:192.168.2.1"
@property
def step_hz(self) -> int:
"""Schrittweite zwischen zwei LO-Positionen."""
return self.usable_bw - self.overlap_hz
@property
def center_freqs(self) -> list:
"""Liste aller benötigten LO-Frequenzen."""
freqs = []
f = self.freq_min + self.usable_bw / 2
while f - self.usable_bw / 2 < self.freq_max:
freqs.append(f)
f += self.step_hz
return freqs
@property
def num_segments(self) -> int:
return len(self.center_freqs)
@property
def freq_resolution_hz(self) -> float:
return self.sample_rate / self.fft_size
# ──────────────────────────────────────────────────────────────────────────────
# PlutoSDR Schnittstelle
# ──────────────────────────────────────────────────────────────────────────────
class PlutoReceiver:
"""
Verwaltet die Verbindung zum ADALM-PLUTO und konfiguriert
ihn für den Betrieb ohne AGC.
"""
def __init__(self, cfg: ScanConfig):
self.cfg = cfg
self.sdr = None
self._connected = False
def connect(self):
"""Verbindet mit dem ADALM-PLUTO und konfiguriert Grundeinstellungen."""
try:
import adi
except ImportError:
raise ImportError(
"pyadi-iio nicht installiert.\n"
"Bitte ausführen: pip install pyadi-iio"
)
print(f"[PlutoRX] Verbinde mit {self.cfg.uri} ...")
self.sdr = adi.Pluto(uri=self.cfg.uri)
# ── Samplingrate & Bandbreite ──────────────────────────────────────
self.sdr.sample_rate = self.cfg.sample_rate
self.sdr.rx_rf_bandwidth = PLUTO_BANDWIDTH_MAX
print(f"[PlutoRX] Samplingrate: {self.cfg.sample_rate / 1e6:.2f} MSPS")
print(f"[PlutoRX] HF-Bandbreite: {PLUTO_BANDWIDTH_MAX / 1e6:.2f} MHz")
# ── AGC DEAKTIVIEREN – kritisch für reproduzierbare Messungen ──────
# 'manual' = AGC vollständig aus; Gain wird direkt gesetzt
self.sdr.gain_control_mode_chan0 = "manual"
self.sdr.rx_hardwaregain_chan0 = self.cfg.gain_db
print(f"[PlutoRX] AGC: DEAKTIVIERT (manual)")
print(f"[PlutoRX] HW-Gewinn: {self.cfg.gain_db} dB")
# ── Puffer-Grösse ──────────────────────────────────────────────────
# Mindestens fft_size * averages Samples für stabile Mittelung
buf_samples = self.cfg.fft_size * (self.cfg.averages + 2)
self.sdr.rx_buffer_size = buf_samples
print(f"[PlutoRX] Puffer: {buf_samples} Samples")
self._connected = True
print(f"[PlutoRX] ✓ Verbunden.")
def tune(self, center_freq_hz: float):
"""Stellt den LO auf die angegebene Frequenz."""
if not self._connected:
raise RuntimeError("Nicht verbunden. connect() aufrufen.")
# ADALM-PLUTO akzeptiert ganze Zahlen
self.sdr.rx_lo = int(center_freq_hz)
def capture(self) -> np.ndarray:
"""
Liest einen Puffer ein und gibt das IQ-Signal zurück.
Durch zweimaliges Einlesen wird der erste (ggf. inkohärente)
Puffer nach dem Umschalten verworfen.
"""
_ = self.sdr.rx() # Ersten Puffer nach Frequenzwechsel verwerfen
return self.sdr.rx() # Nutzdaten
def close(self):
"""Gibt die Geräteverbindung frei."""
if self.sdr is not None:
try:
del self.sdr
except Exception:
pass
self._connected = False
print("[PlutoRX] Verbindung getrennt.")
# ──────────────────────────────────────────────────────────────────────────────
# Spektrum-Prozessor
# ──────────────────────────────────────────────────────────────────────────────
class SpectrumProcessor:
"""
Berechnet das gemittelte Leistungsdichtespektrum aus IQ-Rohdaten.
"""
def __init__(self, cfg: ScanConfig):
self.cfg = cfg
# Fensterfunktion vorberechnen
self.window = np.blackman(cfg.fft_size)
self.window_power = np.sum(self.window ** 2)
def compute_psd(self, iq_samples: np.ndarray) -> np.ndarray:
"""
Berechnet das gemittelte PSD in dBFS.
Args:
iq_samples: Komplexe IQ-Samples
Returns:
PSD in dBFS, Länge = fft_size (Frequenzachse: DC-zentriert)
"""
n = self.cfg.fft_size
avgs = self.cfg.averages
# Sicherstellen dass genug Samples vorhanden
needed = n * avgs
if len(iq_samples) < needed:
# Auffüllen falls nötig
iq_samples = np.tile(iq_samples, int(np.ceil(needed / len(iq_samples))))
# Samples normalisieren (Pluto liefert int16, adi normalisiert zu float)
# Mittelwert entfernen (DC-Offset)
iq_samples = iq_samples - np.mean(iq_samples)
# Gemitteltes Spektrum berechnen
psd_sum = np.zeros(n)
for i in range(avgs):
chunk = iq_samples[i * n: (i + 1) * n]
if len(chunk) < n:
break
spec = np.fft.fft(chunk * self.window, n=n)
psd_sum += np.abs(spec) ** 2
psd = psd_sum / avgs / self.window_power
# FFT-Shift: Negative Frequenzen links
psd = np.fft.fftshift(psd)
# In dBFS umrechnen (normiert auf Vollaussteuerung)
psd_db = 10 * np.log10(psd + 1e-20)
return psd_db
def freq_axis(self, center_hz: float) -> np.ndarray:
"""Frequenzachse für ein Segment in Hz."""
n = self.cfg.fft_size
fs = self.cfg.sample_rate
f = np.fft.fftshift(np.fft.fftfreq(n, d=1.0 / fs))
return center_hz + f
# ──────────────────────────────────────────────────────────────────────────────
# Sweep-Engine
# ──────────────────────────────────────────────────────────────────────────────
class SweepEngine:
"""
Koordiniert den lückenlosen Sweep über den gesamten Frequenzbereich.
"""
def __init__(self, cfg: ScanConfig):
self.cfg = cfg
self.rx = PlutoReceiver(cfg)
self.proc = SpectrumProcessor(cfg)
self._running = False
def connect(self):
self.rx.connect()
def close(self):
self.rx.close()
def sweep_once(self) -> tuple[np.ndarray, np.ndarray]:
"""
Führt einen vollständigen Sweep durch und gibt
(frequencies_hz, power_dbfs) zurück.
"""
cfg = self.cfg
centers = cfg.center_freqs
usable_bw = cfg.usable_bw
overlap = cfg.overlap_hz
fft_size = cfg.fft_size
fs = cfg.sample_rate
# Frequenzauflösung
df = fs / fft_size
# Anzahl nutzbarer Bins (Mitte des Spektrums, ohne Randabfall)
half_usable = usable_bw / 2
usable_bins = int(half_usable / df) # Bins auf jeder Seite
# Überlappungs-Bins
overlap_bins = int(overlap / 2 / df)
# Innere nutzbare Bins (ohne Rand)
inner_low = fft_size // 2 - usable_bins + overlap_bins
inner_high = fft_size // 2 + usable_bins - overlap_bins
all_freqs = []
all_power = []
t_start = time.time()
for idx, fc in enumerate(centers):
# LO einstellen
self.rx.tune(fc)
# Samples einlesen
iq = self.capture_averaged()
# PSD berechnen
psd = self.proc.compute_psd(iq)
fa = self.proc.freq_axis(fc)
# Nur nutzbaren Mittelteil verwenden
seg_f = fa[inner_low:inner_high]
seg_p = psd[inner_low:inner_high]
# Frequenzbereich beschneiden
mask = (seg_f >= cfg.freq_min) & (seg_f <= cfg.freq_max)
all_freqs.append(seg_f[mask])
all_power.append(seg_p[mask])
elapsed = time.time() - t_start
print(f"\r[Sweep] Segment {idx+1}/{len(centers)} | "
f"LO={fc/1e6:.1f} MHz | "
f"{elapsed:.2f}s", end="", flush=True)
print() # Newline nach Progress
# Zusammensetzen
freqs = np.concatenate(all_freqs)
power = np.concatenate(all_power)
# Sortieren (sollte schon sortiert sein)
sort_idx = np.argsort(freqs)
return freqs[sort_idx], power[sort_idx]
def capture_averaged(self) -> np.ndarray:
"""Liest Samples ein. Erster Puffer nach LO-Wechsel wird verworfen."""
return self.rx.capture()
def continuous_sweep(self, callback, stop_event: threading.Event):
"""
Führt kontinuierlich Sweeps durch und ruft callback(freqs, power) auf.
"""
sweep_count = 0
while not stop_event.is_set():
try:
t0 = time.time()
freqs, power = self.sweep_once()
dt = time.time() - t0
sweep_count += 1
print(f"[Sweep] #{sweep_count} abgeschlossen in {dt:.2f}s "
f"({len(freqs)} Punkte)")
callback(freqs, power)
except Exception as e:
print(f"[Sweep] Fehler: {e}", file=sys.stderr)
if not stop_event.is_set():
time.sleep(1)
# ──────────────────────────────────────────────────────────────────────────────
# Matplotlib Live-Anzeige (falls kein Web-Server benötigt)
# ──────────────────────────────────────────────────────────────────────────────
class MatplotlibDisplay:
"""Einfache Live-Anzeige mit Matplotlib."""
def __init__(self, cfg: ScanConfig):
self.cfg = cfg
self.fig = None
self.ax = None
self.line = None
self._latest_data = None
def setup(self):
import matplotlib.pyplot as plt
import matplotlib.animation as animation
self.plt = plt
self.anim_mod = animation
self.fig, self.ax = plt.subplots(figsize=(16, 6))
self.fig.patch.set_facecolor('#0d1117')
self.ax.set_facecolor('#161b22')
self.ax.set_xlabel("Frequenz (MHz)", color='#8b949e')
self.ax.set_ylabel("Leistung (dBFS)", color='#8b949e')
self.ax.set_title(
f"ADALM-PLUTO Spektrumanalyzer | "
f"{self.cfg.freq_min/1e6:.0f}–{self.cfg.freq_max/1e6:.0f} MHz | "
f"AGC: AUS | Gewinn: {self.cfg.gain_db} dB",
color='#58a6ff', fontsize=12
)
self.ax.tick_params(colors='#8b949e')
self.ax.spines[:].set_color('#30363d')
self.ax.grid(True, color='#21262d', linewidth=0.5)
self.line, = self.ax.plot([], [], color='#58a6ff',
linewidth=0.7, alpha=0.9)
self.ax.set_xlim(self.cfg.freq_min / 1e6, self.cfg.freq_max / 1e6)
self.ax.set_ylim(-100, 0)
plt.tight_layout()
def update_callback(self, freqs: np.ndarray, power: np.ndarray):
self._latest_data = (freqs / 1e6, power)
def _animate(self, frame):
if self._latest_data is not None:
f, p = self._latest_data
self.line.set_data(f, p)
return (self.line,)
def run(self, sweep_engine: SweepEngine):
self.setup()
stop_event = threading.Event()
sweep_thread = threading.Thread(
target=sweep_engine.continuous_sweep,
args=(self.update_callback, stop_event),
daemon=True
)
sweep_thread.start()
anim = self.anim_mod.FuncAnimation(
self.fig, self._animate,
interval=500, blit=True, cache_frame_data=False
)
try:
self.plt.show()
except KeyboardInterrupt:
pass
finally:
stop_event.set()
sweep_engine.close()
# ──────────────────────────────────────────────────────────────────────────────
# JSON-Server für Web-UI (optional)
# ──────────────────────────────────────────────────────────────────────────────
class WebServer:
"""
Minimaler HTTP-Server, der Spektrumdaten als JSON ausliefert.
Kompatibel mit der beiliegenden Web-UI.
"""
def __init__(self, cfg: ScanConfig, port: int = 8765):
self.cfg = cfg
self.port = port
self._data_lock = threading.Lock()
self._latest = {"freqs": [], "power": [], "timestamp": 0}
def update(self, freqs: np.ndarray, power: np.ndarray):
with self._data_lock:
self._latest = {
"freqs": (freqs / 1e6).tolist(), # MHz
"power": power.tolist(),
"timestamp": time.time(),
"meta": {
"gain_db": self.cfg.gain_db,
"agc": "off",
"sample_rate_msps": self.cfg.sample_rate / 1e6,
"freq_min_mhz": self.cfg.freq_min / 1e6,
"freq_max_mhz": self.cfg.freq_max / 1e6,
"num_segments": self.cfg.num_segments,
"res_hz": self.cfg.freq_resolution_hz,
}
}
def _handle(self, request_handler_class):
"""Gibt Handler-Klasse mit Zugriff auf self zurück."""
server = self
from http.server import BaseHTTPRequestHandler
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/spectrum":
with server._data_lock:
body = json.dumps(server._latest).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(body)
elif self.path == "/":
self.send_response(302)
self.send_header("Location", "/spectrum")
self.end_headers()
else:
self.send_response(404)
self.end_headers()
def log_message(self, fmt, *args):
pass # Kein Log-Spam
return Handler
def start(self):
from http.server import HTTPServer
handler = self._handle(None)
httpd = HTTPServer(("0.0.0.0", self.port), handler)
print(f"[WebServer] Lausche auf http://localhost:{self.port}/spectrum")
t = threading.Thread(target=httpd.serve_forever, daemon=True)
t.start()
# ──────────────────────────────────────────────────────────────────────────────
# Hauptprogramm
# ──────────────────────────────────────────────────────────────────────────────
def parse_args():
p = argparse.ArgumentParser(
description="ADALM-PLUTO Wideband Spektrumanalyzer",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
p.add_argument("--uri", default="ip:192.168.2.1",
help="PlutoSDR URI (z.B. ip:192.168.2.1 oder usb:)")
p.add_argument("--fmin", type=float, default=100e6,
help="Startfrequenz in Hz")
p.add_argument("--fmax", type=float, default=2100e6,
help="Stopfrequenz in Hz")
p.add_argument("--gain", type=int, default=40,
help="HF-Gewinn in dB (AGC deaktiviert, 0–73 dB)")
p.add_argument("--fft", type=int, default=FFT_SIZE,
help="FFT-Grösse")
p.add_argument("--avg", type=int, default=AVERAGES,
help="Anzahl Mittelwerte pro Segment")
p.add_argument("--port", type=int, default=8765,
help="HTTP-Port für Web-UI (0 = deaktiviert)")
p.add_argument("--no-gui", action="store_true",
help="Matplotlib-GUI deaktivieren")
p.add_argument("--once", action="store_true",
help="Nur einen Sweep durchführen und beenden")
p.add_argument("--info", action="store_true",
help="Scan-Parameter anzeigen und beenden")
return p.parse_args()
def print_info(cfg: ScanConfig):
bw_total = cfg.freq_max - cfg.freq_min
print("=" * 60)
print(" ADALM-PLUTO Spektrumanalyzer – Konfiguration")
print("=" * 60)
print(f" URI: {cfg.uri}")
print(f" Frequenzbereich: {cfg.freq_min/1e6:.1f} – {cfg.freq_max/1e6:.1f} MHz")
print(f" Gesamtbandbreite: {bw_total/1e6:.1f} MHz")
print(f" Samplingrate: {cfg.sample_rate/1e6:.2f} MSPS")
print(f" Nutzb. BW/Segment: {cfg.usable_bw/1e6:.2f} MHz")
print(f" Überlappung: {cfg.overlap_hz/1e6:.2f} MHz")
print(f" Schrittweite: {cfg.step_hz/1e6:.2f} MHz")
print(f" Anzahl Segmente: {cfg.num_segments}")
print(f" FFT-Grösse: {cfg.fft_size}")
print(f" Frequenzauflösung: {cfg.freq_resolution_hz:.1f} Hz")
print(f" Mittelwerte: {cfg.averages}")
print(f" AGC: DEAKTIVIERT")
print(f" HW-Gewinn: {cfg.gain_db} dB")
print("=" * 60)
def main():
args = parse_args()
cfg = ScanConfig(
freq_min = args.fmin,
freq_max = args.fmax,
sample_rate = PLUTO_SAMPLE_RATE_USE,
usable_bw = USABLE_BW,
overlap_hz = OVERLAP_HZ,
fft_size = args.fft,
averages = args.avg,
gain_db = args.gain,
uri = args.uri,
)
print_info(cfg)
if args.info:
return
engine = SweepEngine(cfg)
try:
engine.connect()
except Exception as e:
print(f"\n[FEHLER] Verbindung fehlgeschlagen: {e}", file=sys.stderr)
print("Tipps:", file=sys.stderr)
print(" - IP-Adresse prüfen: ping 192.168.2.1", file=sys.stderr)
print(" - USB-Verbindung: --uri usb:", file=sys.stderr)
print(" - IIO-Kontext prüfen: iio_info -n 192.168.2.1", file=sys.stderr)
sys.exit(1)
# ── Einzel-Sweep ──────────────────────────────────────────────────────
if args.once:
freqs, power = engine.sweep_once()
engine.close()
# Ergebnis als CSV ausgeben
print(f"\nFrequenz(MHz),Leistung(dBFS)")
for f, p in zip(freqs / 1e6, power):
print(f"{f:.4f},{p:.2f}")
return
# ── Web-Server ────────────────────────────────────────────────────────
web = None
if args.port > 0:
web = WebServer(cfg, port=args.port)
web.start()
# ── Callbacks zusammenstellen ─────────────────────────────────────────
def on_sweep(freqs, power):
if web:
web.update(freqs, power)
# ── GUI oder Headless ─────────────────────────────────────────────────
if not args.no_gui:
disp = MatplotlibDisplay(cfg)
def combined_callback(freqs, power):
disp.update_callback(freqs, power)
on_sweep(freqs, power)
# Sweep in eigenem Thread
stop_event = threading.Event()
sweep_thread = threading.Thread(
target=engine.continuous_sweep,
args=(combined_callback, stop_event),
daemon=True
)
sweep_thread.start()
disp.setup()
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def animate(frame):
if disp._latest_data is not None:
f, p = disp._latest_data
disp.line.set_data(f, p)
return (disp.line,)
anim = animation.FuncAnimation(
disp.fig, animate,
interval=200, blit=True, cache_frame_data=False
)
try:
plt.show()
except KeyboardInterrupt:
pass
finally:
stop_event.set()
engine.close()
else:
# Headless: nur sweepen
stop_event = threading.Event()
try:
engine.continuous_sweep(on_sweep, stop_event)
except KeyboardInterrupt:
print("\n[Info] Beendet durch Benutzer.")
finally:
stop_event.set()
engine.close()
if __name__ == "__main__":
main()