uart_process.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // strncpy(MC_VerInfo.HW_Version, (char*)"TTKZ010 ", 10);
  43. MC_VerInfo.HW_Version[7] = 'F' + i;
  44. break;
  45. }
  46. default:
  47. {
  48. SendData(ID_MC_TE_TO_CDL, Mode, Command, Data);//将TE过来的数据直接透传到CAN
  49. break;
  50. }
  51. }
  52. }
  53. //发送数据
  54. void SendUartDataToTE(USART_Buf_TypeDef * ptTx,uint16_t IDnum,uint8_t Mode, uint16_t Command, uint8_t* Data)
  55. {
  56. uint8_t SendBuf[150] = {0};
  57. uint8_t CRC_Buf[150] = {0};
  58. uint32_t CRC_Result = 0x00000000;
  59. uint8_t DataLength;
  60. DataLength = (uint8_t)(Command & 0xFF);
  61. SendBuf[0] = FRAME_BEGIN1;
  62. SendBuf[1] = FRAME_BEGIN2;
  63. SendBuf[2] = (uint8_t)((IDnum >> 8) & 0xFF);
  64. SendBuf[3] = (uint8_t)(IDnum & 0xFF);;
  65. SendBuf[4] = Mode;
  66. SendBuf[5] = DataLength + 2;
  67. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  68. SendBuf[7] = DataLength;
  69. memcpy(SendBuf + 8, Data, DataLength);
  70. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  71. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  72. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  73. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  74. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  75. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  76. SendBuf[12 + DataLength] = FRAME_END;
  77. UART_SendData(ptTx, SendBuf, DataLength + 13);
  78. }
  79. //串口数据解析
  80. uint8_t UartRevData[150], UartRevData_CRC_Buf[150];
  81. void Uart_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  82. {
  83. uint8_t Mode, CmdLength, DataLength;
  84. uint16_t Cmd, i;
  85. uint32_t CrcResult, CrcData;
  86. uint8_t FrameBegin1, FrameBegin2;
  87. static uint32_t DelayTimeCnt = 0;
  88. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  89. if(ptUartRx->ucBufCnt >= 13)
  90. {
  91. //读取帧头
  92. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  93. UartRevData_CRC_Buf[0] = FrameBegin1;
  94. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  95. UartRevData_CRC_Buf[1] = FrameBegin2;
  96. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  97. {
  98. UartRevData_CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  99. UartRevData_CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  100. //读取帧模式
  101. Mode = UART_ReadChar(ptUartRx, 4);
  102. UartRevData_CRC_Buf[4] = Mode;
  103. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  104. {
  105. //读取命令段长度和命令字
  106. CmdLength = UART_ReadChar(ptUartRx, 5);
  107. UartRevData_CRC_Buf[5] = CmdLength;
  108. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  109. UartRevData_CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  110. UartRevData_CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  111. DataLength = UART_ReadChar(ptUartRx, 7);
  112. if((CmdLength - DataLength) == 2)
  113. {
  114. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  115. {
  116. if(IsWaitRX_Flag == FALSE)
  117. {
  118. DelayTimeCnt = HAL_GetTick();
  119. IsWaitRX_Flag = TRUE;
  120. }
  121. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  122. {
  123. // UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  124. DelayTimeCnt = HAL_GetTick();
  125. IsWaitRX_Flag = FALSE;
  126. }
  127. return;
  128. }
  129. else
  130. {
  131. IsWaitRX_Flag = FALSE;
  132. //接收到完整正确数据包
  133. for(i=0; i<DataLength; i++)//读取数据段
  134. {
  135. UartRevData[i] = UART_ReadChar(ptUartRx, 8 + i);
  136. UartRevData_CRC_Buf[8 + i] = UartRevData[i];
  137. }
  138. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  139. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  140. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  141. UART_ReadChar(ptUartRx, 11 + DataLength);
  142. CrcResult = CRC32_Calculate(UartRevData_CRC_Buf, 8 + DataLength);
  143. if((CrcData - CrcResult) == 0) // 比较校验
  144. {
  145. //数据处理
  146. UART_DataProcess(Mode, Cmd, UartRevData);//Mode为帧模式,Cmd为命令字,Data为数据段
  147. UART_DelChar(ptUartRx, CmdLength + 11);
  148. return;
  149. }
  150. UART_DelChar(ptUartRx, 1);
  151. }
  152. }
  153. else
  154. {
  155. UART_DelChar(ptUartRx, 1);
  156. }
  157. }
  158. else
  159. {
  160. UART_DelChar(ptUartRx, 1);
  161. }
  162. }
  163. else
  164. {
  165. UART_DelChar(ptUartRx, 1);
  166. }
  167. }
  168. }