import os import subprocess # 设置环境变量 os.environ["IDF_PATH"] = r"D:\esp\v5.3.2\esp-idf" # 参数配置 python_exe = r"D:\esp\.espressif\python_env\idf5.3_py3.11_env\Scripts\python.exe" idf_monitor = os.path.join(os.environ["IDF_PATH"], "tools", "idf_monitor.py") port = "COM13" baudrate = "115200" toolchain_prefix = "riscv32-esp-elf-" make_cmd = f"'{python_exe}' '{os.path.join(os.environ['IDF_PATH'], 'tools', 'idf.py')}'" target = "esp32c3" elf_file = r"d:\esp\WorkSpace\esp32c3-usb_serial\build\usb_serial_jtag_echo.elf" # 构造命令行 cmd = [ python_exe, idf_monitor, "-p", port, "-b", baudrate, "--toolchain-prefix", toolchain_prefix, "--make", make_cmd, "--target", target, elf_file ] print("运行命令: ", " ".join(cmd)) # 运行命令 subprocess.run(cmd)