uart_process.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "uart_process.h"
  2. #include "var.h"
  3. #include "crc_cal.h"
  4. #include "stdlib.h"
  5. #include "stdio.h"
  6. #include "string.h"
  7. #include "can_process.h"
  8. #include "tasks.h"
  9. //命令处理
  10. void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  11. {
  12. uint8_t DataLength;
  13. uint16_t Data16_Temp;
  14. //通信正常标志
  15. IsComOK_TE.IsOK_Flag = TRUE;
  16. IsComOK_TE.OK_TrigTime = HAL_GetTick();
  17. //命令解析
  18. DataLength = (uint8_t)(Command &0x00FF);
  19. switch(Command)
  20. {
  21. case 0x8008:
  22. {
  23. //复制TE返回故障码
  24. memcpy((uint8_t*)&MC_TE_SensorStatus.TE_ErrorCode, Data, DataLength);
  25. TE_MCU_DataRefreshFlag = TRUE;
  26. break;
  27. }
  28. case 0xC502://最后一帧数据
  29. {
  30. SendData(ID_MC_TE_TO_CDL, Mode, Command, Data);//将TE过来的数据直接透传到CAN
  31. IsSendDataToTE_Step = SENDSENSOR;
  32. break;
  33. }
  34. case 0x9002://TE检测的硬件版本
  35. {
  36. Data16_Temp = Data[0] + (Data[1] << 8);
  37. uint8_t i;
  38. for(i = 0; i < sizeof(Ver_Table) / 2 ; i++)
  39. {
  40. if(Data16_Temp < Ver_Table[i]) break;
  41. }
  42. MC_VerInfo.HW_Version[7] = 'F' + i;
  43. break;
  44. }
  45. default:
  46. {
  47. SendData(ID_MC_TE_TO_CDL, Mode, Command, Data);//将TE过来的数据直接透传到CAN
  48. break;
  49. }
  50. }
  51. }
  52. //发送数据
  53. uint8_t UartSendBuf[150];
  54. uint8_t UartCRC_Buf[150];
  55. void SendUartDataToTE(USART_Buf_TypeDef * ptTx,uint16_t IDnum,uint8_t Mode, uint16_t Command, uint8_t* Data)
  56. {
  57. uint32_t CRC_Result = 0x00000000;
  58. uint8_t DataLength;
  59. DataLength = (uint8_t)(Command & 0xFF);
  60. UartSendBuf[0] = FRAME_BEGIN1;
  61. UartSendBuf[1] = FRAME_BEGIN2;
  62. UartSendBuf[2] = (uint8_t)((IDnum >> 8) & 0xFF);
  63. UartSendBuf[3] = (uint8_t)(IDnum & 0xFF);;
  64. UartSendBuf[4] = Mode;
  65. UartSendBuf[5] = DataLength + 2;
  66. UartSendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  67. UartSendBuf[7] = DataLength;
  68. memcpy(UartSendBuf + 8, Data, DataLength);
  69. memcpy(UartCRC_Buf, UartSendBuf, 8 + DataLength);
  70. CRC_Result = CRC32_Calculate(UartCRC_Buf, DataLength + 8);
  71. UartSendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  72. UartSendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  73. UartSendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  74. UartSendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  75. UartSendBuf[12 + DataLength] = FRAME_END;
  76. UART_SendData(ptTx, UartSendBuf, DataLength + 13);
  77. }
  78. //串口数据解析
  79. uint8_t UartRevData[150], UartRevData_CRC_Buf[150];
  80. void Uart_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  81. {
  82. uint8_t Mode, CmdLength, DataLength;
  83. uint16_t Cmd, i;
  84. uint32_t CrcResult, CrcData;
  85. uint8_t FrameBegin1, FrameBegin2;
  86. static uint32_t DelayTimeCnt = 0;
  87. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  88. if(ptUartRx->ucBufCnt >= 13)
  89. {
  90. //读取帧头
  91. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  92. UartRevData_CRC_Buf[0] = FrameBegin1;
  93. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  94. UartRevData_CRC_Buf[1] = FrameBegin2;
  95. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  96. {
  97. UartRevData_CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  98. UartRevData_CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  99. //读取帧模式
  100. Mode = UART_ReadChar(ptUartRx, 4);
  101. UartRevData_CRC_Buf[4] = Mode;
  102. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  103. {
  104. //读取命令段长度和命令字
  105. CmdLength = UART_ReadChar(ptUartRx, 5);
  106. UartRevData_CRC_Buf[5] = CmdLength;
  107. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  108. UartRevData_CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  109. UartRevData_CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  110. DataLength = UART_ReadChar(ptUartRx, 7);
  111. if((CmdLength - DataLength) == 2)
  112. {
  113. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  114. {
  115. if(IsWaitRX_Flag == FALSE)
  116. {
  117. DelayTimeCnt = HAL_GetTick();
  118. IsWaitRX_Flag = TRUE;
  119. }
  120. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  121. {
  122. // UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  123. DelayTimeCnt = HAL_GetTick();
  124. IsWaitRX_Flag = FALSE;
  125. }
  126. return;
  127. }
  128. else
  129. {
  130. IsWaitRX_Flag = FALSE;
  131. //接收到完整正确数据包
  132. for(i=0; i<DataLength; i++)//读取数据段
  133. {
  134. UartRevData[i] = UART_ReadChar(ptUartRx, 8 + i);
  135. UartRevData_CRC_Buf[8 + i] = UartRevData[i];
  136. }
  137. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  138. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  139. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  140. UART_ReadChar(ptUartRx, 11 + DataLength);
  141. CrcResult = CRC32_Calculate(UartRevData_CRC_Buf, 8 + DataLength);
  142. if((CrcData - CrcResult) == 0) // 比较校验
  143. {
  144. //数据处理
  145. UART_DataProcess(Mode, Cmd, UartRevData);//Mode为帧模式,Cmd为命令字,Data为数据段
  146. UART_DelChar(ptUartRx, CmdLength + 11);
  147. return;
  148. }
  149. UART_DelChar(ptUartRx, 1);
  150. }
  151. }
  152. else
  153. {
  154. UART_DelChar(ptUartRx, 1);
  155. }
  156. }
  157. else
  158. {
  159. UART_DelChar(ptUartRx, 1);
  160. }
  161. }
  162. else
  163. {
  164. UART_DelChar(ptUartRx, 1);
  165. }
  166. }
  167. }