agentsclimarketplace

将串口通信代码重构为modbus rtu协议

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt3.5_8/将串口通信代码重构为modbus-rtu协议

将基于原始串口通信的数据采集脚本转换为使用PyModbus库的Modbus RTU协议,保留SQLite数据库存储逻辑和特定的寄存器读取功能。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill 将串口通信代码重构为modbus-rtu协议

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

2.5 KB, 654 tokens by cl100k_base, as published. Nobody here has run it

将串口通信代码重构为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

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.