This repository was archived by the owner on Sep 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
89 lines (73 loc) · 2.42 KB
/
Copy pathutil.py
File metadata and controls
89 lines (73 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""PyBlasher utility helper functions."""
import os.path
import sys
import serial
from serial.tools import list_ports
from constants import *
def parse_hex(s: str) -> bytes:
"""Accepts: '01 0A ff', '0x01,0x0A,0xFF', or '010AFF'."""
cleaned = (
s.replace("0x", "")
.replace(",", " ")
.replace("\n", " ")
.replace("\t", " ")
.strip()
)
parts = cleaned.split()
if (
len(parts) == 1
and all(c in "0123456789abcdefABCDEF" for c in parts[0])
and len(parts[0]) % 2 == 0
):
return bytes.fromhex(parts[0])
return bytes(int(p, 16) for p in parts if p)
def hexdump(data: bytes, width: int = 16) -> str:
lines = []
for i in range(0, len(data), width):
chunk = data[i : i + width]
hex_part = " ".join(f"{b:02X}" for b in chunk)
ascii_part = "".join(chr(b) if 32 <= b < 127 else "." for b in chunk)
lines.append(f"{i:04X} {hex_part:<{width*3}} {ascii_part}")
return "\n".join(lines)
def resource_path(relative_path: str) -> str:
"""Get absolute path using a relative path to a resource.
Based on: https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-in-the-exe-file.
"""
try:
base_path = sys._MEIPASS2
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def find_cp2102n_ports() -> list[str]:
"""Scan serial ports and return those matching the CP2102N VID/PID."""
matches = []
vid_pid = f"{CP2102N_VID:04X}:{CP2102N_PID:04X}".lower()
for port in list_ports.comports():
if port.vid == CP2102N_VID and port.pid == CP2102N_PID:
matches.append(port.device)
elif port.hwid and vid_pid in port.hwid.lower():
matches.append(port.device)
return matches
def open_serial_port(
port: str,
baud: int = 115200,
timeout: float = 0.2,
write_timeout: float = 0.5,
) -> serial.Serial:
"""Open a serial port with sane defaults and clean buffers."""
ser = serial.Serial(
port=port,
baudrate=baud,
timeout=timeout,
write_timeout=write_timeout,
rtscts=False,
dsrdtr=False,
)
ser.dtr = False
ser.rts = False
ser.reset_input_buffer()
ser.reset_output_buffer()
return ser
def write_serial_bytes(ser: serial.Serial, data: bytes) -> None:
ser.write(data)
ser.flush()