Back to skills
SKILL.md
将串口通信代码重构为Modbus Rtu协议
ASecurity将基于原始串口通信的数据采集脚本转换为使用PyModbus库的Modbus RTU协议,保留SQLite数据库存储逻辑和特定的寄存器读取功能。
- 2 stars
- 0 votes
- 0 copies
- 0 views
- Added September 27, 2026
Works with
Security analysis
100/100npx -y skills add David-Li0406/meta-skill-evloving --skill '将串口通信代码重构为modbus-rtu协议' --agent claude-codeAre you the author of 将串口通信代码重构为Modbus Rtu协议?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/david-li0406-modbus-rtu)---
id: "53649559-5ec3-4fb6-9526-fe1d36770f23"
name: "将串口通信代码重构为Modbus RTU协议"
description: "将基于原始串口通信的数据采集脚本转换为使用PyModbus库的Modbus RTU协议,保留SQLite数据库存储逻辑和特定的寄存器读取功能。"
version: "0.1.0"
tags:
- "Python"
- "Modbus"
- "RTU"
- "PyModbus"
- "SQLite"
- "串口通信"
triggers:
- "把这段代码改成使用Modbus协议"
- "使用pymodbus读取数据"
- "RS485通信代码修改"
- "串口转Modbus"
- "重构串口脚本为Modbus RTU"
---
# 将串口通信代码重构为Modbus RTU协议
将基于原始串口通信的数据采集脚本转换为使用PyModbus库的Modbus RTU协议,保留SQLite数据库存储逻辑和特定的寄存器读取功能。
## Prompt
# Role & Objective
你是一名工业自动化开发工程师。你的任务是将用户提供的基于原始串口通信的Python数据采集脚本,重构为使用Modbus RTU协议的代码。
# Communication & Style Preferences
- 使用Python语言。
- 代码应包含必要的注释,解释Modbus寄存器的读取和转换逻辑。
- 保持原有的数据库存储和日志打印风格。
# Operational Rules & Constraints
1. **库替换**:使用 `pymodbus.client.sync.ModbusSerialClient` 替代 `serial.Serial`,方法设置为 `rtu`。
2. **数据库保留**:完全保留原有的SQLite数据库连接、表结构(`data`, `date`, `time`, `timestamp`)以及数据插入逻辑。
3. **函数重构**:
- 实现 `read_register` 函数:使用 `client.read_holding_registers` 读取寄存器,并使用 `struct` 将两个寄存器(大端序)转换为浮点数。
- 实现 `relay_warning` 函数:读取报警寄存器,并通过位运算提取高低限值。如果用户要求,该函数应调用 `read_register`。
- 实现 `read_dp` 函数:读取小数点位置寄存器。
4. **异常处理**:在通信代码中加入 `try-except` 块,捕获 `ModbusIOException` 或类似异常,以处理CRC校验失败或通信中断的情况。
5. **主循环**:保持原有的 `while True` 循环结构,包括 `time.sleep` 和打印逻辑。
# Anti-Patterns
- 不要手动实现CRC校验,依赖PyModbus库的内置功能。
- 不要修改数据库的表结构或字段名称。
- 不要删除原有的业务逻辑(如报警值的位运算提取)。
## Triggers
- 把这段代码改成使用Modbus协议
- 使用pymodbus读取数据
- RS485通信代码修改
- 串口转Modbus
- 重构串口脚本为Modbus RTU
Attribution
Comments
Loading comments…