can

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

CAN — 嵌入式 CAN / CAN-FD 调试工具

CAN — Embedded CAN/CAN-FD Debugging Tool

统一封装接口发现、实时监控、报文发送、日志记录、数据库文件解码和统计分析能力。
Unified encapsulation of interface discovery, real-time monitoring, message transmission, log recording, database file decoding, and statistical analysis capabilities.

配置

Configuration

环境级配置 (
skill/config.json
)

Environment-level Configuration (
skill/config.json
)

仅保留 slcan 相关的环境级配置:
json
{
  "slcan_serial_port": "",
  "slcan_serial_baudrate": 115200
}
字段说明默认值
slcan_serial_port
slcan 场景的串口
""
slcan_serial_baudrate
slcan 场景的串口速率
115200
Only retain slcan-related environment-level configurations:
json
{
  "slcan_serial_port": "",
  "slcan_serial_baudrate": 115200
}
FieldDescriptionDefault Value
slcan_serial_port
Serial port for slcan scenario
""
slcan_serial_baudrate
Serial baud rate for slcan scenario
115200

工程级配置 (
.embeddedskills/config.json
)

Project-level Configuration (
.embeddedskills/config.json
)

工作区下的
.embeddedskills/config.json
存放工程级 CAN 配置:
json
{
  "can": {
    "interface": "",
    "channel": "",
    "bitrate": 500000,
    "data_bitrate": 2000000,
    "log_dir": ".embeddedskills/logs/can"
  }
}
字段说明默认值
interface
CAN 后端,如
pcan
/
vector
/
slcan
""
channel
通道名,如
PCAN_USBBUS1
""
bitrate
仲裁域比特率
500000
data_bitrate
CAN-FD 数据域比特率
2000000
log_dir
日志输出目录
.embeddedskills/logs/can
The
.embeddedskills/config.json
in the workspace stores project-level CAN configurations:
json
{
  "can": {
    "interface": "",
    "channel": "",
    "bitrate": 500000,
    "data_bitrate": 2000000,
    "log_dir": ".embeddedskills/logs/can"
  }
}
FieldDescriptionDefault Value
interface
CAN backend, e.g.,
pcan
/
vector
/
slcan
""
channel
Channel name, e.g.,
PCAN_USBBUS1
""
bitrate
Arbitration field bit rate
500000
data_bitrate
CAN-FD data field bit rate
2000000
log_dir
Log output directory
.embeddedskills/logs/can

参数解析优先级

Parameter Parsing Priority

  1. CLI 参数 (
    --interface
    ,
    --channel
    ,
    --bitrate
    等) - 最高优先级
  2. 工程级配置 (
    .embeddedskills/config.json
    中的
    can
    部分)
  3. 状态文件 (
    .embeddedskills/state.json
    中的历史记录)
  4. 默认值 - 最低优先级
  1. CLI Parameters (
    --interface
    ,
    --channel
    ,
    --bitrate
    , etc.) - Highest priority
  2. Project-level Configuration (the
    can
    section in
    .embeddedskills/config.json
    )
  3. State File (historical records in
    .embeddedskills/state.json
    )
  4. Default Values - Lowest priority

自动扫描行为

Automatic Scanning Behavior

当未指定
interface
channel
时,脚本会自动扫描系统 CAN 接口:
  • 若只找到一个接口,自动使用并写入工程配置
  • 若找到多个接口,返回候选列表让用户选择
  • 若未找到接口,提示错误
When
interface
and
channel
are not specified, the script will automatically scan system CAN interfaces:
  • If only one interface is found, it will be used automatically and written to the project configuration
  • If multiple interfaces are found, a candidate list will be returned for the user to select
  • If no interface is found, an error will be prompted

子命令

Subcommands

子命令用途风险
scan
扫描可用 CAN 接口与 USB-CAN 设备
monitor
实时监控总线报文
send
发送标准帧 / 扩展帧 / 远程帧 / CAN-FD 帧
log
记录总线报文到 ASC / BLF / CSV 文件
decode
用 DBC 等数据库文件解码报文或日志
stats
统计总线负载、ID 分布和帧率
SubcommandPurposeRisk
scan
Scan available CAN interfaces and USB-CAN devicesLow
monitor
Real-time monitoring of bus messagesLow
send
Send standard/extended/remote/CAN-FD framesHigh
log
Record bus messages to ASC/BLF/CSV filesLow
decode
Decode messages or logs using database files like DBCLow
stats
Statistics on bus load, ID distribution, and frame rateLow

执行流程

Execution Flow

  1. 检查
    python-can
    是否可用,未安装时提示
    pip install python-can
  2. 按优先级解析参数:CLI > 工程级配置 > 状态文件 > 默认值
  3. 无子命令时默认执行
    scan
  4. monitor / send / log / stats
    使用解析后的连接参数
  5. decode
    先确认数据库文件和输入源存在
  6. 若未指定
    interface
    /
    channel
    ,自动扫描系统 CAN 接口:
    • 唯一候选:自动使用并写入工程配置
    • 多候选:返回列表让用户选择
  7. 成功执行后,将确认的参数写回工程配置
  8. send
    只要配置可连接就直接执行,不二次确认
  9. 运行对应脚本并输出结构化结果
  10. 失败时优先反馈接口、驱动、比特率和过滤条件问题
  1. Check if
    python-can
    is available; prompt
    pip install python-can
    if not installed
  2. Parse parameters by priority: CLI > Project-level Configuration > State File > Default Values
  3. Execute
    scan
    by default when no subcommand is specified
  4. monitor / send / log / stats
    use the parsed connection parameters
  5. decode
    first confirms the existence of the database file and input source
  6. If
    interface
    /
    channel
    are not specified, automatically scan system CAN interfaces:
    • Unique candidate: automatically use and write to project configuration
    • Multiple candidates: return list for user selection
  7. After successful execution, write the confirmed parameters back to the project configuration
  8. send
    executes directly as long as the configuration is connectable, no secondary confirmation required
  9. Run the corresponding script and output structured results
  10. When failed, prioritize feedback on interface, driver, bit rate, and filter condition issues

