uart_process.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "can_process.h"
  2. #include "uart_process.h"
  3. #include "crc_cal.h"
  4. #include "stdlib.h"
  5. #include "stdio.h"
  6. #include "string.h"
  7. #include "encrypt.h"
  8. #include "Update.h"
  9. //命令处理
  10. static void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  11. {
  12. switch(Command)
  13. {
  14. //CDL在线检测
  15. case 0x1100:
  16. {
  17. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x1100, (uint8_t*)NULL);
  18. break;
  19. }
  20. //设备开关机
  21. case 0x2201:
  22. {
  23. if(Data[0] == 0xF0)//关机
  24. {
  25. HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_RESET);
  26. }
  27. else if(Data[0] == 0xF1)//开机
  28. {
  29. HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_SET);
  30. }
  31. break;
  32. }
  33. //复位
  34. case 0x4400:
  35. {
  36. Refresh_Flag();
  37. __set_FAULTMASK(1);//关闭所有中断
  38. HAL_NVIC_SystemReset();
  39. break;
  40. }
  41. //随机码和密钥进行校验
  42. case 0x5514:
  43. {
  44. static uint8_t InputCode[12];
  45. static uint8_t InputKey[8];
  46. static uint8_t OutputCode[12];
  47. static uint8_t SendData[27];
  48. memcpy(InputCode, Data, 12);
  49. memcpy(InputKey, Data + 12, 8);
  50. CheckCodeCal(InputCode, InputKey, OutputCode);
  51. memcpy(SendData, OutputCode, 12);
  52. memcpy(SendData + 12, (uint8_t*)"V1.0.1_20190902", 15);
  53. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x551B, SendData);
  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[255] = {0};
  63. uint8_t CRC_Buf[255] = {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;
  89. static uint8_t Data[255], CRC_Buf[255];
  90. uint16_t Cmd, i, ID;
  91. uint32_t CrcResult, CrcData;
  92. uint8_t FrameBegin1, FrameBegin2;
  93. static uint32_t DelayTimeCnt = 0;
  94. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  95. if(ptUartRx->ucBufCnt >= 13)
  96. {
  97. //读取帧头
  98. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  99. CRC_Buf[0] = FrameBegin1;
  100. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  101. CRC_Buf[1] = FrameBegin2;
  102. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  103. {
  104. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  105. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  106. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  107. //读取帧模式
  108. Mode = UART_ReadChar(ptUartRx, 4);
  109. CRC_Buf[4] = Mode;
  110. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  111. {
  112. //读取命令段长度和命令字
  113. CmdLength = UART_ReadChar(ptUartRx, 5);
  114. CRC_Buf[5] = CmdLength;
  115. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  116. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  117. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  118. DataLength = UART_ReadChar(ptUartRx, 7);
  119. if((CmdLength - DataLength) == 2)
  120. {
  121. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  122. {
  123. if(IsWaitRX_Flag == FALSE)
  124. {
  125. DelayTimeCnt = HAL_GetTick();
  126. IsWaitRX_Flag = TRUE;
  127. }
  128. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  129. {
  130. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  131. IsWaitRX_Flag = FALSE;
  132. }
  133. return;
  134. }
  135. else
  136. {
  137. IsWaitRX_Flag = FALSE;
  138. //接收到完整正确数据包
  139. for(i=0; i<DataLength; i++)//读取数据段
  140. {
  141. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  142. CRC_Buf[8 + i] = Data[i];
  143. }
  144. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  145. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  146. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  147. UART_ReadChar(ptUartRx, 11 + DataLength);
  148. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  149. if((CrcData - CrcResult) == 0) // 比较校验
  150. {
  151. //数据处理
  152. UART_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  153. //数据转发CAN
  154. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  155. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  156. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  157. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  158. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  159. CRC_Buf[10 + DataLength] = FRAME_END;
  160. CAN_SendData(ID, CRC_Buf, DataLength + 11);
  161. //清除缓存
  162. UART_DelChar(ptUartRx, CmdLength + 11);
  163. //通信指示
  164. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  165. return;
  166. }
  167. UART_DelChar(ptUartRx, 1);
  168. }
  169. }
  170. else
  171. {
  172. UART_DelChar(ptUartRx, 1);
  173. }
  174. }
  175. else
  176. {
  177. UART_DelChar(ptUartRx, 1);
  178. }
  179. }
  180. else
  181. {
  182. UART_DelChar(ptUartRx, 1);
  183. }
  184. }
  185. }