add functions

This commit is contained in:
CSSC-WORK\murmur 2024-12-11 17:41:56 +08:00
parent b67eee24e7
commit 8369e578ef
2 changed files with 89 additions and 14 deletions

View File

@ -171,16 +171,21 @@ static void FillBigEndian16(uint8_t *data, uint16_t value) {
}
}
/*
+----------+--------+------------+------------+------------+
| | | | | CRC校验值 |
+----------+--------+------------+------------+------------+
| 1 | 1 | 2 | 2 | 2 |
+----------+--------+------------+------------+------------+
*/
// pump 读寄存器
uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) {
// 读取保存寄存器不能直接使用RTU_Frame因为无数据位
uint8_t data[8] = {0};
data[0] = index;
data[1] = RTU_PUMP_FUNC_READ_REG;
FillBigEndian16(&data[2], reg);
FillBigEndian16(&data[4], 2);
FillBigEndian16(&data[4], 1);
uint16_t crc = CalculateCRC16(data, 6);
// 小端序填充
@ -189,6 +194,14 @@ uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) {
writeCMD(data, 8);
}
/*
+----------+--------+------------+----------+------------+
| | | | | CRC校验值 |
+----------+--------+------------+----------+------------+
| 1 | 1 | 2 | 2 | 2 |
+----------+--------+------------+----------+------------+
*/
// 写泵1个寄存器
uint8_t WritePump1Reg(uint8_t index, uint16_t reg, uint16_t value) {
// 写一个寄存器不需要指定寄存器长度
@ -203,6 +216,14 @@ uint8_t WritePump1Reg(uint8_t index, uint16_t reg, uint16_t value) {
writeCMD(data, 8);
}
/*
+----------+--------+------------+------------+----------+----------+------------+
| | | | | | | CRC校验值 |
+----------+--------+------------+------------+----------+----------+------------+
| 1 | 1 | 2 | 2 | 1 | 4 | 2 |
+----------+--------+------------+------------+----------+----------+------------+
*/
// 写泵2个寄存器
uint8_t WritePump2Reg(uint8_t index, uint16_t reg, uint32_t value) {
// 写2个寄存器需要指定寄存器长度
@ -303,6 +324,14 @@ static uint8_t StopPump(uint8_t index) {
uint8_t ReadHWReg(uint8_t index) {
ReadPump1Reg(index, RTU_PUMP_CMD_HW);
}
// 设置波特率
uint8_t WriteBRReg(uint8_t index, uint16_t br) {
WritePump1Reg(index, RTU_PUMP_CMD_BR, br);
}
// 设置通信协议
uint8_t WritePRReg(uint8_t index, uint16_t pr) {
WritePump1Reg(index, RTU_PUMP_CMD_PR, pr);
}
// 获取泵状态
void ReadPumpStatus(uint8_t index) {

View File

@ -4,17 +4,19 @@
#include <stdint.h>
// 帧头帧尾定义
#define FRAME_HEADER 0xA55A5AA5
#define FRAME_TAIL 0x5AA5A55A
#define FRAME_HEADER 0xA55A5AA5
#define FRAME_TAIL 0x5AA5A55A
// 功能码定义
#define CMD_STATUS_QUERY 0x0001 // 状态查询
#define CMD_VALVE_CTRL 0x0002 // 三通阀控制
#define CMD_PUMP_RUN_TIME 0x0003 // 泵运行时长控制
#define CMD_STATUS_QUERY 0x0001 // 状态查询
#define CMD_VALVE_CTRL 0x0002 // 三通阀控制
#define CMD_PUMP_RUN_TIME 0x0003 // 泵运行时长控制
#define CMD_PUMP_RUN_SPEED 0x0004 // 泵运行速度设置
#define CMD_SOFT_STOP 0x0005 // 软急停功能
#define CMD_PUMP_RUN_STEP 0x0006 // 泵步进设置
#define CMD_SYSTEM_INIT 0x0007 // 系统初始化
#define CMD_SOFT_STOP 0x0005 // 软急停功能
#define CMD_PUMP_RUN_STEP 0x0006 // 泵步进设置
#define CMD_SYSTEM_INIT 0x0007 // 系统初始化
#define CMD_BR_SET 0x0008 // 波特率设置
#define CMD_PR_SET 0x0009 // 通信协议设置
// 命令帧错误码定义
typedef enum {
CMD_FRAME_OK = 0,
@ -30,9 +32,9 @@ typedef enum {
// 0x03读取保持寄存器
// 0x06写单个寄存器
// 0x10写多个寄存器
#define RTU_PUMP_FUNC_READ_REG 0x03 // 读取保持寄存器
#define RTU_PUMP_FUNC_WRITE_REG 0x06 // 写单个寄存器
#define RTU_PUMP_FUNC_WRITE_MULTI_REG 0x10 // 写多个寄存器
#define RTU_PUMP_FUNC_READ_REG 0x03 // 读取保持寄存器
#define RTU_PUMP_FUNC_WRITE_REG 0x06 // 写单个寄存器
#define RTU_PUMP_FUNC_WRITE_MULTI_REG 0x10 // 写多个寄存器
/*寄存器4001告警代码
@ -189,6 +191,9 @@ const uint8_t statusInfo[16][32]={
#define RTU_PUMP_CMD_VE 0x001D // 速度
#define RTU_PUMP_CMD_DI 0x001E // 目标位置
#define RTU_PUMP_CMD_HW 0x006C // 硬件版本40109
#define RTU_PUMP_CMD_BR 0x0094 //波特率BR40149
#define RTU_PUMP_CMD_PR 0x0095 //通信协议PR40150
// 速度控制慢跑Jogging模式
/*
@ -233,6 +238,47 @@ const uint8_t statusInfo[16][32]={
#define RTU_PUMP_CMD_SC 0x4002 // 状态寄存器
#define RTU_PUMP_CMD_AL 0x4001 // 告警寄存器
// valve
/*
+------------------------------------+------------+
| | |
+------------------------------------+------------+
| CIA402模式 | 00B1h |
+------------------------------------+------------+
| | 03C2h |
+------------------------------------+------------+
| 17 | 0416h |
| 18 | |
+------------------------------------+------------+
| 10000 | 0417h |
+------------------------------------+------------+
| 1000 | 0419h |
+------------------------------------+------------+
| 200000 | 041Bh |
+------------------------------------+------------+
| 使 | 0380h |
+------------------------------------+------------+
| 使 | 0380h |
+------------------------------------+------------+
| 使使 | 0380h |
+------------------------------------+------------+
| 使 | 0380h |
+------------------------------------+------------+
*/
#define RTU_VALVE_CMD_CTL_MODE 0x00B1 // 阀门控制模式
#define RTU_VALVE_CMD_RUN_MODE 0x03C2 // 阀门运行模式
#define RTU_VALVE_CMD_ORIGIN_MODE 0x0416 // 阀门原点回归模式
#define RTU_VALVE_CMD_ORIGIN_SPEED 0x0417 // 阀门原点回归速度
#define RTU_VALVE_CMD_ORIGIN_ACCEL 0x041B // 阀门原点回归加速度
#define RTU_VALVE_CMD_ENABLE 0x0380 // 阀门使能
#define RTU_VALVE_CMD_DISABLE 0x0380 // 阀门失能
#define RTU_VALVE_CMD_RUN_ORIGIN 0x0380 // 阀门运行原点回归
// 错误码定义
typedef enum {
ERR_NONE = 0x00,