76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Restore calibration settings on MTB:
|
|
- Seriennummer eingeben
|
|
- Neueste JSON-Datei laden
|
|
- meanCount und AdjustScaleI pro Kanal anwenden
|
|
"""
|
|
|
|
import json
|
|
import glob
|
|
import os
|
|
import sys
|
|
import serial.tools.list_ports
|
|
from ModuleTestBox import ModuleTestBox
|
|
|
|
def find_latest_file(serial_id: int, folder: str = "TestResults") -> str:
|
|
pattern = os.path.join(folder, f"MTB_Test_{serial_id}*.json")
|
|
files = glob.glob(pattern)
|
|
if not files:
|
|
raise FileNotFoundError(f"Keine Dateien gefunden für Serial {serial_id!r}")
|
|
# sortiere nach Suffix (höchste Zahl zuletzt)
|
|
files.sort(key=lambda f: int(os.path.splitext(f)[0].rsplit('_',1)[-1]) if '_' in os.path.splitext(f)[0] else 0)
|
|
return files[-1]
|
|
|
|
def prompt_mean_count() -> int:
|
|
while True:
|
|
try:
|
|
mc = int(input("MeanCount nicht in Datei gefunden. Bitte eingeben (z.B. 60): "))
|
|
return mc
|
|
except ValueError:
|
|
print("Bitte eine ganze Zahl eingeben.")
|
|
|
|
def main():
|
|
serial_id = int(input("Seriennummer eingeben: ").strip())
|
|
try:
|
|
filename = find_latest_file(serial_id)
|
|
except FileNotFoundError as e:
|
|
print(e)
|
|
sys.exit(1)
|
|
print(f"Lade Datei: {filename}")
|
|
with open(filename, "r") as f:
|
|
data = json.load(f)
|
|
|
|
# Versuch, meanCount auszulesen
|
|
mean_count = None
|
|
for entry in data:
|
|
if "meanCount" in entry:
|
|
mean_count = entry["meanCount"]
|
|
break
|
|
if mean_count is None:
|
|
mean_count = prompt_mean_count()
|
|
print(f"Verwende meanCount = {mean_count}")
|
|
|
|
# Hole gain_error pro Kanal
|
|
hv_channels = next((e["HVchannels"] for e in data if "HVchannels" in e), None)
|
|
if hv_channels is None:
|
|
print("Keine HVchannels in der Datei gefunden.")
|
|
sys.exit(1)
|
|
|
|
# Verbinden und hochladen
|
|
print("Verbinde mit MTB …")
|
|
with ModuleTestBox(hints=["VID:PID=CAFE:4001"], verbose=False) as mtb:
|
|
mtb.SetBoardID(serial_id)
|
|
mtb.SetMeanCount(mean_count)
|
|
for ch_info in hv_channels:
|
|
ch = ch_info["channel"]
|
|
corr_ppm = ch_info["gain_error"]
|
|
print(f"Channel {ch}: AdjustScaleI({corr_ppm:+.0f})")
|
|
mtb.SelectChannel(ch)
|
|
mtb.AdjustScaleI(int(round(corr_ppm)))
|
|
print("Fertig. Alle Einstellungen geladen.")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|