add functions
This commit is contained in:
parent
b67eee24e7
commit
8369e578ef
35
protocol.c
35
protocol.c
@ -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) {
|
||||
|
68
protocol.h
68
protocol.h
@ -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 //波特率BR,40149
|
||||
#define RTU_PUMP_CMD_PR 0x0095 //通信协议PR,40150
|
||||
|
||||
|
||||
// 速度控制,慢跑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,
|
||||
|
Loading…
Reference in New Issue
Block a user