Skip to main content

一款用于自动化执行串口指令的命令行工具,支持多设备、多指令的串行和并行执行

Project description

AutoCom

一款用于自动化执行串口指令的命令行工具,支持多设备、多指令的串行和并行执行。

Cross Platform Serial Communication Multi-Device Automation PyPI


📦 安装

从 PyPI 安装(推荐)

pip install autocom

从 GitHub 直接安装

pip install git+https://github.com/iFishin/AutoCom.git

从源码安装

git clone https://github.com/iFishin/AutoCom.git
cd AutoCom
pip install -r requirements.txt
pip install -e .

🚀 快速开始

命令行使用

# 初始化项目结构(创建 dicts/、configs/、temps/ 目录及示例文件)
autocom --init

# 执行字典文件(循环3次)
autocom -d dicts/dict.yaml -l 3

# 无限循环模式(按 Ctrl+C 停止)
autocom -d dicts/dict.yaml -i

# 使用配置文件
autocom -d dicts/dict.yaml -c configs/config.yaml

# 执行文件夹内所有字典文件
autocom -f dicts/

# 监控模式(监听文件夹,自动执行新文件)
autocom -m temps/

Python API 使用

from autocom import CommandDeviceDict, CommandExecutor, CommonUtils

# 加载配置
dict_data = {...}  # 你的配置字典
device_dict = CommandDeviceDict(dict_data)

# 创建执行器
executor = CommandExecutor(device_dict)

# 执行指令
result = executor.execute()

# 清理资源
device_dict.close_all_devices()
executor.data_store.stop()

📁 项目结构

AutoCom/
├── components/         # 核心组件模块(Device、Logger、TablePrinter、CommandDeviceDict、DataStore、CommandExecutor)
├── utils/              # 工具类和辅助函数(ActionHandler、common、dirs)
├── tests/              # 测试文件
├── scripts/            # 构建和维护脚本(dev.py、update_actions_doc.py)
├── docs/               # 项目文档
├── dicts/              # 字典配置文件目录
├── configs/            # 设备配置文件目录
├── AutoCom.py          # 主程序入口
├── cli.py              # 命令行接口
├── version.py          # 版本信息
└── CHANGELOG.md        # 变更日志

📚 文档

文档 说明
docs/About.md 项目背景与设计理念
docs/Started.md 开发快速指南与发布流程
docs/Actions.md 所有 Action 操作项的详细说明
docs/DEV.md 开发工具使用说明
docs/ToDO.md 待办事项与未来计划
CHANGELOG.md 版本变更历史
docs/MCP.md MCP Server: AI Agent 接口

📖 格式框架详解(Devices、Commands、Actions 的 JSON 格式说明)参见 docs/About.md


🔧 核心功能

功能 说明
多设备支持 同时管理多个串口设备,支持不同配置
串行/并行执行 指令可按顺序执行或并行并发执行
Action 扩展系统 通过 ActionHandler 自定义指令成功/失败后的处理逻辑
配置覆盖机制 ConfigForDevices / ConfigForCommands 简化重复配置
常量和变量 Constants 支持用户输入变量,在指令参数中引用
文件夹遍历 批量执行文件夹内所有字典文件
监控模式 监听文件夹,新文件自动执行
持续日志监听 后台线程持续记录串口输出
MCP Server 为 AI Agent 提供串口操作接口,支持 Claude Desktop 等 MCP 客户端

🤖 MCP Server(AI Agent 接口)

AutoCom MCP Server 基于 Model Context Protocol (MCP) 实现,让 AI Agent(如 Claude Desktop、Cursor 等)能够直接控制串口设备。

安装

# 安装 MCP 依赖
pip install autocom[mcp]

启动

# Stdio 模式(默认,适合 Claude Desktop)
autocom mcp

# SSE (HTTP) 模式(适合远程调用)
autocom mcp --sse --port 8888

可用的 MCP 工具

工具名 描述 关键参数
list_devices 列出可用串口设备 无需参数
execute_command 发送单条指令并获取响应 port, command, baud_rate(可选)
execute_commands 批量执行多条指令 port, commands[], parallel(可选)
load_dict 加载 AutoCom 字典 JSON 配置 file_path, config_path(可选)
monitor_port 监听串口输出 port, duration(可选)

配置 Claude Desktop

claude_desktop_config.json 中添加:

{
  "mcpServers": {
    "autocom": {
      "command": "autocom",
      "args": ["mcp"]
    }
  }
}

SSE 模式

SSE 模式启动后可通过浏览器或 MCP Inspector 调试:

# 健康检查
http://localhost:8888/health

# MCP Inspector 调试
npx @modelcontextprotocol/inspector
# 连接地址: http://localhost:8888/mcp/sse

使用示例(Python API)

# 扫描设备
result = await client.call_tool("list_devices", {})

# 发送指令
result = await client.call_tool("execute_command", {
    "port": "COM3",
    "command": "AT+GMR",
    "baud_rate": 115200,
    "timeout": 5.0,
})

# 批量执行
result = await client.call_tool("execute_commands", {
    "port": "/dev/ttyUSB0",
    "commands": ["AT", "AT+GMR", "AT+CSQ"],
    "parallel": False,
})

🤝 贡献

欢迎提交 Issue 和 Pull Request!详见 CONTRIBUTING.md

📄 许可证

MIT License © 2025 iFishin

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autocom-1.2.2.tar.gz (79.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

autocom-1.2.2-py3-none-any.whl (80.4 kB view details)

Uploaded Python 3

File details

Details for the file autocom-1.2.2.tar.gz.

File metadata

  • Download URL: autocom-1.2.2.tar.gz
  • Upload date:
  • Size: 79.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for autocom-1.2.2.tar.gz
Algorithm Hash digest
SHA256 db599dfacae14a87cae9693e0eff003d3e8d49b32881bb8cfbf83a5209c539df
MD5 6f99cb9f18c6230e8c979fbc3d000336
BLAKE2b-256 a1e1cc1916946d666f36231b2712c6e866bb609ecdc2c62587685435f6fa70a8

See more details on using hashes here.

File details

Details for the file autocom-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: autocom-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 80.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for autocom-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d37de76c53b95ab3f34bf64eaed510726512d683cb7e74d1a7c4d86f85efaaf5
MD5 a85345c9e4d840808de88461806d2c35
BLAKE2b-256 bf938b5605716e6a8591884beb547c76a4e5186d7e3ade417a5a186fde074a36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page