uart_process.c 4.7 KB

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