脚本调用

Script Invocation

所有脚本位于 skill 目录的
scripts/
下,通过
python
直接调用。 脚本会按优先级从 CLI 参数、工程级配置、状态文件中读取参数。
bash
undefined
All scripts are located in the
scripts/
directory under the skill directory, and can be called directly via
python
. Scripts read parameters from CLI parameters, project-level configuration, and state file by priority.
bash
undefined

扫描接口

Scan interfaces

python scripts/can_scan.py [--json]
python scripts/can_scan.py [--json]

实时监控

Real-time monitoring

python scripts/can_monitor.py [--interface <接口>] [--channel <通道>] [--bitrate <速率>] [--fd] [--filter-id <ID列表>] [--exclude-id <ID列表>] [--dbc <DBC文件>] [--timeout <秒>] [--json]
python scripts/can_monitor.py [--interface <interface>] [--channel <channel>] [--bitrate <bitrate>] [--fd] [--filter-id <ID list>] [--exclude-id <ID list>] [--dbc <DBC file>] [--timeout <seconds>] [--json]

发送报文

Send messages

python scripts/can_send.py [--interface <接口>] [--channel <通道>] [--bitrate <速率>] <id> <data> [--extended] [--remote] [--fd] [--repeat <次>] [--interval <秒>] [--periodic <毫秒>] [--listen] [--json]
python scripts/can_send.py [--interface <interface>] [--channel <channel>] [--bitrate <bitrate>] <id> <data> [--extended] [--remote] [--fd] [--repeat <times>] [--interval <seconds>] [--periodic <milliseconds>] [--listen] [--json]

日志记录

Log recording

python scripts/can_log.py [--interface <接口>] [--channel <通道>] [--bitrate <速率>] [--output <文件>] [--duration <秒>] [--max-count <数量>] [--filter-id <ID列表>] [--console] [--json]
python scripts/can_log.py [--interface <interface>] [--channel <channel>] [--bitrate <bitrate>] [--output <file>] [--duration <seconds>] [--max-count <number>] [--filter-id <ID list>] [--console] [--json]

数据库解码

Database decoding

python scripts/can_decode.py <db_file> [--db-format <auto|dbc|arxml|kcd|sym|cdd>] [--id <CAN_ID>] [--data <HEX数据>] [--log <日志文件>] [--signal <信号名>] [--list] [--json]
python scripts/can_decode.py <db_file> [--db-format <auto|dbc|arxml|kcd|sym|cdd>] [--id <CAN_ID>] [--data <HEX data>] [--log <log file>] [--signal <signal name>] [--list] [--json]

总线统计

Bus statistics

python scripts/can_stats.py [--interface <接口>] [--channel <通道>] [--bitrate <速率>] [--duration <秒>] [--top <数量>] [--watch <ID列表>] [--json]
undefined
python scripts/can_stats.py [--interface <interface>] [--channel <channel>] [--bitrate <bitrate>] [--duration <seconds>] [--top <number>] [--watch <ID list>] [--json]
undefined

输出格式

Output Format

单次命令返回标准 JSON:
json
{
  "status": "ok",
  "action": "scan",
  "summary": "发现 2 个 CAN 接口",
  "details": { ... }
}
持续命令(monitor --json、send --listen --json)输出 JSON Lines,结束摘要写入 stderr。
错误输出:
json
{
  "status": "error",
  "action": "send",
  "error": { "code": "interface_open_failed", "message": "无法打开指定 CAN 接口" }
}
Single command returns standard JSON:
json
{
  "status": "ok",
  "action": "scan",
  "summary": "Found 2 CAN interfaces",
  "details": { ... }
}
Continuous commands (monitor --json, send --listen --json) output JSON Lines, and the end summary is written to stderr.
Error output:
json
{
  "status": "error",
  "action": "send",
  "error": { "code": "interface_open_failed", "message": "Failed to open the specified CAN interface" }
}

核心规则

Core Rules

  • 不自动猜测 interface、channel、bitrate,多接口时不自动选择
  • 参数解析优先级:CLI > 工程级配置 > 状态文件 > 默认值
  • 未指定
    interface
    /
    channel
    时自动扫描,唯一候选自动写入配置,多候选需用户选择
  • 成功执行后,确认的参数自动写回
    .embeddedskills/config.json
  • 未明确说明用途时不主动发送任何报文
  • --json
    输出的持续流使用 JSON Lines,摘要写 stderr 不污染数据流
  • DBC 解码失败不应导致监控中断
  • 找不到帧定义时返回明确错误,不静默吞掉
  • Do not automatically guess interface, channel, or bitrate; do not automatically select when multiple interfaces exist
  • Parameter parsing priority: CLI > Project-level Configuration > State File > Default Values
  • Automatically scan when
    interface
    /
    channel
    are not specified; automatically write the unique candidate to configuration, require user selection for multiple candidates
  • After successful execution, automatically write confirmed parameters back to
    .embeddedskills/config.json
  • Do not actively send any messages without clear instructions on usage
  • Continuous streams output via
    --json
    use JSON Lines, with summaries written to stderr to avoid polluting the data stream
  • DBC decoding failure should not interrupt monitoring
  • Return clear errors when frame definitions are not found, do not silently discard them

参考

References

  • references/common_interfaces.json
    :常见 USB-CAN 设备信息
  • references/common_interfaces.json
    : Information on common USB-CAN devices