From f18bded566706ce417e63d335e24a4ddcbc944ce Mon Sep 17 00:00:00 2001 From: murmur Date: Thu, 19 Dec 2024 22:49:20 +0800 Subject: [PATCH] update crc --- protocol.c | 51 ++++++++++++++++++++++++++++++++++++++------------- protocol.h | 3 ++- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/protocol.c b/protocol.c index 565af44..b975a58 100644 --- a/protocol.c +++ b/protocol.c @@ -12,8 +12,8 @@ static DeviceStatus deviceStatus = { }; DeviceParam dp = {{ - {"pump1", 1, 100, 100, 100}, - {"pump2", 2, 100, 100, 100}, + {"pump1", 4, 100, 100, 100}, + {"pump2", 4, 100, 100, 100}, {"valve1", 3, 100, 100, 100}, {"valve2", 4, 100, 100, 100} }}; @@ -57,7 +57,7 @@ static const uint16_t crcTable[] = { }; /** - * 计算数据的CRC16校验值 + * 计算数据的CRC16校验值,CRC16校验多项式为:X16+X15+X2+1 * * @param data 需要计算CRC的数据缓冲区 * @param length 数据长度 @@ -172,6 +172,7 @@ void updateInitStatus(InitStatus_t status) { * @param txLen 发送数据长度 */ void writeCMD(uint8_t *txBuf, uint16_t txLen) { + HAL_UART_Transmit(&huart2, txBuf, txLen, 1000); } /** @@ -183,7 +184,8 @@ void writeCMD(uint8_t *txBuf, uint16_t txLen) { */ void sendMsgToHost(uint8_t *txBuf, uint16_t txLen) { // 发送数据 - HAL_UART_Transmit_DMA(&huart2, txBuf, txLen); + // HAL_UART_Transmit_DMA(&huart1, txBuf, txLen); + transDataToHost(txBuf, txLen); } @@ -917,10 +919,8 @@ static uint8_t HandleInit(void) { * @return 0:成功 其他:失败 */ static uint8_t HandleStatusQuery(void) { - // 填充并返回数据 - uint8_t txBuf[sizeof(DeviceStatus)] = {0}; - memcpy(txBuf, &deviceStatus, sizeof(DeviceStatus)); - sendMsgToHost(txBuf, sizeof(txBuf)); + + packMsgToHost(HOST_CMD_STATUS_QUERY, 0); return 0; } @@ -1088,7 +1088,7 @@ void packMsgToHost(uint16_t funcCode, uint8_t isOK) { uint8_t len = 0; uint8_t dlen = 0; uint8_t index = 0; - FillBigEndian32(msgBuf+4, FRAME_HEADER); + FillBigEndian32(msgBuf, FRAME_HEADER); FillBigEndian16(msgBuf+sizeof(FRAME_HEADER), funcCode); if(funcCode == HOST_CMD_STATUS_QUERY) { @@ -1128,7 +1128,7 @@ void packMsgToHost(uint16_t funcCode, uint8_t isOK) { * @param rxLen 接收到的数据长度 * @return 命令帧错误码 */ -CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint16_t rxLen) { +CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint8_t rxLen) { // 检查命令是否正确 // FRAME_HEADER是按小端序存储的,而rxBuf是按大端序存的 uint8_t header[sizeof(FRAME_HEADER)]; @@ -1138,16 +1138,19 @@ CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint16_t rxLen) { if(memcmp(rxBuf, header, sizeof(FRAME_HEADER)) != 0) { + printf("CMD_FRAME_HEADER_ERROR\r\n"); return CMD_FRAME_HEADER_ERROR; } if (memcmp(rxBuf + rxLen - sizeof(FRAME_TAIL), tail, sizeof(FRAME_TAIL)) != 0) { + printf("CMD_FRAME_TAIL_ERROR\r\n"); return CMD_FRAME_TAIL_ERROR; } - uint16_t crc = CalculateCRC16(rxBuf+sizeof(FRAME_HEADER), rxLen - sizeof(FRAME_HEADER)-sizeof(FRAME_TAIL))-2;// 计算crc,不包含帧头和帧尾和crc自身 - if ((rxBuf[rxLen-sizeof(FRAME_TAIL)-6]<<8) | rxBuf[rxLen-sizeof(FRAME_TAIL)-5] != crc) + uint16_t crc = CalculateCRC16(rxBuf+sizeof(FRAME_HEADER), rxLen - sizeof(FRAME_HEADER)-sizeof(FRAME_TAIL)-2); // 计算crc,不包含帧头和帧尾和crc自身 + if (((rxBuf[rxLen-sizeof(FRAME_TAIL)-2]<<8) | rxBuf[rxLen-sizeof(FRAME_TAIL)-1]) != crc) { + printf("CMD_FRAME_CHECK_ERROR\r\n"); return CMD_FRAME_CHECK_ERROR; } return CMD_FRAME_OK; @@ -1162,7 +1165,7 @@ CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint16_t rxLen) { * @param rxLen 接收到的数据长度 * @return 命令帧错误码 */ -CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint16_t rxLen) { +CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint8_t rxLen) { CmdFrameError_t error = checkHostCmd(rxBuf, rxLen); if (error != CMD_FRAME_OK) @@ -1203,3 +1206,25 @@ CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint16_t rxLen) { return error; } + + +void runPumpDemo(void) { + printf("runPumpDemo\n"); + printf("InitPump\n"); + InitPump(); + // 泵1正转100步 + printf("SetPumpStepTarget(0, 100)\n"); + SetPumpStepTarget(0, 100); + printf("StartPumpRelativeMove(0)\n"); + StartPumpRelativeMove(0); + HAL_Delay(1000); + // 泵1反转100步 + printf("SetPumpStepTarget(0, -100)\n"); + SetPumpStepTarget(0, -100); + printf("StartPumpRelativeMove(0)\n"); + StartPumpRelativeMove(0); + HAL_Delay(1000); + printf("StopPump(0)\n"); + StopPump(0); + HAL_Delay(1000); +} \ No newline at end of file diff --git a/protocol.h b/protocol.h index 206f59f..8c263c1 100644 --- a/protocol.h +++ b/protocol.h @@ -408,11 +408,12 @@ typedef struct { extern DeviceParam dp; // 函数声明 -CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint16_t rxLen); +CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint8_t rxLen); // uint16_t CalculateCRC16(uint8_t *data, uint16_t length); void InitDeviceStatus(); void DecodePumpAlarmMsg(uint16_t reg4001); void DecodePumpStatusMsg(uint16_t reg4002); void UpdatePumpStatus(); void ProcessMotorMsg(uint8_t *rxBuf, uint16_t rxLen); +void runPumpDemo(void); #endif // PROTOCOL_H \ No newline at end of file