new
This commit is contained in:
94
replay_pty.py
Normal file
94
replay_pty.py
Normal file
@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Replay a capture file through a pseudo-TTY for local GUI verification."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Воспроизводит лог-файл через PTY как виртуальный серийный порт."
|
||||
)
|
||||
parser.add_argument("file", help="Путь к лог-файлу (например my_picocom_logfile.txt)")
|
||||
parser.add_argument(
|
||||
"--pty",
|
||||
default="/tmp/ttyVIRT0",
|
||||
help="Путь симлинка PTY (по умолчанию /tmp/ttyVIRT0)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--speed",
|
||||
type=float,
|
||||
default=1.0,
|
||||
help=(
|
||||
"Множитель скорости воспроизведения: "
|
||||
"1.0 = реальное время при --baud, "
|
||||
"2.0 = вдвое быстрее, "
|
||||
"0 = максимально быстро"
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--baud",
|
||||
type=int,
|
||||
default=115200,
|
||||
help="Скорость (бод) для расчета задержек (по умолчанию 115200)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if not os.path.isfile(args.file):
|
||||
sys.stderr.write(f"[error] Файл не найден: {args.file}\n")
|
||||
raise SystemExit(1)
|
||||
|
||||
master_fd, slave_fd = os.openpty()
|
||||
slave_path = os.ttyname(slave_fd)
|
||||
os.close(slave_fd)
|
||||
|
||||
try:
|
||||
os.unlink(args.pty)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
os.symlink(slave_path, args.pty)
|
||||
|
||||
print(f"PTY slave : {slave_path}")
|
||||
print(f"Симлинк : {args.pty} -> {slave_path}")
|
||||
print(f"Запустите : python3 -m rfg_adc_plotter.main {args.pty}")
|
||||
print("Ctrl+C для остановки.\n")
|
||||
|
||||
if args.speed > 0:
|
||||
bytes_per_sec = args.baud / 10.0 * args.speed
|
||||
delay_per_byte = 1.0 / bytes_per_sec
|
||||
else:
|
||||
delay_per_byte = 0.0
|
||||
|
||||
chunk_size = 4096
|
||||
loop = 0
|
||||
try:
|
||||
while True:
|
||||
loop += 1
|
||||
print(f"[loop {loop}] {args.file}")
|
||||
with open(args.file, "rb") as handle:
|
||||
while True:
|
||||
chunk = handle.read(chunk_size)
|
||||
if not chunk:
|
||||
break
|
||||
os.write(master_fd, chunk)
|
||||
if delay_per_byte > 0:
|
||||
time.sleep(delay_per_byte * len(chunk))
|
||||
except KeyboardInterrupt:
|
||||
print("\nОстановлено.")
|
||||
finally:
|
||||
try:
|
||||
os.unlink(args.pty)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
os.close(master_fd)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user