123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #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[7] = 'F' + i;
- break;
- }
- default:
- {
- SendData(ID_MC_TE_TO_CDL, Mode, Command, Data);//将TE过来的数据直接透传到CAN
- break;
- }
- }
- }
- //发送数据
- void SendUartDataToTE(USART_Buf_TypeDef * ptTx,uint16_t IDnum,uint8_t Mode, uint16_t Command, uint8_t* Data)
- {
- uint8_t SendBuf[150] = {0};
- uint8_t CRC_Buf[150] = {0};
- uint32_t CRC_Result = 0x00000000;
- uint8_t DataLength;
-
- DataLength = (uint8_t)(Command & 0xFF);
- SendBuf[0] = FRAME_BEGIN1;
- SendBuf[1] = FRAME_BEGIN2;
- SendBuf[2] = (uint8_t)((IDnum >> 8) & 0xFF);
- SendBuf[3] = (uint8_t)(IDnum & 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);
- }
- //串口数据解析
- 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<DataLength; i++)//读取数据段
- {
- UartRevData[i] = UART_ReadChar(ptUartRx, 8 + i);
- UartRevData_CRC_Buf[8 + i] = UartRevData[i];
- }
- CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
- (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
- (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
- UART_ReadChar(ptUartRx, 11 + DataLength);
- CrcResult = CRC32_Calculate(UartRevData_CRC_Buf, 8 + DataLength);
- if((CrcData - CrcResult) == 0) // 比较校验
- {
- //数据处理
- UART_DataProcess(Mode, Cmd, UartRevData);//Mode为帧模式,Cmd为命令字,Data为数据段
- UART_DelChar(ptUartRx, CmdLength + 11);
- return;
- }
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- }
|