68 خطوط
1.6 KiB
Python
68 خطوط
1.6 KiB
Python
import serial
|
|
import sys
|
|
import time
|
|
|
|
PORT = "/dev/ttyUSB0"
|
|
BAUDRATE = 115200
|
|
TIMEOUT = 2
|
|
MAX_WAIT = 10
|
|
|
|
COMMAND = "bdinfo"
|
|
|
|
try:
|
|
with serial.Serial(
|
|
port=PORT,
|
|
baudrate=BAUDRATE,
|
|
timeout=0.2,
|
|
write_timeout=TIMEOUT
|
|
) as ser:
|
|
|
|
print(f"Connected to {PORT}", flush=True)
|
|
|
|
# Clear old serial data
|
|
ser.reset_input_buffer()
|
|
|
|
# Send command to U-Boot
|
|
ser.write((COMMAND + "\r").encode())
|
|
ser.flush()
|
|
|
|
print(f"Command sent: {COMMAND}", flush=True)
|
|
print("------ U-Boot Response ------", flush=True)
|
|
|
|
response = bytearray()
|
|
start_time = time.monotonic()
|
|
last_data_time = start_time
|
|
|
|
while time.monotonic() - start_time < MAX_WAIT:
|
|
|
|
if ser.in_waiting:
|
|
data = ser.read(ser.in_waiting)
|
|
response.extend(data)
|
|
|
|
# Print response in real time
|
|
print(
|
|
data.decode("utf-8", errors="replace"),
|
|
end="",
|
|
flush=True
|
|
)
|
|
|
|
last_data_time = time.monotonic()
|
|
|
|
# Stop when no new data arrives for 2 seconds
|
|
if response and time.monotonic() - last_data_time > TIMEOUT:
|
|
break
|
|
|
|
time.sleep(0.05)
|
|
|
|
print("\n-----------------------------")
|
|
|
|
if not response:
|
|
print("ERROR: No response from U-Boot")
|
|
sys.exit(1)
|
|
|
|
print("Command completed successfully")
|
|
|
|
except serial.SerialException as e:
|
|
print(f"Serial error: {e}")
|
|
sys.exit(1)
|