diff --git a/.gitignore b/.gitignore index c18c73e..5d55824 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ __pycache__/ *.swp *.swo acm_9 +build +.venv +sample_data \ No newline at end of file diff --git a/replay_pty.py b/replay_pty.py index 00a0798..3707f0d 100644 --- a/replay_pty.py +++ b/replay_pty.py @@ -75,16 +75,20 @@ def main(): else: delay_per_byte = 0.0 + _CHUNK = 4096 loop = 0 try: while True: loop += 1 print(f"[loop {loop}] {args.file}") with open(args.file, "rb") as f: - for line in f: - os.write(master_fd, line) + while True: + chunk = f.read(_CHUNK) + if not chunk: + break + os.write(master_fd, chunk) if delay_per_byte > 0: - time.sleep(delay_per_byte * len(line)) + time.sleep(delay_per_byte * len(chunk)) except KeyboardInterrupt: print("\nОстановлено.") finally: diff --git a/rfg_adc_plotter/gui/matplotlib_backend.py b/rfg_adc_plotter/gui/matplotlib_backend.py index 972bb57..a671074 100644 --- a/rfg_adc_plotter/gui/matplotlib_backend.py +++ b/rfg_adc_plotter/gui/matplotlib_backend.py @@ -97,6 +97,7 @@ def run_matplotlib(args): fancy=bool(args.fancy), bin_mode=bool(getattr(args, "bin_mode", False)), logscale=bool(getattr(args, "logscale", False)), + debug=bool(getattr(args, "debug", False)), ) reader.start() diff --git a/rfg_adc_plotter/gui/pyqtgraph_backend.py b/rfg_adc_plotter/gui/pyqtgraph_backend.py index 90741e1..09b7ca3 100644 --- a/rfg_adc_plotter/gui/pyqtgraph_backend.py +++ b/rfg_adc_plotter/gui/pyqtgraph_backend.py @@ -114,6 +114,7 @@ def run_pyqtgraph(args): fancy=bool(args.fancy), bin_mode=bool(getattr(args, "bin_mode", False)), logscale=bool(getattr(args, "logscale", False)), + debug=bool(getattr(args, "debug", False)), ) reader.start() diff --git a/rfg_adc_plotter/io/sweep_reader.py b/rfg_adc_plotter/io/sweep_reader.py index 4c3598a..d187e0a 100644 --- a/rfg_adc_plotter/io/sweep_reader.py +++ b/rfg_adc_plotter/io/sweep_reader.py @@ -26,6 +26,7 @@ class SweepReader(threading.Thread): fancy: bool = False, bin_mode: bool = False, logscale: bool = False, + debug: bool = False, ): super().__init__(daemon=True) self._port_path = port_path @@ -36,6 +37,7 @@ class SweepReader(threading.Thread): self._fancy = bool(fancy) self._bin_mode = bool(bin_mode) self._logscale = bool(logscale) + self._debug = bool(debug) self._max_width: int = 0 self._sweep_idx: int = 0 self._last_sweep_ts: Optional[float] = None @@ -47,6 +49,11 @@ class SweepReader(threading.Thread): return v - 0x1_0000_0000 if (v & 0x8000_0000) else v def _finalize_current(self, xs, ys, channels: Optional[set]): + if self._debug: + if not xs: + sys.stderr.write("[debug] _finalize_current: xs пуст — свип пропущен\n") + else: + sys.stderr.write(f"[debug] _finalize_current: {len(xs)} точек → свип #{self._sweep_idx + 1}\n") if not xs: return ch_list = sorted(channels) if channels else [0] @@ -163,6 +170,9 @@ class SweepReader(threading.Thread): cur_channels: set[int] = set() buf = bytearray() + _dbg_line_count = 0 + _dbg_match_count = 0 + _dbg_sweep_count = 0 while not self._stop.is_set(): data = chunk_reader.read_available() if data: @@ -182,7 +192,12 @@ class SweepReader(threading.Thread): if not line: continue + _dbg_line_count += 1 + if line.startswith(b"Sweep_start"): + if self._debug: + sys.stderr.write(f"[debug] ASCII строка #{_dbg_line_count}: Sweep_start → финализация свипа\n") + _dbg_sweep_count += 1 self._finalize_current(xs, ys, cur_channels) xs.clear() ys.clear() @@ -208,12 +223,35 @@ class SweepReader(threading.Thread): x = int(parts[1], 10) y = int(parts[2], 10) except Exception: + if self._debug and _dbg_line_count <= 5: + hex_repr = " ".join(f"{b:02x}" for b in line[:16]) + sys.stderr.write( + f"[debug] ASCII строка #{_dbg_line_count} ({len(line)} байт): {hex_repr}" + f"{'...' if len(line) > 16 else ''} → похожа на 's', но не парсится\n" + ) continue + _dbg_match_count += 1 + if self._debug and _dbg_match_count <= 3: + sys.stderr.write(f"[debug] ASCII точка: ch={ch} x={x} y={y}\n") if cur_channel is None: cur_channel = ch cur_channels.add(ch) xs.append(x) ys.append(y) + continue + + if self._debug and _dbg_line_count <= 5: + hex_repr = " ".join(f"{b:02x}" for b in line[:16]) + sys.stderr.write( + f"[debug] ASCII строка #{_dbg_line_count} ({len(line)} байт): {hex_repr}" + f"{'...' if len(line) > 16 else ''} → нет совпадения\n" + ) + + if self._debug and _dbg_line_count % 100 == 0: + sys.stderr.write( + f"[debug] ASCII статистика: строк={_dbg_line_count}, " + f"совпадений={_dbg_match_count}, свипов={_dbg_sweep_count}\n" + ) if len(buf) > 1_000_000: del buf[:-262144] @@ -225,9 +263,22 @@ class SweepReader(threading.Thread): ys: list[int] = [] cur_channel: Optional[int] = None cur_channels: set[int] = set() - words = deque() + + # Бинарный протокол (4 слова LE u16 = 8 байт на запись): + # старт свипа: 0xFFFF, 0xFFFF, 0xFFFF, (ch<<8)|0x0A + # Байты на проводе: ff ff ff ff ff ff 0a [ch] + # ch=0 → последнее слово=0x000A; ch=1 → 0x010A; и т.д. + # точка данных: step_u16, value_hi_u16, value_lo_u16, (ch<<8)|0x0A + # Байты на проводе: [step_lo step_hi] [hi_lo hi_hi] [lo_lo lo_hi] 0a [ch] + # value_i32 = sign_extend((value_hi<<16)|value_lo) + # Признак записи: байт 6 == 0x0A, байт 7 — номер канала. + # При десинхронизации сдвигаемся на 1 БАЙТ (не слово) для самосинхронизации. buf = bytearray() + _dbg_byte_count = 0 + _dbg_desync_count = 0 + _dbg_sweep_count = 0 + _dbg_point_count = 0 while not self._stop.is_set(): data = chunk_reader.read_available() if data: @@ -236,62 +287,66 @@ class SweepReader(threading.Thread): time.sleep(0.0005) continue - usable = len(buf) & ~1 - if usable == 0: - continue + while len(buf) >= 8: + # Читаем 4 LE u16 слова прямо из байтового буфера + w0 = int(buf[0]) | (int(buf[1]) << 8) + w1 = int(buf[2]) | (int(buf[3]) << 8) + w2 = int(buf[4]) | (int(buf[5]) << 8) - i = 0 - while i < usable: - w = int(buf[i]) | (int(buf[i + 1]) << 8) - words.append(w) - i += 2 - - # Бинарный протокол: - # старт свипа (актуальный): 0xFFFF, 0xFFFF, 0xFFFF, (ch<<8)|0x0A - # старт свипа (legacy): 0xFFFF, 0xFFFF, channel, 0x0A0A - # точка: step, value_hi, value_lo, 0x000A - while len(words) >= 4: - w0 = int(words[0]) - w1 = int(words[1]) - w2 = int(words[2]) - w3 = int(words[3]) - - if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and (w3 & 0x00FF) == 0x000A: + # Старт свипа: три слова 0xFFFF + маркер 0x0A в байте 6, канал в байте 7 + if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and buf[6] == 0x0A: + ch_new = buf[7] + if self._debug: + sys.stderr.write(f"[debug] BIN: старт свипа, ch={ch_new}\n") + _dbg_sweep_count += 1 self._finalize_current(xs, ys, cur_channels) xs.clear() ys.clear() cur_channels.clear() - cur_channel = (w3 >> 8) & 0x00FF + cur_channel = ch_new cur_channels.add(cur_channel) - for _ in range(4): - words.popleft() + del buf[:8] + _dbg_byte_count += 8 continue - if w0 == 0xFFFF and w1 == 0xFFFF and w3 == 0x0A0A: - self._finalize_current(xs, ys, cur_channels) - xs.clear() - ys.clear() - cur_channels.clear() - cur_channel = w2 + # Точка данных: маркер 0x0A в байте 6, канал в байте 7 + if buf[6] == 0x0A: + ch_from_term = buf[7] + if cur_channel is None: + cur_channel = ch_from_term cur_channels.add(cur_channel) - for _ in range(4): - words.popleft() - continue - - if w3 == 0x000A: - if cur_channel is not None: - cur_channels.add(cur_channel) xs.append(w0) value_u32 = (w1 << 16) | w2 ys.append(self._u32_to_i32(value_u32)) - for _ in range(4): - words.popleft() + del buf[:8] + _dbg_byte_count += 8 + _dbg_point_count += 1 + if self._debug and _dbg_point_count <= 3: + sys.stderr.write( + f"[debug] BIN точка: step={w0} hi={w1:#06x} lo={w2:#06x} " + f"ch={ch_from_term} → value={self._u32_to_i32((w1 << 16) | w2)}\n" + ) continue - # Поток может начаться с середины пакета; сдвигаемся по слову до ресинхронизации. - words.popleft() + # Поток не выровнен; сдвигаемся на 1 байт до ресинхронизации. + _dbg_desync_count += 1 + _dbg_byte_count += 1 + if self._debug and _dbg_desync_count <= 8: + hex6 = " ".join(f"{buf[k]:02x}" for k in range(min(8, len(buf)))) + sys.stderr.write( + f"[debug] BIN десинхронизация #{_dbg_desync_count}: " + f"байты [{hex6}] не совпадают ни с одним шаблоном\n" + ) + if self._debug and _dbg_desync_count == 9: + sys.stderr.write("[debug] BIN: дальнейшие десинхронизации не выводятся (слишком много)\n") + del buf[:1] + + if self._debug and _dbg_byte_count > 0 and _dbg_byte_count % 4000 < 8: + sys.stderr.write( + f"[debug] BIN статистика: байт={_dbg_byte_count}, " + f"десинхронизаций={_dbg_desync_count}, точек={_dbg_point_count}, свипов={_dbg_sweep_count}\n" + ) - del buf[:usable] if len(buf) > 1_000_000: del buf[:-262144] @@ -308,6 +363,9 @@ class SweepReader(threading.Thread): try: chunk_reader = SerialChunkReader(self._src) + if self._debug: + mode_str = "бинарный (--bin)" if self._bin_mode else "ASCII (по умолчанию)" + sys.stderr.write(f"[debug] Режим парсера: {mode_str}\n") if self._bin_mode: self._run_binary_stream(chunk_reader) else: diff --git a/rfg_adc_plotter/main.py b/rfg_adc_plotter/main.py index ca4e644..059b3d4 100755 --- a/rfg_adc_plotter/main.py +++ b/rfg_adc_plotter/main.py @@ -82,8 +82,10 @@ def build_parser() -> argparse.ArgumentParser: dest="bin_mode", action="store_true", help=( - "Бинарный протокол: старт свипа 0xFFFF,0xFFFF,0xFFFF,(CH<<8)|0x0A; " - "точки step,uint32(hi16,lo16),0x000A" + "Бинарный протокол (8 байт на запись, LE u16 слова): " + "старт свипа ff ff ff ff ff ff 0a [ch]; " + "точка step_u16 hi_u16 lo_u16 0a [ch]; " + "value=sign_ext((hi<<16)|lo); ch=0..N в старшем байте маркера" ), ) parser.add_argument( @@ -91,6 +93,11 @@ def build_parser() -> argparse.ArgumentParser: action="store_true", help="После поправки знака применять экспоненту LOG_EXP**x (LOG_EXP=2)", ) + parser.add_argument( + "--debug", + action="store_true", + help="Отладочный вывод парсера: показывает принятые строки/слова и причины отсутствия свипов", + ) return parser