Hex Dump View Mode
Relevant source files
The following files were used as context for generating this wiki page:
Hex Dump View Mode is a specialized diagnostic feature in ComChan designed for inspecting raw binary payloads transmitted over serial, BLE, or RTT connections. Instead of attempting to decode incoming bytes as UTF-8 text, this mode renders data in a hexadecimal format, which is essential for debugging non-textual protocols, binary formats, or encrypted streams.
The mode supports two primary variations: a basic raw hex dump and a "pretty" print mode. The pretty mode buffers data until a newline is encountered or a specific length is reached, presenting a structured layout that is easier for human operators to analyze. This feature is fully integrated into the standard CLI monitor and supports simultaneous logging to files for post-mortem analysis.
Sources: README.md:164, src/config.rs:114-118
Architecture and Configuration
The Hex Dump View Mode is controlled via the application's configuration layer and executed within the main monitor loop. Users can enable it through CLI flags or a config.toml file.
Configuration Parameters
The behavior of the hex dump is determined by two boolean flags in the MergedConfig and Args structures.
| Flag | CLI Option | Description | Default |
|---|---|---|---|
hex_mode | -x, --hex | Displays incoming serial data in standard hex dump format. | false |
hex_pretty | --hex-pretty | Displays data in a clean, buffered format, useful for structured packets. | false |
Sources: src/config.rs:170-176, src/config.rs:252-253
Data Processing Flow
When either hex mode is active, the normal string decoding process is bypassed. The application utilizes the pretty-hex crate to format the byte slices retrieved from the hardware buffer.
Sources: src/monitor.rs:567-598, Cargo.toml:40
Implementation Details
Basic Hex Mode (hex_mode)
In basic hex mode, every chunk of data read from the serial port is immediately converted to a hex dump and printed. This provides real-time visibility into every byte as it arrives, which is critical for low-latency debugging.
Sources: src/monitor.rs:567-570
Pretty Hex Mode (hex_pretty)
Pretty mode introduces a buffering layer to improve readability. It accumulates bytes into a hex_buf (a Vec<u8>) and triggers a print operation only when specific conditions are met:
- A newline character (
\n) is detected in the stream. - The buffer reaches a size of 64 bytes.
Once triggered, the buffer is cleared for the next set of data.
Sources: src/monitor.rs:571-583
Output Formatting
Because the monitor operates in terminal "raw mode" to capture keystrokes, standard newlines in the hex dump output are replaced with \r\n (carriage return + line feed) to ensure correct vertical alignment in the terminal.
Sources: src/monitor.rs:587-588
Logging and Simulation
Hex Dump View Mode is compatible with other ComChan features:
- Logging: If a log file is specified, the full hex dump (including formatting) is written to the log with a
RX HEXprefix and a local timestamp. - Simulation: When running in simulation mode (
--simulate), mock sensor data strings are converted to hex dumps if either flag is enabled.
// src/monitor.rs:452-458
if config.hex_mode || config.hex_pretty {
let hex_out = format!("{:?}", sim_text.as_bytes().hex_dump());
let raw_mode_safe_hex = hex_out.replace('\n', "\r\n");
print!("\r\n{}\r\n", raw_mode_safe_hex);
io::stdout().flush().ok();
}
Sources: src/monitor.rs:452-463, src/monitor.rs:592-595
Conclusion
Hex Dump View Mode provides a vital toolset for embedded developers dealing with binary protocols. By leveraging the pretty-hex crate and integrating with the existing monitor loop, ComChan allows for both high-speed raw byte monitoring and structured, buffered analysis, ensuring that binary data is as accessible as standard text logs.