uart_process.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. #include "ctf_process.h"
  10. #include "flash.h"
  11. uint8_t Power_mode = 0;//0-default,1-off,2-on
  12. uint32_t T_startPowerOnOff = 0;
  13. uint8_t UART1_BaudrateSwitch = 0;
  14. uint32_t T_UART1_BaudrateSwitch = 0;//0-9600bps,1-57600bps,2-ready to switch baudrate to 57600bps
  15. uint32_t T_RecvCANMsg = 0;
  16. uint32_t T_RecvUartMsg = 0;
  17. uint8_t Transmit_UARTOrCAN = 1;//0-both,1-CAN,2-UART
  18. uint32_t Uart_BootBaudrate = 57600;
  19. uint32_t Uart_AppBaudrate = 9600;
  20. uint32_t CAN_Baudrate = 250;
  21. void UART1_BaudrateCheck(void)
  22. {
  23. if(UART1_BaudrateSwitch == 2)
  24. {
  25. if((HAL_GetTick() - T_UART1_BaudrateSwitch)>50)
  26. {
  27. UART1_BaudrateSwitch = 1;
  28. MX_USART1_Reopen(Uart_BootBaudrate);
  29. }
  30. }
  31. else if(UART1_BaudrateSwitch == 1)
  32. {
  33. if((HAL_GetTick() - T_UART1_BaudrateSwitch)>5000)
  34. {
  35. UART1_BaudrateSwitch = 0;
  36. MX_USART1_Reopen(Uart_AppBaudrate);
  37. }
  38. }
  39. }
  40. uint8_t Transmit_UARTOrCAN_pre = 0;//0-both,1-CAN,2-UART
  41. void MsgModeCheck(void)
  42. {
  43. Transmit_UARTOrCAN_pre = Transmit_UARTOrCAN;
  44. if(((HAL_GetTick() - T_RecvCANMsg) > 30000) && ((HAL_GetTick() - T_RecvUartMsg) > 30000))
  45. {
  46. Transmit_UARTOrCAN = 0;//超过x秒未收到UART和CAN数据进入模式0,下次通信同时发送CAN和UART数据
  47. }
  48. else if(((HAL_GetTick() - T_RecvCANMsg) < 5000)&&(T_RecvCANMsg>0))
  49. {
  50. Transmit_UARTOrCAN = 1;
  51. }
  52. else if(((HAL_GetTick() - T_RecvUartMsg) < 5000)&&(T_RecvUartMsg>0))
  53. {
  54. Transmit_UARTOrCAN = 2;
  55. }
  56. else if((T_RecvCANMsg == 0)&&(T_RecvUartMsg == 0))
  57. {
  58. Transmit_UARTOrCAN = 0;
  59. }
  60. if(Transmit_UARTOrCAN_pre != Transmit_UARTOrCAN)
  61. {
  62. if(Transmit_UARTOrCAN == 2)
  63. {
  64. HAL_Delay(100);
  65. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin1, GPIO_PIN_SET);
  66. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin2, GPIO_PIN_SET);
  67. }
  68. else
  69. {
  70. HAL_Delay(100);
  71. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin1, GPIO_PIN_RESET);
  72. HAL_GPIO_WritePin(CommSwitch_GPIO_Port, CommSwitch_Pin2, GPIO_PIN_RESET);
  73. }
  74. }
  75. }
  76. void PowerOnOff(uint8_t *mode)
  77. {
  78. if(*mode == 1)
  79. {
  80. if((HAL_GetTick() - T_startPowerOnOff) >= 1000)//Off延时1s
  81. {
  82. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
  83. *mode = 0;
  84. }
  85. }
  86. else if(*mode == 2)
  87. {
  88. if((HAL_GetTick() - T_startPowerOnOff) >= 3000)//On延时3s
  89. {
  90. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
  91. *mode = 0;
  92. }
  93. }
  94. }
  95. //命令处理
  96. static void UART3_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  97. {
  98. switch(Command)
  99. {
  100. //CDL在线检测
  101. case 0x1100:
  102. {
  103. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x1100, (uint8_t*)NULL);
  104. break;
  105. }
  106. //设备开关机
  107. case 0x2201:
  108. {
  109. if(Power_mode == 0)
  110. {
  111. T_startPowerOnOff = HAL_GetTick();
  112. if(Data[0] == 0xF0)//关机
  113. {
  114. Power_mode = 1;
  115. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//On
  116. }
  117. else if(Data[0] == 0xF1)//开机
  118. {
  119. Power_mode = 2;
  120. HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//On
  121. }
  122. }
  123. if(Data[0] == 0xF0)//关机
  124. {
  125. // HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_RESET);
  126. HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_RESET);
  127. }
  128. else if(Data[0] == 0xF1)//开机
  129. {
  130. // HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_SET);
  131. HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_SET);
  132. }
  133. break;
  134. }
  135. //复位
  136. case 0x4400:
  137. {
  138. Refresh_Flag();
  139. __set_FAULTMASK(1);//关闭所有中断
  140. HAL_NVIC_SystemReset();
  141. break;
  142. }
  143. case 0x4508:
  144. {
  145. if (strncmp("CDLRESET", (char *)Data, 8) == 0)
  146. {
  147. //重启
  148. __set_FAULTMASK(1);//关闭所有中断
  149. HAL_NVIC_SystemReset();
  150. }
  151. break;
  152. }
  153. case 0x2505:
  154. {
  155. if (strncmp("RESET", (char *)Data, 5) == 0)
  156. {
  157. // UART_TransferData(&UART_TxBuff_Struct1, UARTID_CDL_TO_MC, Mode, 0x751, Command, Data);
  158. // HAL_Delay(200);
  159. //
  160. // __set_FAULTMASK(1);//关闭所有中断
  161. // HAL_NVIC_SystemReset();
  162. T_UART1_BaudrateSwitch = HAL_GetTick();
  163. UART1_BaudrateSwitch = 2;
  164. }
  165. break;
  166. }
  167. //随机码和密钥进行校验
  168. case 0x5514:
  169. {
  170. static uint8_t InputCode[12];
  171. static uint8_t InputKey[8];
  172. static uint8_t OutputCode[12];
  173. static uint8_t SendData[27];
  174. memcpy(InputCode, Data, 12);
  175. memcpy(InputKey, Data + 12, 8);
  176. CheckCodeCal(InputCode, InputKey, OutputCode);
  177. memcpy(SendData, OutputCode, 12);
  178. memcpy(SendData + 12, (uint8_t*)"V4.1.3_20240906", 15);
  179. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x551B, SendData);
  180. break;
  181. }
  182. //授权
  183. case 0x6600:
  184. {
  185. uint8_t SendData[24];
  186. Ctf_CalAndSave();
  187. memcpy(SendData, MAC_ID, 12);
  188. memcpy(SendData + 12, Check_Code, 12);
  189. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x6618, SendData);
  190. HAL_Delay(200);
  191. __set_FAULTMASK(1);//关闭所有中断
  192. HAL_NVIC_SystemReset();
  193. break;
  194. }
  195. case 0x7702:
  196. {
  197. UART_SendSwitch_Lidian2(&UART_TxBuff_Struct1, 0x01, Command, Data);
  198. HAL_Delay(50);
  199. UART_SendSwitch_zhihuigainian(&UART_TxBuff_Struct1, Command, Data);
  200. HAL_Delay(50);
  201. break;
  202. }
  203. case 0x8000:
  204. {
  205. uint8_t SendData[4];
  206. Transmit_UARTOrCAN = (uint8_t)FLASH_Read(CDLPROPERTIES_AREA_ADDRESS, 1);
  207. SendData[0] = 0;
  208. SendData[1] = 0;
  209. SendData[2] = 0;
  210. SendData[3] = Transmit_UARTOrCAN;
  211. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8004, SendData);
  212. HAL_Delay(50);
  213. break;
  214. }
  215. case 0x8104:
  216. {
  217. Transmit_UARTOrCAN = Data[3];
  218. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  219. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  220. break;
  221. }
  222. case 0x8200:
  223. {
  224. uint8_t SendData[4];
  225. Uart_BootBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+4, 1);
  226. SendData[0] = (uint8_t)((Uart_BootBaudrate >> 24) & 0xFF);
  227. SendData[1] = (uint8_t)((Uart_BootBaudrate >> 16) & 0xFF);
  228. SendData[2] = (uint8_t)((Uart_BootBaudrate >> 8) & 0xFF);
  229. SendData[3] = (uint8_t)((Uart_BootBaudrate >> 0) & 0xFF);
  230. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8204, SendData);
  231. HAL_Delay(50);
  232. break;
  233. }
  234. case 0x8304:
  235. {
  236. Uart_BootBaudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  237. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  238. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  239. break;
  240. }
  241. case 0x8400:
  242. {
  243. uint8_t SendData[4];
  244. Uart_AppBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+8, 1);
  245. SendData[0] = (uint8_t)((Uart_AppBaudrate >> 24) & 0xFF);
  246. SendData[1] = (uint8_t)((Uart_AppBaudrate >> 16) & 0xFF);
  247. SendData[2] = (uint8_t)((Uart_AppBaudrate >> 8) & 0xFF);
  248. SendData[3] = (uint8_t)((Uart_AppBaudrate >> 0) & 0xFF);
  249. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8404, SendData);
  250. HAL_Delay(50);
  251. break;
  252. }
  253. case 0x8504:
  254. {
  255. Uart_AppBaudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  256. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  257. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  258. break;
  259. }
  260. case 0x8600:
  261. {
  262. uint8_t SendData[4];
  263. CAN_Baudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+12, 1);
  264. SendData[0] = (uint8_t)((CAN_Baudrate >> 24) & 0xFF);
  265. SendData[1] = (uint8_t)((CAN_Baudrate >> 16) & 0xFF);
  266. SendData[2] = (uint8_t)((CAN_Baudrate >> 8) & 0xFF);
  267. SendData[3] = (uint8_t)((CAN_Baudrate >> 0) & 0xFF);
  268. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8604, SendData);
  269. HAL_Delay(50);
  270. break;
  271. }
  272. case 0x8704:
  273. {
  274. CAN_Baudrate = (uint32_t)((Data[0]<<24) + (Data[1]<<16) + (Data[2]<<8) +(Data[3]));
  275. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  276. SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x07FF, 0x8002, (uint8_t*)"OK");
  277. break;
  278. }
  279. default:break;
  280. }
  281. }
  282. static void UART1_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
  283. {
  284. }
  285. //发送数据
  286. void UART_SendSwitch_Lidian2(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint16_t Cmd, uint8_t* Data)
  287. {
  288. uint8_t databuf[256] = {0};
  289. uint8_t i = 0;
  290. uint8_t len = 0;
  291. uint16_t checkxor = 0;
  292. len = 20;
  293. databuf[0] = Addr;
  294. databuf[1] = len;
  295. databuf[2] = (uint8_t)((Cmd >> 8) & 0xFF);
  296. for(i=0; i<(Cmd & 0xFF); i++)
  297. {
  298. databuf[3+i] = Data[i];
  299. }
  300. for(i=0; i<len; i++)
  301. {
  302. checkxor ^= databuf[i];
  303. }
  304. databuf[len-1] = checkxor;
  305. UART_SendData(ptTx, databuf, len);
  306. }
  307. void UART_SendSwitch_zhihuigainian(USART_Buf_TypeDef * ptTx, uint16_t Cmd, uint8_t* Data)
  308. {
  309. uint8_t databuf[256] = {0};
  310. uint8_t i = 0;
  311. uint8_t len = 0;
  312. uint16_t checkxor = 0;
  313. len = 13;
  314. databuf[0] = 0x55;
  315. for(i=0; i<(Cmd & 0xFF); i++)
  316. {
  317. databuf[1+i] = Data[i];
  318. }
  319. for(i=0; i<10; i=i+2)
  320. {
  321. checkxor ^= (uint16_t)((databuf[i]<<8) + databuf[i+1]);
  322. }
  323. databuf[len-3] = (uint8_t)(checkxor&0xff);
  324. databuf[len-2] = (uint8_t)(checkxor>>8);
  325. databuf[len-1] = 0xF0;
  326. UART_SendData(ptTx, databuf, len);
  327. }
  328. void UART_TransferData(USART_Buf_TypeDef * ptTx, uint8_t Addr, uint8_t Mode, uint16_t ID, uint16_t Cmd, uint8_t* Data)
  329. {
  330. uint8_t databuf[256] = {0};
  331. uint8_t i = 0;
  332. uint8_t len = 0;
  333. uint16_t checksum = 0;
  334. len = (uint8_t)((Cmd & 0xff) + 4);
  335. if(len > 247)
  336. {
  337. len = 247;
  338. }
  339. databuf[0] = UARTFRAME_BEGIN;
  340. databuf[1] = Addr;
  341. databuf[2] = Mode;
  342. databuf[3] = len;
  343. databuf[4] = (uint8_t)((ID >> 8) & 0xFF);
  344. databuf[5] = (uint8_t)(ID & 0xFF);
  345. databuf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  346. databuf[7] = (uint8_t)(Cmd & 0xFF);
  347. for(i=0; i<(len-4); i++)
  348. {
  349. databuf[8+i] = Data[i];
  350. }
  351. for(i=1; i<(len+4); i++)
  352. {
  353. checksum = (uint16_t)(databuf[i] + checksum);
  354. }
  355. databuf[4+len] = (uint8_t)checksum;
  356. databuf[5+len] = (uint8_t)(checksum>>8);
  357. databuf[6+len] = UARTFRAME_END1;
  358. databuf[7+len] = UARTFRAME_END2;
  359. UART_SendData(ptTx, databuf, len + 8);
  360. }
  361. void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t ID, uint16_t Command, uint8_t* Data)
  362. {
  363. uint8_t SendBuf[255] = {0};
  364. uint8_t CRC_Buf[255] = {0};
  365. uint32_t CRC_Result = 0x00000000;
  366. uint8_t DataLength;
  367. DataLength = (uint8_t)(Command & 0xFF);
  368. SendBuf[0] = FRAME_BEGIN1;
  369. SendBuf[1] = FRAME_BEGIN2;
  370. SendBuf[2] = (uint8_t)((ID >> 8) & 0xFF);
  371. SendBuf[3] = (uint8_t)(ID & 0xFF);
  372. SendBuf[4] = Mode;
  373. SendBuf[5] = DataLength + 2;
  374. SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
  375. SendBuf[7] = DataLength;
  376. memcpy(SendBuf + 8, Data, DataLength);
  377. memcpy(CRC_Buf, SendBuf, 8 + DataLength);
  378. CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
  379. SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
  380. SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
  381. SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
  382. SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
  383. SendBuf[12 + DataLength] = FRAME_END;
  384. UART_SendData(ptTx, SendBuf, DataLength + 13);
  385. }
  386. //串口1数据解析
  387. void Uart1_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  388. {
  389. uint8_t Mode, DataLength, Addr;
  390. static uint8_t Data[255];
  391. uint16_t Cmd, i, ID;
  392. uint16_t CheckSumResult=0, CheckSumData=0;
  393. uint8_t FrameBegin, FrameEnd1, FrameEnd2;
  394. static uint32_t DelayTimeCnt = 0;
  395. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  396. if(ptUartRx->ucBufCnt >= 8)
  397. {
  398. //读取帧头
  399. FrameBegin = UART_ReadChar(ptUartRx, 0);
  400. if(FrameBegin == UARTFRAME_BEGIN)
  401. {
  402. Addr = UART_ReadChar(ptUartRx, 1);//读取地址ID
  403. if(Addr == UARTADDR_CDL)
  404. {
  405. //读取帧模式
  406. Mode = UART_ReadChar(ptUartRx, 2);
  407. if((Mode == UARTMODE_READ) || (Mode == UARTMODE_WRITE) || (Mode == UARTMODE_REPORT))
  408. {
  409. //读取数据段长度
  410. DataLength = UART_ReadChar(ptUartRx, 3);
  411. if(ptUartRx->ucBufCnt < (DataLength + 8))//帧头1byte + 地址1byte + 模式1byte + 长度1byte + 校验位2bytes + 帧尾2bytes
  412. {
  413. if(IsWaitRX_Flag == FALSE)
  414. {
  415. DelayTimeCnt = HAL_GetTick();
  416. IsWaitRX_Flag = TRUE;
  417. }
  418. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  419. {
  420. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  421. IsWaitRX_Flag = FALSE;
  422. }
  423. return;
  424. }
  425. else
  426. {
  427. T_RecvUartMsg = HAL_GetTick();
  428. IsWaitRX_Flag = FALSE;
  429. //接收到完整正确数据包
  430. for(i=0; i<(DataLength+4); i++)//读取数据段
  431. {
  432. Data[i] = UART_ReadChar(ptUartRx, i);
  433. }
  434. CheckSumData = (UART_ReadChar(ptUartRx, 4 + DataLength)) + \
  435. (UART_ReadChar(ptUartRx, 5 + DataLength) << 8);
  436. for(i = 1; i<(DataLength+4); i++)
  437. {
  438. CheckSumResult = (uint16_t)(Data[i] + CheckSumResult);
  439. }
  440. FrameEnd1 = UART_ReadChar(ptUartRx, 6 + DataLength);
  441. FrameEnd2 = UART_ReadChar(ptUartRx, 7 + DataLength);
  442. if((CheckSumData == CheckSumResult) && (FrameEnd1 == UARTFRAME_END1) && (FrameEnd2 == UARTFRAME_END2))// 比较校验和帧尾
  443. {
  444. //数据处理
  445. UART1_DataProcess(Mode, Cmd, &Data[4]);//Mode为帧模式,Cmd为命令字,Data为数据段
  446. if(DataLength >= 4)//数据段包含ID+Cmd,至少4bytes
  447. {
  448. ID = (uint16_t)((Data[4]<<8) + Data[5]);
  449. Cmd = (uint16_t)((Data[6]<<8) + Data[7]);
  450. SendCmdData(&UART_TxBuff_Struct3, Mode, ID, Cmd, &Data[8]);
  451. }
  452. if((Cmd==0xC109) || (Cmd==0xC202) || (Cmd==0xC302) || (Cmd==0xC402) || (Cmd==0xC502))
  453. {
  454. T_UART1_BaudrateSwitch = HAL_GetTick();
  455. }
  456. //清除缓存
  457. UART_DelChar(ptUartRx, DataLength + 8);
  458. //通信指示
  459. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  460. }
  461. else
  462. {
  463. UART_DelChar(ptUartRx, 1);
  464. }
  465. }
  466. }
  467. else
  468. {
  469. UART_DelChar(ptUartRx, 1);
  470. }
  471. }
  472. else
  473. {
  474. UART_DelChar(ptUartRx, 1);
  475. }
  476. }
  477. else
  478. {
  479. UART_DelChar(ptUartRx, 1);
  480. }
  481. }
  482. }
  483. //串口3数据解析
  484. void Uart3_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
  485. {
  486. uint8_t Mode, CmdLength, DataLength;
  487. static uint8_t Data[255], CRC_Buf[255];
  488. uint16_t Cmd, i, ID;
  489. uint32_t CrcResult, CrcData;
  490. uint8_t FrameBegin1, FrameBegin2;
  491. static uint32_t DelayTimeCnt = 0;
  492. static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
  493. if(ptUartRx->ucBufCnt >= 13)
  494. {
  495. //读取帧头
  496. FrameBegin1 = UART_ReadChar(ptUartRx, 0);
  497. CRC_Buf[0] = FrameBegin1;
  498. FrameBegin2 = UART_ReadChar(ptUartRx, 1);
  499. CRC_Buf[1] = FrameBegin2;
  500. if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
  501. {
  502. CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
  503. CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
  504. ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3];
  505. //读取帧模式
  506. Mode = UART_ReadChar(ptUartRx, 4);
  507. CRC_Buf[4] = Mode;
  508. if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
  509. {
  510. //读取命令段长度和命令字
  511. CmdLength = UART_ReadChar(ptUartRx, 5);
  512. CRC_Buf[5] = CmdLength;
  513. Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
  514. CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
  515. CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
  516. DataLength = UART_ReadChar(ptUartRx, 7);
  517. if((CmdLength - DataLength) == 2)
  518. {
  519. if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
  520. {
  521. if(IsWaitRX_Flag == FALSE)
  522. {
  523. DelayTimeCnt = HAL_GetTick();
  524. IsWaitRX_Flag = TRUE;
  525. }
  526. if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
  527. {
  528. UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
  529. IsWaitRX_Flag = FALSE;
  530. }
  531. return;
  532. }
  533. else
  534. {
  535. IsWaitRX_Flag = FALSE;
  536. //接收到完整正确数据包
  537. for(i=0; i<DataLength; i++)//读取数据段
  538. {
  539. Data[i] = UART_ReadChar(ptUartRx, 8 + i);
  540. CRC_Buf[8 + i] = Data[i];
  541. }
  542. CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
  543. (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
  544. (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
  545. UART_ReadChar(ptUartRx, 11 + DataLength);
  546. CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
  547. if((CrcData - CrcResult) == 0) // 比较校验
  548. {
  549. //数据处理
  550. UART3_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
  551. //数据转发CAN
  552. memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
  553. CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
  554. CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
  555. CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
  556. CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
  557. CRC_Buf[10 + DataLength] = FRAME_END;
  558. CAN_SendData(ID, CRC_Buf, DataLength + 11);
  559. UART_TransferData(&UART_TxBuff_Struct1, UARTADDR_CDL, Mode, ID, Cmd, Data);
  560. //清除缓存
  561. UART_DelChar(ptUartRx, CmdLength + 11);
  562. //通信指示
  563. HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
  564. return;
  565. }
  566. UART_DelChar(ptUartRx, 1);
  567. }
  568. }
  569. else
  570. {
  571. UART_DelChar(ptUartRx, 1);
  572. }
  573. }
  574. else
  575. {
  576. UART_DelChar(ptUartRx, 1);
  577. }
  578. }
  579. else
  580. {
  581. UART_DelChar(ptUartRx, 1);
  582. }
  583. }
  584. }