AgentSkillsCN

COM Port Management

在对 ESP32 设备进行烧录或监控之前,务必确保串行 COM 端口处于可用状态。

SKILL.md
--- frontmatter
name: COM Port Management
description: Ensure the serial COM port is available before flashing or monitoring an ESP32 device

COM Port Management

When to Use

  • Before running idf.py flash or idf.py monitor
  • When flashing fails with PermissionError(13, 'Access is denied.') or could not open port 'COMx'
  • After a previous monitor session was interrupted or crashed

Step 1: Identify the COM Port

powershell
// turbo
[System.IO.Ports.SerialPort]::GetPortNames()

The board typically appears as COM5 (check Device Manager if unsure).

Step 2: Check for Processes Holding the Port

Stale Python processes from previous idf.py monitor sessions are the most common cause:

powershell
// turbo
Get-Process -Name python -ErrorAction SilentlyContinue | Select-Object Id, ProcessName, StartTime | Format-Table -AutoSize

Step 3: Kill Stale Processes

Kill Python processes older than a few minutes (these are leftover monitor/flash sessions):

powershell
Get-Process -Name python -ErrorAction SilentlyContinue | Where-Object { $_.StartTime -lt (Get-Date).AddMinutes(-5) } | Stop-Process -Force -PassThru | Select-Object Id, ProcessName

NOTE: Only kill processes older than 5 minutes to avoid killing active builds. Adjust the threshold as needed.

Step 4: Verify Port is Free

Try opening the port briefly:

powershell
// turbo
try {
    $port = New-Object System.IO.Ports.SerialPort "COM5", 115200
    $port.Open()
    $port.Close()
    Write-Host "COM5 is available"
} catch {
    Write-Host "COM5 is still locked: $($_.Exception.Message)"
}

Common Causes

SymptomCauseFix
PermissionError(13, 'Access is denied.')Previous idf.py monitor still runningKill stale Python processes
could not open portPort doesn't exist or device unpluggedCheck USB cable, run GetPortNames()
Port locked after crashesptool/monitor didn't clean upKill all Python processes, replug USB