#include "uart_process.h" #include "var.h" #include "crc_cal.h" #include "stdlib.h" #include "stdio.h" #include "string.h" #include "can_process.h" #include "tasks.h" //命令处理 void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data) { uint8_t DataLength; uint16_t Data16_Temp; //通信正常标志 IsComOK_TE.IsOK_Flag = TRUE; IsComOK_TE.OK_TrigTime = HAL_GetTick(); //命令解析 DataLength = (uint8_t)(Command &0x00FF); switch(Command) { case 0x8008: { //复制TE返回故障码 memcpy((uint8_t*)&MC_TE_SensorStatus.TE_ErrorCode, Data, DataLength); TE_MCU_DataRefreshFlag = TRUE; break; } case 0xC502://最后一帧数据 { SendData(ID_MC_TE_TO_CDL, Mode, Command, Data);//将TE过来的数据直接透传到CAN IsSendDataToTE_Step = SENDSENSOR; break; } case 0x9002://TE检测的硬件版本 { Data16_Temp = Data[0] + (Data[1] << 8); uint8_t i; for(i = 0; i < sizeof(Ver_Table) / 2 ; i++) { if(Data16_Temp < Ver_Table[i]) break; } // strncpy(MC_VerInfo.HW_Version, (char*)"TTKZ010 ", 10); MC_VerInfo.HW_Version[5] = 'I' + i; break; } default: { SendData(ID_MC_TE_TO_CDL, Mode, Command, Data);//将TE过来的数据直接透传到CAN break; } } } //发送数据 uint8_t UartSendBuf[150]; uint8_t UartCRC_Buf[150]; void SendUartDataToTE(USART_Buf_TypeDef * ptTx,uint16_t IDnum,uint8_t Mode, uint16_t Command, uint8_t* Data) { uint32_t CRC_Result = 0x00000000; uint8_t DataLength; DataLength = (uint8_t)(Command & 0xFF); UartSendBuf[0] = FRAME_BEGIN1; UartSendBuf[1] = FRAME_BEGIN2; UartSendBuf[2] = (uint8_t)((IDnum >> 8) & 0xFF); UartSendBuf[3] = (uint8_t)(IDnum & 0xFF);; UartSendBuf[4] = Mode; UartSendBuf[5] = DataLength + 2; UartSendBuf[6] = (uint8_t)((Command >> 8) & 0xFF); UartSendBuf[7] = DataLength; memcpy(UartSendBuf + 8, Data, DataLength); memcpy(UartCRC_Buf, UartSendBuf, 8 + DataLength); CRC_Result = CRC32_Calculate(UartCRC_Buf, DataLength + 8); UartSendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF); UartSendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF); UartSendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF); UartSendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF); UartSendBuf[12 + DataLength] = FRAME_END; UART_SendData(ptTx, UartSendBuf, DataLength + 13); } //串口数据解析 uint8_t UartRevData[150], UartRevData_CRC_Buf[150]; void Uart_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut) { uint8_t Mode, CmdLength, DataLength; 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); UartRevData_CRC_Buf[0] = FrameBegin1; FrameBegin2 = UART_ReadChar(ptUartRx, 1); UartRevData_CRC_Buf[1] = FrameBegin2; if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2)) { UartRevData_CRC_Buf[2] = UART_ReadChar(ptUartRx, 2); UartRevData_CRC_Buf[3] = UART_ReadChar(ptUartRx, 3); //读取帧模式 Mode = UART_ReadChar(ptUartRx, 4); UartRevData_CRC_Buf[4] = Mode; if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT)) { //读取命令段长度和命令字 CmdLength = UART_ReadChar(ptUartRx, 5); UartRevData_CRC_Buf[5] = CmdLength; Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7); UartRevData_CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF); UartRevData_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); DelayTimeCnt = HAL_GetTick(); IsWaitRX_Flag = FALSE; } return; } else { IsWaitRX_Flag = FALSE; //接收到完整正确数据包 for(i=0; i