uart_process.c 4.6 KB

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