uart_process.c 4.9 KB

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