fix
This commit is contained in:
parent
aa1596cc43
commit
7d9be883a5
@ -1,7 +1,10 @@
|
|||||||
import threading, queue, time, csv, os
|
import threading, queue, time, csv, os
|
||||||
|
|
||||||
CHUNK = 2000
|
CHUNK = 2000
|
||||||
|
SAMPLE_RATE = 100_000.0 # 100 kS/s
|
||||||
|
DT = 1.0 / SAMPLE_RATE
|
||||||
OUTDIR = "logs"
|
OUTDIR = "logs"
|
||||||
|
FLUSH_EVERY = 50
|
||||||
os.makedirs(OUTDIR, exist_ok=True)
|
os.makedirs(OUTDIR, exist_ok=True)
|
||||||
|
|
||||||
class DeviceController:
|
class DeviceController:
|
||||||
@ -59,23 +62,45 @@ class DeviceController:
|
|||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
||||||
def writer_loop(self):
|
def writer_loop(self):
|
||||||
|
os.makedirs(OUTDIR, exist_ok=True)
|
||||||
ts0 = None
|
ts0 = None
|
||||||
fn = os.path.join(OUTDIR, f"{time.strftime('%Y%m%d_%H%M%S')}_{self.dev.serial}.csv")
|
fn = os.path.join(OUTDIR, f"{time.strftime('%Y%m%d_%H%M%S')}_{self.dev.serial}.csv")
|
||||||
|
chunks_written = 0
|
||||||
|
sample_idx = 0
|
||||||
|
|
||||||
|
try:
|
||||||
with open(fn, "w", newline="") as f:
|
with open(fn, "w", newline="") as f:
|
||||||
w = csv.writer(f)
|
w = csv.writer(f)
|
||||||
|
# Long-Format: eine Zeile pro Kanalwert
|
||||||
w.writerow(["t_rel_s", "ch", "value"])
|
w.writerow(["t_rel_s", "ch", "value"])
|
||||||
sample_idx = 0
|
|
||||||
while not (self.stop_evt.is_set() and self.writer_q.empty()):
|
while not (self.stop_evt.is_set() and self.writer_q.empty()):
|
||||||
try:
|
try:
|
||||||
ts, va, vb = self.writer_q.get(timeout=0.2)
|
ts, va, vb = self.writer_q.get(timeout=0.2)
|
||||||
except Exception:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
if ts0 is None: ts0 = ts
|
|
||||||
for i, v in enumerate(va):
|
if ts0 is None:
|
||||||
t_rel = (sample_idx + i) * 1e-5 # 100 kS/s → 10 µs
|
ts0 = ts
|
||||||
w.writerow([t_rel, "A", v])
|
|
||||||
for i, v in enumerate(vb):
|
# Sicherstellen, dass A und B gleich lang sind
|
||||||
t_rel = (sample_idx + i) * 1e-5
|
if len(va) != len(vb):
|
||||||
w.writerow([t_rel, "B", v])
|
n = min(len(va), len(vb))
|
||||||
|
va, vb = va[:n], vb[:n]
|
||||||
|
|
||||||
|
# Zeitindex aus Sample-Index ableiten (konstante Rate)
|
||||||
|
for i, (a, b) in enumerate(zip(va, vb)):
|
||||||
|
t_rel = (sample_idx + i) * DT
|
||||||
|
w.writerow([t_rel, "A", a])
|
||||||
|
w.writerow([t_rel, "B", b])
|
||||||
|
|
||||||
sample_idx += len(va)
|
sample_idx += len(va)
|
||||||
print(f"[{self.dev.serial}] Datei geschlossen.")
|
chunks_written += 1
|
||||||
|
|
||||||
|
if chunks_written % FLUSH_EVERY == 0:
|
||||||
|
f.flush() # optional: os.fsync(f.fileno())
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[{self.dev.serial}] Writer-Fehler: {e}")
|
||||||
|
finally:
|
||||||
|
print(f"[{self.dev.serial}] Datei geschlossen: {fn}")
|
||||||
Loading…
x
Reference in New Issue
Block a user