diff --git a/protocol.c b/protocol.c index dbde0bc..fc1c7c1 100644 --- a/protocol.c +++ b/protocol.c @@ -1,7 +1,16 @@ #include "protocol.h" #include -static DeviceStatus deviceStatus = {0}; +static DeviceStatus deviceStatus = { + .deviceStatus = 1, + .valves = {210, 120}, + .pumps = {0, 0, 50, 50}, + .bubbleStatus = 0, + .stopStatus = 0, + .errorCode = 0, + .initStatus = 1 +}; + DeviceDefaultParam dp = {{ {"pump1", 1, 100, 100, 100}, {"pump2", 2, 100, 100, 100}, @@ -112,7 +121,7 @@ void updateInitStatus(InitStatus_t status) { //modBUS RTU 写命令 -void writeCMD(uint8_t *txBuf, uint16_t *txLen) { +void writeCMD(uint8_t *txBuf, uint16_t txLen) { } uint8_t SendToHost(uint8_t *txBuf, uint16_t txLen) { @@ -706,15 +715,68 @@ static uint8_t HandleSoftStop(uint8_t *rxBuf, uint16_t rxLen) { } return 0; } - +void ProcessMotorMsg(uint8_t *rxBuf, uint16_t rxLen) { + // 实现电机消息处理逻辑 + log_d("ProcessMotorMsg"); +} +void sendMsgToHost(uint8_t *txBuf, uint16_t txLen) { + // 发送数据 + HAL_UART_Transmit_DMA(&huart2, txBuf, txLen); +} +void packMsgToHost(uint16_t funcCode, uint8_t isOK) { + // 实现打包消息到上位机逻辑 + // 帧头+功能码(2Byte)+数据长度(1Byte)+ 具体数据(NByte)+CRC16校验位+帧尾 + uint8_t msgBuf[64];//最大为4+2+1+15+2+4=28 + uint8_t len = 0; + uint8_t dlen = 0; + uint8_t index = 0; + FillBigEndian32(msgBuf+4, FRAME_HEADER); + FillBigEndian16(msgBuf+sizeof(FRAME_HEADER), funcCode); + + if(funcCode == HOST_CMD_STATUS_QUERY) { + dlen = sizeof(DeviceStatus); + index = sizeof(FRAME_HEADER)+2; + msgBuf[index] = dlen; + index += 1; + memcpy(msgBuf+index, &deviceStatus, dlen); + index += dlen; + uint16_t crc = CalculateCRC16(msgBuf+4, index-4);//不包含帧头 + FillBigEndian16(msgBuf+index, crc); + index += 2; + FillBigEndian32(msgBuf+index, FRAME_TAIL); + len = index+4; + } + else { + dlen = 1; + index = sizeof(FRAME_HEADER)+2; + msgBuf[index] = dlen; + index += 1; + msgBuf[index] = isOK; + index += 1; + uint16_t crc = CalculateCRC16(msgBuf+4, index-4);//不包含帧头 + FillBigEndian16(msgBuf+index, crc); + index += 2; + FillBigEndian32(msgBuf+index, FRAME_TAIL); + len = index+4; + } + // 发送数据 + sendMsgToHost(msgBuf, len); +} CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint16_t rxLen) { // 检查命令是否正确 - if (memcmp(rxBuf, FRAME_HEADER, 4) != 0) + // FRAME_HEADER是按小端序存储的,而rxBuf是按大端序存储的 + uint32_t header; + FillBigEndian32(&header, FRAME_HEADER); + uint32_t tail; + FillBigEndian32(&tail, FRAME_TAIL); + + if(memcmp(rxBuf, &header, 4) != 0) { return CMD_FRAME_HEADER_ERROR; } - if (memcmp(rxBuf + rxLen - 4, FRAME_TAIL, 4) != 0) + + if (memcmp(rxBuf + rxLen - 4, &tail, 4) != 0) { return CMD_FRAME_TAIL_ERROR; } @@ -743,25 +805,25 @@ CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint16_t rxLen) { uint8_t *data = &rxBuf[sizeof(FRAME_HEADER)+3];//提取数据 switch(cmdCode) { - case CMD_STATUS_QUERY: + case HOST_CMD_STATUS_QUERY: error = HandleStatusQuery(); break; - case CMD_VALVE_CTRL: + case HOST_CMD_VALVE_CTRL: error = HandleValveControl(data, dataLen); break; - case CMD_PUMP_RUN_TIME: + case HOST_CMD_PUMP_RUN_TIME: error = HandlePumpTimeControl(data, dataLen); break; - case CMD_PUMP_RUN_SPEED: + case HOST_CMD_PUMP_RUN_SPEED: error = HandlePumpSpeedControl(data, dataLen); break; - case CMD_SOFT_STOP: + case HOST_CMD_SOFT_STOP: error = HandleSoftStop(data, dataLen); break; - case CMD_PUMP_RUN_STEP: - error = HandlePumpStep(data, dataLen); + case HOST_CMD_PUMP_RUN_STEP: + error = HandlePumpStepControl(data, dataLen); break; - case CMD_SYSTEM_INIT: + case HOST_CMD_SYSTEM_INIT: error = HandleInit(); break; default: diff --git a/protocol.h b/protocol.h index 2fa63b1..c2b22a6 100644 --- a/protocol.h +++ b/protocol.h @@ -1,6 +1,8 @@ #ifndef PROTOCOL_H #define PROTOCOL_H +#include "main.h" +#include "usart.h" #include #define LOG_TAG "motor" #include @@ -10,13 +12,13 @@ #define FRAME_TAIL 0x5AA5A55A // 功能码定义 -#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 HOST_CMD_STATUS_QUERY 0x0001 // 状态查询 +#define HOST_CMD_VALVE_CTRL 0x0002 // 三通阀控制 +#define HOST_CMD_PUMP_RUN_TIME 0x0003 // 泵运行时长控制 +#define HOST_CMD_PUMP_RUN_SPEED 0x0004 // 泵运行速度设置 +#define HOST_CMD_SOFT_STOP 0x0005 // 软急停功能 +#define HOST_CMD_PUMP_RUN_STEP 0x0006 // 泵步进设置 +#define HOST_CMD_SYSTEM_INIT 0x0007 // 系统初始化 #define CMD_BR_SET 0x0008 // 波特率设置 #define CMD_PR_SET 0x0009 // 通信协议设置 // 命令帧错误码定义 @@ -379,24 +381,8 @@ typedef struct { } DeviceStatus; -static uint8_t pumpName[2][10] = { - "Pump1", - "Pump2" -}; - -// 定义协议消息结构 typedef struct { - uint8_t device_id; - uint8_t func; - uint8_t reg_addr[2]; - uint8_t reg_cnt[2]; - uint8_t data_cnt; - uint8_t data[];//柔性数组,大小由data_cnt决定 - // uint8_t crc[2]; -} RTU_Frame; - -typedef struct { - uint8_t name[10]; + uint8_t name[20]; uint8_t id; uint32_t maxSpeed; uint32_t maxAccel; @@ -420,4 +406,5 @@ void InitDeviceStatus(); void DecodePumpAlarmMsg(uint16_t reg4001); void DecodePumpStatusMsg(uint16_t reg4002); void UpdatePumpStatus(); +void ProcessMotorMsg(uint8_t *rxBuf, uint16_t rxLen); #endif // PROTOCOL_H \ No newline at end of file