uart_process.c 4.9 KB

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