meter-protocol-serial

Original:🇨🇳 Chinese
Translated
11 scripts

698/645 electricity meter protocol serial port frame sending and parsing Skill, supporting frame assembly, sending, receiving, parsing and assertion verification, used for fast regression verification after bug fixing

61installs
Added on

NPX Install

npx skill4agent add ruiwarn/skills meter-protocol-serial

Tags

Translated version includes tags in frontmatter

SKILL.md Content (Chinese)

View Translation Comparison →

meter-protocol-serial

698/645 Electricity Meter Protocol Serial Port Frame Sending and Parsing Skill

Trigger Conditions

This skill is triggered when the user's requirement involves any of the following keywords:
  • 698
    、
    645
    、
    DL/T698
    、
    DL/T645
  • OAD
    、
    DI
  • serial frame sending
    、
    frame assembly
    、
    COM port
  • protocol debugging
    、
    regression test
  • frame sending verification
    、
    read meter data
    、
    write meter parameters

Function Overview

This skill provides unified frame assembly, sending, receiving and parsing capabilities for 698/645 electricity meter protocols:
  1. Frame-only mode (no port parameter): Output the assembled request frame in hexadecimal
  2. Serial port closed-loop mode (with port parameter): Send request, receive response, parse result

Usage

Quick Invocation

Linux / macOS / Native Python

bash
# 645读数据(只组帧)
python3 $SKILL_DIR/scripts/protocol_cli.py proto=645 op=read di=00010000

# 645读数据(串口发送)
python3 $SKILL_DIR/scripts/protocol_cli.py port=/dev/ttyUSB0 proto=645 op=read di=00010000

# 698读数据
python3 $SKILL_DIR/scripts/protocol_cli.py proto=698 op=read oad=40010200

# 698设置参数
python3 $SKILL_DIR/scripts/protocol_cli.py proto=698 op=set oad=43000300 value=bool:true

Windows / WSL (Important Differences)

When using Windows serial port in WSL, it is recommended to use the Python wrapper script, which will automatically convert to
python.exe
+ Windows path:
bash
# WSL中访问Windows COM口(推荐方式:使用 Python 包装脚本)
python3 $SKILL_DIR/meter-cmd.py port=COM10 proto=645 op=read di=04000401

# 或者手动转换路径
SKILL_PATH=$(wslpath -w /home/xx/.agents/skills/meter-protocol-serial/scripts/protocol_cli.py)
python.exe "$SKILL_PATH" port=COM10 proto=645 op=read di=04000401
Common Errors:
bash
# 错误方式1(WSL Python无法访问Windows串口)
python3 protocol_cli.py port=COM10 ...  # 会报错: No such file or directory

# 错误方式2(Windows Python无法直接访问WSL路径)
python.exe /home/xx/.../protocol_cli.py  # 会报错: can't open file
WSL Serial Port Mapping:
WindowsWSL Device
COM1/dev/ttyS1
COM10/dev/ttyS10
Windows Native PowerShell/CMD:
powershell
# Windows直接运行(PowerShell)
python.exe C:\path\to\scripts\protocol_cli.py port=COM10 proto=645 op=read di=04000401

# Windows直接运行(CMD)
python C:\path\to\scripts\protocol_cli.py port=COM10 proto=645 op=read di=04000401

Regression Test with Assertion

bash
# Linux/WSL通用(只组帧,无串口)
python3 $SKILL_DIR/scripts/protocol_cli.py proto=645 op=read di=00010000 expect=ack

# WSL+Windows串口(推荐直接走包装脚本)
python3 $SKILL_DIR/meter-cmd.py port=COM3 proto=645 op=read di=00010000 expect=ack

Supported Protocols

ProtocolOperationDescription
DL/T645-2007readRead data
DL/T645-2007writeWrite data
DL/T698.45readGET.request.normal
DL/T698.45setSET.request.normal

Input Parameters

General Parameters

ParameterDescriptionExample
protoProtocol type645 or 698
opOperation typeread / write / set
portSerial port (optional)/dev/ttyUSB0 or COM3
timeout_msTimeout duration2000
baudBaud rate2400 (default for 645), 9600 (default for 698)
data_bitsData bits8
parityParity biteven / odd / none
stop_bitsStop bit1
expectAssertion conditionack / hex:0102 / bool:true / int:1
decode_hintDecoding hinthex / ascii / uint16_le / uint32_le / bcd
noteNoteAny text

645 Dedicated Parameters

ParameterDescriptionExample
diData identifier (8-bit hexadecimal)00010000
addrMeter address (12-bit BCD)000000000001
valueWrite data valuehex:010203 / ascii:abc
fe_countNumber of leading FE bytes4
raw_prefixPrefix datahex:041234
raw_suffixSuffix datahex:0000

