-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_for_computer.py
More file actions
93 lines (75 loc) · 2.77 KB
/
code_for_computer.py
File metadata and controls
93 lines (75 loc) · 2.77 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
import threading
import serial
import time
import matplotlib.pyplot as plt
from collections import deque
# Funktion zum Lesen der seriellen Daten
def read_serial(ser, data_queue):
while True:
line = ser.readline()
if line:
decoded_line = line.decode('utf-8').strip()
print(decoded_line)
data_queue.append(decoded_line)
# Funktion zum Aktualisieren des Diagramms
def update_plot(data_queue, rpm_data, duty_data, ax, line1, line2):
while True:
if data_queue:
data = data_queue.pop(0)
if data:
try:
# Extrahiere RPM und Duty-Cycle aus den Daten
rpm, duty = map(float, data.split(','))
# Füge die Daten in die Deques ein
rpm_data.append(rpm)
duty_data.append(duty)
# Aktualisiere die Daten für die Linien
line1.set_ydata(rpm_data)
line2.set_ydata(duty_data)
# Aktualisiere die X-Achse und Y-Achsen
ax.set_xlim(0, len(rpm_data))
ax.set_ylim(min(min(rpm_data), min(duty_data)), max(max(rpm_data), max(duty_data)))
plt.draw()
plt.pause(0.01)
except ValueError:
pass
time.sleep(0.1)
def wait_for_serial_port(port):
while True:
try:
ser = serial.Serial(port, 115200, timeout=1)
print(f"Verbindung zu {port} hergestellt.")
return ser
except (serial.SerialException, OSError) as e:
print(f"Fehler beim Verbinden mit {port}: {e}. Warte auf das Gerät...")
time.sleep(2)
ser = wait_for_serial_port('COM6')
data_queue = []
# Thread zum Lesen der seriellen Daten
serial_thread = threading.Thread(target=read_serial, args=(ser, data_queue))
serial_thread.daemon = True
serial_thread.start()
# Matplotlib-Einstellungen
plt.ion()
fig, ax = plt.subplots(figsize=(10, 6))
# Initialisiere die Deques für die RPM- und Duty-Daten
rpm_data = deque([0]*100, maxlen=100)
duty_data = deque([0]*100, maxlen=100)
# Erstelle die Linien für die Diagramme
line1, = ax.plot(rpm_data, label='RPM', color='blue')
line2, = ax.plot(duty_data, label='Duty Cycle', color='orange')
# Achsenbeschriftungen und Titel
ax.set_title('RPM und Duty Cycle')
ax.set_ylabel('Werte')
ax.set_xlabel('Zeit')
ax.legend()
# Thread zum Aktualisieren des Diagramms
plot_thread = threading.Thread(target=update_plot, args=(data_queue, rpm_data, duty_data, ax, line1, line2))
plot_thread.daemon = True
plot_thread.start()
# Hauptschleife
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Programm beendet.")