polarimeter_software/User/app/mb_hmi/README.md
2025-09-30 10:37:23 +08:00

153 lines
5.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 旋光仪Modbus通信协议实现
## 概述
本目录包含基于nanomodbus库实现的旋光仪Modbus通信协议代码。该实现完全遵循`旋光仪Modbus通信协议规范.md`中的定义,提供了完整的寄存器映射、命令处理和数据同步功能。
## 文件结构
```
mb_hmi/
├── mb_regs_def.h # 寄存器地址和命令码定义
├── mb_command.h # 命令处理模块头文件
├── mb_command.c # 命令处理模块实现
├── mb_server.h # Modbus服务器模块头文件
├── mb_server.c # Modbus服务器模块实现
├── mb_interface.h # 用户接口头文件
├── mb_interface.c # 用户接口实现
└── CMakeLists.txt # 构建配置文件
```
## 主要功能
### 1. 寄存器映射
- 保持寄存器 (3xxxx):可读写参数,如控制命令、测量模式、目标温度等
- 输入寄存器 (4xxxx):只读数据,如实时温度、旋光度、测量结果等
### 2. 命令处理
- 系统控制命令:设备初始化、开始/停止测量、数据清零、开始/停止控温
- 参数设置:测量模式、目标温度、旋光管长度、测量精度、自动测量次数
- 数据查询:实时数据、测量结果、设备状态、历史数据
### 3. 用户接口
提供了丰富的接口函数方便用户与Modbus模块交互
#### 数据写入接口
- `mb_write_current_temp(float temp)`: 写入当前温度数据
- `mb_write_current_rotation(float rotation)`: 写入当前旋光度数据
- `mb_write_measurement_result(uint8_t mode, float value, float temp)`: 写入测量结果
#### 参数获取接口
- `mb_get_target_temp()`: 获取目标温度设置
- `mb_get_tube_length()`: 获取旋光管长度设置
- `mb_get_measure_mode()`: 获取测量模式
- `mb_get_auto_measure_count()`: 获取自动测量次数
- `mb_get_running_state()`: 获取运行状态
- `mb_is_measure_complete()`: 检查测量是否完成
## 用户需要实现的部分
### 1. 硬件初始化操作
在`system_ctrl_cmd_handler`函数中,需要实现设备初始化的具体逻辑:
```c
case 0x0001: // 设备初始化
// TODO: 用户需要实现硬件初始化操作(如传感器、外设复位)
...
```
### 2. 测量流程控制
在`system_ctrl_cmd_handler`函数中,需要实现开始/停止测量的具体逻辑:
```c
case 0x0002: // 开始测量
// TODO: 用户需要实现启动测量流程(控制测量模块)
...
case 0x0003: // 停止测量
// TODO: 用户需要实现停止测量流程
...
```
### 3. 温度控制功能
在`system_ctrl_cmd_handler`函数中,需要实现温度控制的具体逻辑:
```c
case 0x0005: // 开始控温
// TODO: 用户需要实现启动温度控制模块
...
case 0x0006: // 停止控温
// TODO: 用户需要实现停止温度控制模块
...
```
### 4. 历史数据时间戳
在`mb_save_to_history`函数中,需要实现真实的时间戳获取:
```c
uint32_t timestamp = 0; // TODO: User needs to implement actual timestamp
```
### 5. 数据清零操作
在`system_ctrl_cmd_handler`函数中,需要完善数据清零的具体逻辑:
```c
case 0x0004: // 数据清零
// TODO: 用户需要实现清除测量数据
...
```
## 数据同步机制
系统提供了自动的数据同步机制当用户通过接口函数写入数据后相应的Modbus寄存器会自动更新。主要的同步函数包括
- `mb_sync_realtime_temp()`: 同步实时温度数据
- `mb_sync_realtime_rotation()`: 同步实时旋光度数据
- `mb_sync_result()`: 同步测量结果数据
- `mb_sync_running_status_reg()`: 同步设备状态
- `mb_sync_exception_reg()`: 同步报警状态
- `mb_sync_running_state()`: 同步运行状态
## 使用示例
### 初始化Modbus系统
```c
#include "hmi_server.h"
int main(void)
{
// 初始化Modbus服务器
if (hmi_server_init() != 0) {
// 初始化失败处理
return -1;
}
// 主循环
while (1) {
// 应用程序代码
// 写入当前温度示例
float current_temp = 25.5; // 假设从传感器读取的温度
mb_write_current_temp(current_temp);
// 写入测量结果示例
if (measurement_done) {
uint8_t mode = 0; // 旋光度模式
float value = 1.69; // 测量值
float temp_at_meas = 25.5; // 测量时的温度
mb_write_measurement_result(mode, value, temp_at_meas);
}
// 检查Modbus设置的参数示例
float target_temp = mb_get_target_temp();
if (target_temp != current_temp_setpoint) {
// 更新温度控制目标
current_temp_setpoint = target_temp;
// 调整温度控制...
}
// 延时或其他操作
...
}
}
```
## 注意事项
1. 所有函数和变量命名遵循小写+下划线的代码风格
2. 温度范围限制为-10℃到100℃
3. 旋光管长度必须大于0
4. 自动测量次数范围为1-255
5. 历史数据最多存储4组
6. 浮点数传输采用大端模式高16位在前低16位在后