fix: resolve CSV data truncation in save_to_csv by correctly identifying column widths and remove redundant logic in ZWaferParser
This commit is contained in:
@@ -104,9 +104,6 @@ class ZWaferParser:
|
||||
elif key.lower() == "wafer id":
|
||||
wafer_data.serial = value
|
||||
|
||||
if found_kv and wafer_data.date == datetime.min:
|
||||
wafer_data.date = datetime.min
|
||||
|
||||
return found_kv
|
||||
|
||||
# ===== Sensor Layout Parsing =====
|
||||
|
||||
@@ -274,7 +274,13 @@ def save_to_csv(
|
||||
filename = f"{serial_number}-{timestamp}.csv"
|
||||
filepath = os.path.join(output_dir, filename)
|
||||
|
||||
num_cols = csv_column_count(family_code)
|
||||
# Header count = max of display column count and actual data width.
|
||||
# csv_column_count gives the display sensor count (e.g. 48 for P),
|
||||
# but the binary parser may yield more readings (244 for P). Use the
|
||||
# actual data width so no sensor values are silently dropped.
|
||||
display_cols = csv_column_count(family_code)
|
||||
data_width = max((len(r) for r in data), default=0)
|
||||
num_cols = max(display_cols, data_width)
|
||||
with open(filepath, "w", encoding="utf-8") as f:
|
||||
# Header row: Sensor1,Sensor2,...
|
||||
headers = [f"Sensor{j + 1}" for j in range(num_cols)]
|
||||
|
||||
@@ -13,6 +13,9 @@ log = logging.getLogger(__name__)
|
||||
|
||||
ParseFrame = Callable[[bytes, int], Frame]
|
||||
|
||||
# Max consecutive resync attempts before giving up on a corrupt stream.
|
||||
_MAX_RESYNC_ATTEMPTS = 50
|
||||
|
||||
class StreamReader:
|
||||
|
||||
def __init__(self, transport, parse_frame: ParseFrame,
|
||||
@@ -103,6 +106,7 @@ class StreamReader:
|
||||
def _run_binary(self, initial_bytes: bytes) -> None:
|
||||
log.info("StreamReader: starting binary stream parsing")
|
||||
buf = bytearray(initial_bytes)
|
||||
resync_attempts = 0
|
||||
|
||||
while not self._stop.is_set():
|
||||
# Find sync marker in buffer.
|
||||
@@ -115,6 +119,7 @@ class StreamReader:
|
||||
idx += 1
|
||||
|
||||
if synced:
|
||||
resync_attempts = 0
|
||||
buf = buf[idx:]
|
||||
# Accumulate header (5 bytes after sync).
|
||||
while len(buf) < 7 and not self._stop.is_set():
|
||||
@@ -152,6 +157,17 @@ class StreamReader:
|
||||
buf = buf[total_packet_len:]
|
||||
else:
|
||||
# Resync: keep only trailing 0xAA if present, then read more.
|
||||
resync_attempts += 1
|
||||
if resync_attempts > _MAX_RESYNC_ATTEMPTS:
|
||||
log.error(
|
||||
"StreamReader: giving up after %d resync attempts "
|
||||
"(no 0xAA 0x88 sync marker found in stream)",
|
||||
resync_attempts,
|
||||
)
|
||||
self._on_error(
|
||||
TimeoutError("Binary stream resync failed: no sync marker")
|
||||
)
|
||||
return
|
||||
if len(buf) > 0 and buf[-1] == 0xAA:
|
||||
buf = bytearray([0xAA])
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user