fix binary format
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -7,3 +7,6 @@ __pycache__/
|
|||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
acm_9
|
acm_9
|
||||||
|
build
|
||||||
|
.venv
|
||||||
|
sample_data
|
||||||
@ -75,16 +75,20 @@ def main():
|
|||||||
else:
|
else:
|
||||||
delay_per_byte = 0.0
|
delay_per_byte = 0.0
|
||||||
|
|
||||||
|
_CHUNK = 4096
|
||||||
loop = 0
|
loop = 0
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
loop += 1
|
loop += 1
|
||||||
print(f"[loop {loop}] {args.file}")
|
print(f"[loop {loop}] {args.file}")
|
||||||
with open(args.file, "rb") as f:
|
with open(args.file, "rb") as f:
|
||||||
for line in f:
|
while True:
|
||||||
os.write(master_fd, line)
|
chunk = f.read(_CHUNK)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
os.write(master_fd, chunk)
|
||||||
if delay_per_byte > 0:
|
if delay_per_byte > 0:
|
||||||
time.sleep(delay_per_byte * len(line))
|
time.sleep(delay_per_byte * len(chunk))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\nОстановлено.")
|
print("\nОстановлено.")
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
@ -97,6 +97,7 @@ def run_matplotlib(args):
|
|||||||
fancy=bool(args.fancy),
|
fancy=bool(args.fancy),
|
||||||
bin_mode=bool(getattr(args, "bin_mode", False)),
|
bin_mode=bool(getattr(args, "bin_mode", False)),
|
||||||
logscale=bool(getattr(args, "logscale", False)),
|
logscale=bool(getattr(args, "logscale", False)),
|
||||||
|
debug=bool(getattr(args, "debug", False)),
|
||||||
)
|
)
|
||||||
reader.start()
|
reader.start()
|
||||||
|
|
||||||
|
|||||||
@ -114,6 +114,7 @@ def run_pyqtgraph(args):
|
|||||||
fancy=bool(args.fancy),
|
fancy=bool(args.fancy),
|
||||||
bin_mode=bool(getattr(args, "bin_mode", False)),
|
bin_mode=bool(getattr(args, "bin_mode", False)),
|
||||||
logscale=bool(getattr(args, "logscale", False)),
|
logscale=bool(getattr(args, "logscale", False)),
|
||||||
|
debug=bool(getattr(args, "debug", False)),
|
||||||
)
|
)
|
||||||
reader.start()
|
reader.start()
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,7 @@ class SweepReader(threading.Thread):
|
|||||||
fancy: bool = False,
|
fancy: bool = False,
|
||||||
bin_mode: bool = False,
|
bin_mode: bool = False,
|
||||||
logscale: bool = False,
|
logscale: bool = False,
|
||||||
|
debug: bool = False,
|
||||||
):
|
):
|
||||||
super().__init__(daemon=True)
|
super().__init__(daemon=True)
|
||||||
self._port_path = port_path
|
self._port_path = port_path
|
||||||
@ -36,6 +37,7 @@ class SweepReader(threading.Thread):
|
|||||||
self._fancy = bool(fancy)
|
self._fancy = bool(fancy)
|
||||||
self._bin_mode = bool(bin_mode)
|
self._bin_mode = bool(bin_mode)
|
||||||
self._logscale = bool(logscale)
|
self._logscale = bool(logscale)
|
||||||
|
self._debug = bool(debug)
|
||||||
self._max_width: int = 0
|
self._max_width: int = 0
|
||||||
self._sweep_idx: int = 0
|
self._sweep_idx: int = 0
|
||||||
self._last_sweep_ts: Optional[float] = None
|
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
|
return v - 0x1_0000_0000 if (v & 0x8000_0000) else v
|
||||||
|
|
||||||
def _finalize_current(self, xs, ys, channels: Optional[set]):
|
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:
|
if not xs:
|
||||||
return
|
return
|
||||||
ch_list = sorted(channels) if channels else [0]
|
ch_list = sorted(channels) if channels else [0]
|
||||||
@ -163,6 +170,9 @@ class SweepReader(threading.Thread):
|
|||||||
cur_channels: set[int] = set()
|
cur_channels: set[int] = set()
|
||||||
|
|
||||||
buf = bytearray()
|
buf = bytearray()
|
||||||
|
_dbg_line_count = 0
|
||||||
|
_dbg_match_count = 0
|
||||||
|
_dbg_sweep_count = 0
|
||||||
while not self._stop.is_set():
|
while not self._stop.is_set():
|
||||||
data = chunk_reader.read_available()
|
data = chunk_reader.read_available()
|
||||||
if data:
|
if data:
|
||||||
@ -182,7 +192,12 @@ class SweepReader(threading.Thread):
|
|||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
_dbg_line_count += 1
|
||||||
|
|
||||||
if line.startswith(b"Sweep_start"):
|
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)
|
self._finalize_current(xs, ys, cur_channels)
|
||||||
xs.clear()
|
xs.clear()
|
||||||
ys.clear()
|
ys.clear()
|
||||||
@ -208,12 +223,35 @@ class SweepReader(threading.Thread):
|
|||||||
x = int(parts[1], 10)
|
x = int(parts[1], 10)
|
||||||
y = int(parts[2], 10)
|
y = int(parts[2], 10)
|
||||||
except Exception:
|
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
|
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:
|
if cur_channel is None:
|
||||||
cur_channel = ch
|
cur_channel = ch
|
||||||
cur_channels.add(ch)
|
cur_channels.add(ch)
|
||||||
xs.append(x)
|
xs.append(x)
|
||||||
ys.append(y)
|
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:
|
if len(buf) > 1_000_000:
|
||||||
del buf[:-262144]
|
del buf[:-262144]
|
||||||
@ -225,9 +263,22 @@ class SweepReader(threading.Thread):
|
|||||||
ys: list[int] = []
|
ys: list[int] = []
|
||||||
cur_channel: Optional[int] = None
|
cur_channel: Optional[int] = None
|
||||||
cur_channels: set[int] = set()
|
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()
|
buf = bytearray()
|
||||||
|
_dbg_byte_count = 0
|
||||||
|
_dbg_desync_count = 0
|
||||||
|
_dbg_sweep_count = 0
|
||||||
|
_dbg_point_count = 0
|
||||||
while not self._stop.is_set():
|
while not self._stop.is_set():
|
||||||
data = chunk_reader.read_available()
|
data = chunk_reader.read_available()
|
||||||
if data:
|
if data:
|
||||||
@ -236,62 +287,66 @@ class SweepReader(threading.Thread):
|
|||||||
time.sleep(0.0005)
|
time.sleep(0.0005)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
usable = len(buf) & ~1
|
while len(buf) >= 8:
|
||||||
if usable == 0:
|
# Читаем 4 LE u16 слова прямо из байтового буфера
|
||||||
continue
|
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
|
# Старт свипа: три слова 0xFFFF + маркер 0x0A в байте 6, канал в байте 7
|
||||||
while i < usable:
|
if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and buf[6] == 0x0A:
|
||||||
w = int(buf[i]) | (int(buf[i + 1]) << 8)
|
ch_new = buf[7]
|
||||||
words.append(w)
|
if self._debug:
|
||||||
i += 2
|
sys.stderr.write(f"[debug] BIN: старт свипа, ch={ch_new}\n")
|
||||||
|
_dbg_sweep_count += 1
|
||||||
# Бинарный протокол:
|
|
||||||
# старт свипа (актуальный): 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:
|
|
||||||
self._finalize_current(xs, ys, cur_channels)
|
self._finalize_current(xs, ys, cur_channels)
|
||||||
xs.clear()
|
xs.clear()
|
||||||
ys.clear()
|
ys.clear()
|
||||||
cur_channels.clear()
|
cur_channels.clear()
|
||||||
cur_channel = (w3 >> 8) & 0x00FF
|
cur_channel = ch_new
|
||||||
cur_channels.add(cur_channel)
|
cur_channels.add(cur_channel)
|
||||||
for _ in range(4):
|
del buf[:8]
|
||||||
words.popleft()
|
_dbg_byte_count += 8
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if w0 == 0xFFFF and w1 == 0xFFFF and w3 == 0x0A0A:
|
# Точка данных: маркер 0x0A в байте 6, канал в байте 7
|
||||||
self._finalize_current(xs, ys, cur_channels)
|
if buf[6] == 0x0A:
|
||||||
xs.clear()
|
ch_from_term = buf[7]
|
||||||
ys.clear()
|
if cur_channel is None:
|
||||||
cur_channels.clear()
|
cur_channel = ch_from_term
|
||||||
cur_channel = w2
|
|
||||||
cur_channels.add(cur_channel)
|
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)
|
xs.append(w0)
|
||||||
value_u32 = (w1 << 16) | w2
|
value_u32 = (w1 << 16) | w2
|
||||||
ys.append(self._u32_to_i32(value_u32))
|
ys.append(self._u32_to_i32(value_u32))
|
||||||
for _ in range(4):
|
del buf[:8]
|
||||||
words.popleft()
|
_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
|
continue
|
||||||
|
|
||||||
# Поток может начаться с середины пакета; сдвигаемся по слову до ресинхронизации.
|
# Поток не выровнен; сдвигаемся на 1 байт до ресинхронизации.
|
||||||
words.popleft()
|
_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:
|
if len(buf) > 1_000_000:
|
||||||
del buf[:-262144]
|
del buf[:-262144]
|
||||||
|
|
||||||
@ -308,6 +363,9 @@ class SweepReader(threading.Thread):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
chunk_reader = SerialChunkReader(self._src)
|
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:
|
if self._bin_mode:
|
||||||
self._run_binary_stream(chunk_reader)
|
self._run_binary_stream(chunk_reader)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -82,8 +82,10 @@ def build_parser() -> argparse.ArgumentParser:
|
|||||||
dest="bin_mode",
|
dest="bin_mode",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help=(
|
help=(
|
||||||
"Бинарный протокол: старт свипа 0xFFFF,0xFFFF,0xFFFF,(CH<<8)|0x0A; "
|
"Бинарный протокол (8 байт на запись, LE u16 слова): "
|
||||||
"точки step,uint32(hi16,lo16),0x000A"
|
"старт свипа 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(
|
parser.add_argument(
|
||||||
@ -91,6 +93,11 @@ def build_parser() -> argparse.ArgumentParser:
|
|||||||
action="store_true",
|
action="store_true",
|
||||||
help="После поправки знака применять экспоненту LOG_EXP**x (LOG_EXP=2)",
|
help="После поправки знака применять экспоненту LOG_EXP**x (LOG_EXP=2)",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--debug",
|
||||||
|
action="store_true",
|
||||||
|
help="Отладочный вывод парсера: показывает принятые строки/слова и причины отсутствия свипов",
|
||||||
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user