update crc

This commit is contained in:
murmur 2024-12-19 22:49:20 +08:00
parent 2877853cf1
commit f18bded566
2 changed files with 40 additions and 14 deletions

View File

@ -12,8 +12,8 @@ static DeviceStatus deviceStatus = {
}; };
DeviceParam dp = {{ DeviceParam dp = {{
{"pump1", 1, 100, 100, 100}, {"pump1", 4, 100, 100, 100},
{"pump2", 2, 100, 100, 100}, {"pump2", 4, 100, 100, 100},
{"valve1", 3, 100, 100, 100}, {"valve1", 3, 100, 100, 100},
{"valve2", 4, 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 data CRC的数据缓冲区
* @param length * @param length
@ -172,6 +172,7 @@ void updateInitStatus(InitStatus_t status) {
* @param txLen * @param txLen
*/ */
void writeCMD(uint8_t *txBuf, uint16_t 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) { 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: : * @return 0: :
*/ */
static uint8_t HandleStatusQuery(void) { static uint8_t HandleStatusQuery(void) {
// 填充并返回数据
uint8_t txBuf[sizeof(DeviceStatus)] = {0}; packMsgToHost(HOST_CMD_STATUS_QUERY, 0);
memcpy(txBuf, &deviceStatus, sizeof(DeviceStatus));
sendMsgToHost(txBuf, sizeof(txBuf));
return 0; return 0;
} }
@ -1088,7 +1088,7 @@ void packMsgToHost(uint16_t funcCode, uint8_t isOK) {
uint8_t len = 0; uint8_t len = 0;
uint8_t dlen = 0; uint8_t dlen = 0;
uint8_t index = 0; uint8_t index = 0;
FillBigEndian32(msgBuf+4, FRAME_HEADER); FillBigEndian32(msgBuf, FRAME_HEADER);
FillBigEndian16(msgBuf+sizeof(FRAME_HEADER), funcCode); FillBigEndian16(msgBuf+sizeof(FRAME_HEADER), funcCode);
if(funcCode == HOST_CMD_STATUS_QUERY) { if(funcCode == HOST_CMD_STATUS_QUERY) {
@ -1128,7 +1128,7 @@ void packMsgToHost(uint16_t funcCode, uint8_t isOK) {
* @param rxLen * @param rxLen
* @return * @return
*/ */
CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint16_t rxLen) { CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint8_t rxLen) {
// 检查命令是否正确 // 检查命令是否正确
// FRAME_HEADER是按小端序存储的而rxBuf是按大端序存的 // FRAME_HEADER是按小端序存储的而rxBuf是按大端序存的
uint8_t header[sizeof(FRAME_HEADER)]; 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) if(memcmp(rxBuf, header, sizeof(FRAME_HEADER)) != 0)
{ {
printf("CMD_FRAME_HEADER_ERROR\r\n");
return CMD_FRAME_HEADER_ERROR; return CMD_FRAME_HEADER_ERROR;
} }
if (memcmp(rxBuf + rxLen - sizeof(FRAME_TAIL), tail, sizeof(FRAME_TAIL)) != 0) if (memcmp(rxBuf + rxLen - sizeof(FRAME_TAIL), tail, sizeof(FRAME_TAIL)) != 0)
{ {
printf("CMD_FRAME_TAIL_ERROR\r\n");
return CMD_FRAME_TAIL_ERROR; return CMD_FRAME_TAIL_ERROR;
} }
uint16_t crc = CalculateCRC16(rxBuf+sizeof(FRAME_HEADER), rxLen - sizeof(FRAME_HEADER)-sizeof(FRAME_TAIL))-2;// 计算crc不包含帧头和帧尾和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)-6]<<8) | rxBuf[rxLen-sizeof(FRAME_TAIL)-5] != 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_CHECK_ERROR;
} }
return CMD_FRAME_OK; return CMD_FRAME_OK;
@ -1162,7 +1165,7 @@ CmdFrameError_t checkHostCmd(uint8_t *rxBuf, uint16_t rxLen) {
* @param rxLen * @param rxLen
* @return * @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); CmdFrameError_t error = checkHostCmd(rxBuf, rxLen);
if (error != CMD_FRAME_OK) if (error != CMD_FRAME_OK)
@ -1203,3 +1206,25 @@ CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint16_t rxLen) {
return error; 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);
}

View File

@ -408,11 +408,12 @@ typedef struct {
extern DeviceParam dp; 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); // uint16_t CalculateCRC16(uint8_t *data, uint16_t length);
void InitDeviceStatus(); void InitDeviceStatus();
void DecodePumpAlarmMsg(uint16_t reg4001); void DecodePumpAlarmMsg(uint16_t reg4001);
void DecodePumpStatusMsg(uint16_t reg4002); void DecodePumpStatusMsg(uint16_t reg4002);
void UpdatePumpStatus(); void UpdatePumpStatus();
void ProcessMotorMsg(uint8_t *rxBuf, uint16_t rxLen); void ProcessMotorMsg(uint8_t *rxBuf, uint16_t rxLen);
void runPumpDemo(void);
#endif // PROTOCOL_H #endif // PROTOCOL_H