Basic Serial Monitor Mode
Relevant source files
The following files were used as context for generating this wiki page:
Basic Serial Monitor Mode is the primary operational state of ComChan, designed for bidirectional serial communication between a host computer and embedded devices. It provides a terminal-based interface for monitoring incoming data streams and sending commands to a serial port, supporting standard configurations such as custom baud rates, data bits, and parity settings.
This mode operates as a robust raw monitor with features like auto-reconnection, session logging, and hex dump visualization. It acts as the functional base from which users can hot-swap into the Serial Plotter Mode via specific keyboard shortcuts.
Sources: src/monitor.rs:94-100, README.md:160-170
Architecture and Data Flow
The architecture of Basic Serial Monitor Mode utilizes an asynchronous-style multi-threaded approach to handle concurrent input and output. A dedicated background thread manages user keyboard input to prevent blocking the primary data reception loop, which handles port polling, logging, and terminal rendering.
Threading Model
The system splits responsibilities into two main execution contexts:
- Input Thread: Captures user keystrokes in raw terminal mode, handles local line editing, and transmits completed commands via a
std::sync::mpscchannel. - Main Loop: Manages the serial port connection lifecycle, polls for incoming bytes, processes CSV/Log writing, and renders data to the standard output.
The diagram shows the separation of user input handling and the primary data processing pipeline.
Sources: src/monitor.rs:136-150, src/monitor.rs:270-285
Key Components and Logic
Connection Management
ComChan implements a persistent connection logic that attempts to open the specified serial port. If the device is not found or is disconnected, the monitor enters a polling state, displaying a "Waiting for device" message and attempting to reconnect every second until the hardware is detected.
Sources: src/monitor.rs:328-350
Configuration Parameters
The monitor utilizes a MergedConfig structure to define the serial communication parameters.
| Parameter | Source/File | Description |
|---|---|---|
baud | src/config.rs | Speed of communication (e.g., 9600, 115200). |
data_bits | src/config.rs | Number of bits per character (typically 8). |
stop_bits | src/config.rs | Number of stop bits (typically 1). |
parity | src/config.rs | Error-checking mode: "none", "odd", or "even". |
flow_control | src/config.rs | Handshaking: "none", "software", or "hardware". |
Sources: src/config.rs:250-270, src/serial.rs:10-50
Input Handling and Line Editing
The input thread implements a local line editor that supports:
- Buffer Editing: Backspace and Delete operations.
- Navigation: Left/Right arrow keys to move the cursor within the typed line.
- History: Up/Down arrow keys to cycle through previously sent commands.
- Remote Prompt Preservation: Dynamic redrawing logic that attempts to clear only the locally typed text to avoid overwriting remote device prompts.
Sources: src/monitor.rs:160-230
Specialized Visualization Modes
Hex Dump View
When enabled via --hex or --hex-pretty, ComChan bypasses standard UTF-8 string rendering. In hex-pretty mode, incoming bytes are buffered (until a newline or 64 bytes) and then rendered in a formatted hex dump showing memory offsets, hex values, and ASCII representations.
if config.hex_mode || config.hex_pretty {
let hex_out = format!("{:?}", data_to_print.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:506-525, src/config.rs:140
Logging and Data Export
The monitor concurrently streams data to multiple sinks:
- Standard Log: Appends raw RX/TX strings with local timestamps to a specified
.logfile. - CSV Streamer: Uses
parse_sensor_datato identify numeric patterns (e.g.,Key: Value) and writes them into a column-based CSV format.
Sources: src/monitor.rs:550-570, src/export.rs:150-180
Keyboard Shortcuts
| Shortcut | Action | Source |
|---|---|---|
Ctrl+C | Gracefully shut down and exit ComChan. | src/monitor.rs:175 |
Ctrl+L | Clear the terminal screen. | src/monitor.rs:168 |
Ctrl+P | Switch to Plotter Mode (Instant Mode Hot-Swapping). | src/monitor.rs:172 |
Enter | Send the current line buffer to the serial port. | src/monitor.rs:180 |
Sources: src/monitor.rs:165-185, README.md:166
Conclusion
Basic Serial Monitor Mode provides a high-performance, low-latency interface for serial interaction. By combining standard terminal monitoring with advanced features like auto-recovery, integrated line history, and multiple data export formats, it serves as a comprehensive tool for embedded systems debugging and telemetry.