Skip to content

Console

See the console guide for an introduction.

console

Interactive serial console with session logging (hwn console).

A small, dependency-free terminal in the spirit of miniterm / picocom, built on any Tube: a serial port, a tcp: ser2net bridge or a simulated device. Keys go to the target one by one (so Ctrl-C reaches U-Boot instead of killing the console) and everything the target prints can be appended to a log file, optionally with a timestamp at the start of every line, as evidence for the report.

Keys:

  • Ctrl-] quits.
  • Ctrl-T opens the menu; the next key selects: b send BREAK, h toggle the hex view, e toggle local echo, Ctrl-T / Ctrl-] send that key literally, ? show the keys.

Example

from hwntools import serial
from hwntools.console import Console

with serial("/dev/ttyUSB0", 115200) as io:
    Console(io, log_path="session.log", timestamps=True).run()

Console

Console(tube: Tube, log_path: str | PathLike[str] | None = None, timestamps: bool = False, echo: bool = False, eol: str = 'cr', hex_view: bool = False, label: str = '', stdin: IO[bytes] | None = None, stdout: IO[bytes] | None = None, drain: float = 0.5)

Connect tube to the terminal.

Parameters:

Name Type Description Default
tube Tube

the connection to the target.

required
log_path str | PathLike[str] | None

append everything received to this file (raw bytes). A header and a footer line mark each session.

None
timestamps bool

prefix every line in the log with [YYYY-mm-dd HH:MM:SS.mmm].

False
echo bool

print typed characters locally (for targets that do not echo).

False
eol str

what Enter sends: "cr" (default), "lf" or "crlf".

'cr'
hex_view bool

show received bytes as hex instead of text (the log stays raw).

False
label str

name of the connection used in messages and the log header.

''
stdin IO[bytes] | None

binary stream of keys; defaults to the terminal. When it is not a terminal (a pipe or a file), its bytes are sent as keys and the console exits once the input ends and the target has been quiet for drain seconds.

None
stdout IO[bytes] | None

binary stream for the target's output; defaults to the terminal.

None
drain float

see stdin.

0.5

on_receive

on_receive(data: bytes) -> None

Handle bytes from the target: show them and append them to the log.

send_break

send_break() -> None

Send a BREAK if the connection supports it (serial ports do; TCP does not).

handle_key

handle_key(key: bytes) -> bool

Process one key (one byte, or one UTF-8 character). Returns False to quit.

handle_keys

handle_keys(keys: bytes) -> bool

Process several keys; False once a key asked to quit.

run

run() -> int

Run until the quit key, the end of piped input or the target closing.

Returns 0 (also when the target closes the connection).