#include "uart_process.h" #include "var.h" #include "crc_cal.h" #include "stdlib.h" #include "stdio.h" #include "string.h" //命令处理 void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data) { uint8_t DataLength; static uint8_t EnterCnt = 0; //通信正常标志 IsComOK_TE = TRUE; //命令解析 DataLength = (uint8_t)(Command &0x00FF); switch(Command) { case 0x8008: { //复制TE返回故障码 memcpy((uint8_t*)&MC_TE_SensorStatus.TE_ErrorCode, Data, DataLength); //发送同步信号,清零计数 EnterCnt++; if(EnterCnt >= 20) { if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.BikeSpeed == 0) { MC_TE_SensorData.SpeedSensorTrigCnt = 0; } if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.StopBreak == 0) { MC_TE_SensorData.BreakTrgiCnt = 0; } if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.CadenseSensor_1 == 0) { MC_TE_SensorData.CadenceHall_1_Cnt = 0; } if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.CadenseSensor_2 == 0) { MC_TE_SensorData.CadenceHall_2_Cnt = 0; } if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.HallSensor_A == 0) { MC_TE_SensorData.MotorHall_A_Cnt = 0; } if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.HallSensor_B == 0) { MC_TE_SensorData.MotorHall_B_Cnt = 0; } HAL_GPIO_TogglePin(SYC_IO_GPIO_Port, SYC_IO_Pin); EnterCnt = 0; } break; } default:break; } } //发送数据 void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t Command, uint8_t* Data) { uint8_t SendBuf[96] = {0}; uint8_t CRC_Buf[98] = {0}; uint32_t CRC_Result = 0x00000000; uint8_t DataLength; DataLength = (uint8_t)(Command & 0xFF); SendBuf[0] = FRAME_BEGIN1; SendBuf[1] = FRAME_BEGIN2; SendBuf[2] = 0x07; SendBuf[3] = 0xFF; SendBuf[4] = Mode; SendBuf[5] = DataLength + 2; SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF); SendBuf[7] = DataLength; memcpy(SendBuf + 8, Data, DataLength); memcpy(CRC_Buf, SendBuf, 8 + DataLength); CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8); SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF); SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF); SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF); SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF); SendBuf[12 + DataLength] = FRAME_END; UART_SendData(ptTx, SendBuf, DataLength + 13); } //串口数据解析 void Uart_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut) { uint8_t Mode, CmdLength, DataLength, Data[64], CRC_Buf[78]; uint16_t Cmd, i; uint32_t CrcResult, CrcData; uint8_t FrameBegin1, FrameBegin2; static uint32_t DelayTimeCnt = 0; static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE; if(ptUartRx->ucBufCnt >= 13) { //读取帧头 FrameBegin1 = UART_ReadChar(ptUartRx, 0); CRC_Buf[0] = FrameBegin1; FrameBegin2 = UART_ReadChar(ptUartRx, 1); CRC_Buf[1] = FrameBegin2; if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2)) { CRC_Buf[2] = UART_ReadChar(ptUartRx, 2); CRC_Buf[3] = UART_ReadChar(ptUartRx, 3); //读取帧模式 Mode = UART_ReadChar(ptUartRx, 4); CRC_Buf[4] = Mode; if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT)) { //读取命令段长度和命令字 CmdLength = UART_ReadChar(ptUartRx, 5); CRC_Buf[5] = CmdLength; Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7); CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF); CRC_Buf[7] = (uint8_t)(Cmd & 0xFF); DataLength = UART_ReadChar(ptUartRx, 7); if((CmdLength - DataLength) == 2) { if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte { if(IsWaitRX_Flag == FALSE) { DelayTimeCnt = HAL_GetTick(); IsWaitRX_Flag = TRUE; } if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms { UART_DelChar(ptUartRx, ptUartRx->ucBufCnt); IsWaitRX_Flag = FALSE; } return; } else { IsWaitRX_Flag = FALSE; //接收到完整正确数据包 for(i=0; i