698 Dedicated Parameters

ParameterDescriptionExample
oadObject Attribute Descriptor (8-bit hexadecimal)40010200
server_addrServer address000000000000
client_addrClient address00
caClient address (abbreviated)00
valueWrite data valuebool:true / int32:123 / string:abc

Value Format

645 Protocol:
  • hex:01020304
    - Raw hexadecimal data
  • ascii:abc
    - ASCII string
698 Protocol:
  • bool:true|false
    - Boolean value
  • int8:1
    ,
    int16:1
    ,
    int32:1
    - Signed integer
  • uint8:1
    ,
    uint16:1
    ,
    uint32:1
    - Unsigned integer
  • enum:1
    - Enum value
  • octet:112233
    - Octet string (with length prefix)
  • string:abc
    - Visible string (with length prefix)
  • hex:010203
    - Raw hexadecimal (directly used as Data)

Output Format

MODE=frame_only|serial_roundtrip
PROTO=645|698
OP=read|write|set
TARGET=di:xxx|oad:xxx
REQUEST_HEX=68 AA AA ... CS 16
RESPONSE_HEX=68 BB BB ... CS 16  (串口模式)
FRAME_CHECK=ok|fail|partial
DECODE_STATUS=ok|partial|fail
RESULT=success|error|timeout
DATA_HEX=01020304
DATA_TYPED=typed_value  (如果有)
ASSERT_RESULT=pass|fail|skipped
ASSERT_REASON=...  (如果有)
SUMMARY=简短中文总结

645 Specific Output

645_ADDR=000000000001
645_CTRL=91
645_DI=00010000

698 Specific Output

698_SERVER_ADDR=45AAAAAAAAAAAA  (地址特征字节+6字节地址)
698_CLIENT_ADDR=00
698_CTRL=43  (控制域: DIR=0,PRM=1,分帧=0,扰码=0,功能码=3)
698_PIID=00
698_OAD=40010200
698_DAR=00  (数据访问结果,00=成功)

Exit Code

Code ValueMeaning
0Success
2Input parameter error
3Serial port opening or sending failure
4Timeout with no response
5Response received but parsing failed
6Assertion failed

AI Usage Suggestions

Response Order

When users use this skill, AI should reply in the following order:
  1. Execute command: Provide the actual executed CLI command
  2. Key output: Extract key fields such as REQUEST_HEX, RESULT, ASSERT_RESULT, etc.
  3. Brief explanation: Explain the meaning of the result and suggestions for next steps

Example Scenarios

Scenario 1: User says "Help me assemble a 645 data reading frame"
执行命令:
python3 $SKILL_DIR/scripts/protocol_cli.py proto=645 op=read di=00010000

输出:
MODE=frame_only
PROTO=645
OP=read
TARGET=di:00010000
REQUEST_HEX=FEFEFEFE6800000000000068110433333333AD16
...
SUMMARY=组帧成功: FEFEFEFE6800000000000068110433...
Scenario 2: User says "Verify if data can be read from COM3 port"
执行命令:
python3 $SKILL_DIR/scripts/protocol_cli.py port=COM3 proto=645 op=read di=00010000 expect=ack

输出:
MODE=serial_roundtrip
PROTO=645
OP=read
...
ASSERT_RESULT=pass
SUMMARY=断言通过: 收到响应

Notes

  1. port parameter is optional: When there is no port, it enters frame-only mode and will not open the serial port
  2. Default address:
    • 645 default address
      AAAAAAAAAAAA
      (12-bit BCD wildcard address)
    • 698 default server address
      AAAAAAAAAAAA
      (6-byte wildcard address, address feature byte = 0x45), client address
      00
  3. Default baud rate: 645 default 9600bps, 698 default 9600bps
  4. Timeout handling: Default timeout is 2000ms, can be adjusted via timeout_ms
  5. Assertion failure: When assert_result=fail, the process returns a non-zero exit code, which can be used for script automation

Module Structure

scripts/
├── protocol_cli.py      # CLI主入口
├── request_parser.py    # 请求解析
├── profiles.py          # 默认配置
├── serial_transport.py  # 串口收发
├── proto_645.py         # 645协议处理
├── proto_698.py         # 698协议处理
├── value_codec.py       # 值编解码
└── result_formatter.py  # 结果格式化

V1 Limitations

  • 698: Does not support login, link establishment, security authentication, secure transmission
  • 698: Does not support ACTION and ROAD
  • 645: No dedicated semantic parsing for various service DIs
  • Both protocols: Only support standard plaintext read and write, do not support manufacturer private extensions