Attach to serial monitor and capture output. Keyword Behavior: --expect: Monitors until timeout, then checks if ALL keywords were found. Exit 0 if all found, exit 1 if any missing. --fail-on: Terminates immediately when ANY keyword is found, exits 1. --
(
build_dir: Path,
environment: str | None = None,
monitor_port: str | None = None,
verbose: bool = False,
timeout: int = 80,
fail_keywords: list[str] | None = None,
expect_keywords: list[str] | None = None,
stream: bool = False,
input_on_trigger: str | None = None,
device_error_keywords: list[str] | None = None,
stop_keyword: str | None = None,
json_rpc_commands: list[dict[str, Any]] | None = None,
use_pyserial: bool = False,
)
| 384 | |
| 385 | |
| 386 | def run_monitor( |
| 387 | build_dir: Path, |
| 388 | environment: str | None = None, |
| 389 | monitor_port: str | None = None, |
| 390 | verbose: bool = False, |
| 391 | timeout: int = 80, |
| 392 | fail_keywords: list[str] | None = None, |
| 393 | expect_keywords: list[str] | None = None, |
| 394 | stream: bool = False, |
| 395 | input_on_trigger: str | None = None, |
| 396 | device_error_keywords: list[str] | None = None, |
| 397 | stop_keyword: str | None = None, |
| 398 | json_rpc_commands: list[dict[str, Any]] | None = None, |
| 399 | use_pyserial: bool = False, |
| 400 | ) -> MonitorResult: |
| 401 | """Attach to serial monitor and capture output. |
| 402 | |
| 403 | Keyword Behavior: |
| 404 | --expect: Monitors until timeout, then checks if ALL keywords were found. |
| 405 | Exit 0 if all found, exit 1 if any missing. |
| 406 | --fail-on: Terminates immediately when ANY keyword is found, exits 1. |
| 407 | --stop: Terminates immediately when keyword is found, exits 0 if all expect patterns found. |
| 408 | --input-on-trigger: Wait for trigger pattern, then send text to serial. |
| 409 | |
| 410 | Args: |
| 411 | build_dir: Project directory containing platformio.ini |
| 412 | environment: PlatformIO environment to monitor (None = default) |
| 413 | monitor_port: Serial port to monitor (None = auto-detect) |
| 414 | verbose: Enable verbose output |
| 415 | timeout: Maximum time to monitor in seconds (default: 60) |
| 416 | fail_keywords: List of regex patterns that trigger immediate termination + exit 1 (default: [r"\bERROR\b"]) |
| 417 | expect_keywords: List of regex patterns that must ALL be found by timeout for exit 0 |
| 418 | stream: If True, monitor runs indefinitely until Ctrl+C (ignores timeout) |
| 419 | input_on_trigger: Format "PATTERN:TEXT" - sends TEXT when PATTERN is detected |
| 420 | device_error_keywords: List of error keywords in serial exceptions that indicate device stuck |
| 421 | (default: ["ClearCommError", "PermissionError"]) |
| 422 | stop_keyword: Regex pattern that triggers early successful exit if all expect patterns found |
| 423 | json_rpc_commands: List of JSON-RPC commands to send to device at startup (before trigger) |
| 424 | use_pyserial: If True, use pyserial directly instead of fbuild SerialMonitor |
| 425 | |
| 426 | Returns: |
| 427 | Tuple of (success, output_lines, json_rpc_handler) |
| 428 | """ |
| 429 | if fail_keywords is None: |
| 430 | fail_keywords = [] |
| 431 | if expect_keywords is None: |
| 432 | expect_keywords = [] |
| 433 | if device_error_keywords is None: |
| 434 | device_error_keywords = ["ClearCommError", "PermissionError"] |
| 435 | if json_rpc_commands is None: |
| 436 | json_rpc_commands = [] |
| 437 | |
| 438 | # Initialize JSON-RPC handler |
| 439 | rpc_handler = JsonRpcHandler() |
| 440 | rpc_commands_sent = False # Track if RPC commands have been sent |
| 441 | rpc_responses_needed = len(json_rpc_commands) # Number of RPC responses to wait for |
| 442 | rpc_responses_received = 0 |
| 443 |
no test coverage detected