serial_command
Send a command to a serial device and wait for its response, with optional regex pattern matching and automated reply for time-sensitive interactions.
Instructions
Send a command and wait for the response. This is the primary tool for interacting with serial devices — it combines write + read into a single atomic operation.
If expect is provided, waits until that regex pattern appears in the
response. Without expect, waits for the device to stop sending (300ms
of silence after last received byte).
If respond or respond_hex is provided along with expect, the response
is sent immediately when the pattern matches — before this tool returns.
This enables sub-millisecond triggered responses for time-sensitive sequences.
The respond string is sent as-is (no newline appended).
Examples: - Linux shell: serial_command(data="ls -la", expect="\$") - AT modem: serial_command(data="AT", expect="OK|ERROR") - Router CLI: serial_command(data="show version", expect="#") - Simple ping: serial_command(data="hello", timeout=2) - Reboot + catch bootloader: serial_command(data="reboot", expect="Hit any key", respond=" ")
Args: data: Text to send to the device expect: Regex pattern to wait for in the response (e.g. "\$", "OK", ">") timeout: Max seconds to wait for response (default 5) session_id: Port name of the session. Optional if only one session is open. encoding: Character encoding (default utf-8) append_newline: Whether to append \r\n to the data (default True) respond: Text to send immediately when expect pattern matches (sent as-is, no newline) respond_hex: Hex bytes to send when expect pattern matches (e.g. "7F", "AA 55")
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | ||
| expect | No | ||
| respond | No | ||
| timeout | No | ||
| encoding | No | utf-8 | |
| session_id | No | ||
| respond_hex | No | ||
| append_newline | No |