AppTaskUart1.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2022-02-10 11:43:56
  4. * @Version : V3.0
  5. * @LastEditors: chenjie
  6. * @LastEditTime: 2022-11-10
  7. * @Description : file content
  8. * @FilePath: \S32K146_4G\code\app\AppTaskUart1.c
  9. */
  10. /*
  11. * AppTaskUart2.c
  12. * 4G的串口函数
  13. * Created on: 2022年2月10日
  14. * Author: QiXiang_CHENJIE
  15. */
  16. #include "AppTaskUart1.h"
  17. #include "hal_fls.h"
  18. const ATCmdFunc Atcmdfunc[] = {
  19. {AT_CMD_TEST, "AT\r\n", at_callbackFunc},
  20. {AT_ATE0, "ATE0\r\n", at_callbackFunc},
  21. {AT_SIMREADY, "AT+CPIN?\r\n", at_callbackFunc},
  22. {AT_GETICCID, "AT+CICCID\r\n", at_callbackFunc},
  23. {AT_CGREG, "AT+CGREG?\r\n", at_callbackFunc},
  24. {AT_CSQ, "AT+CSQ\r\n", at_callbackFunc},
  25. {AT_NETOPEN, "AT+NETOPEN\r\n", at_callbackFunc},
  26. {AT_CGIP, "AT+CDNSGIP=", at_callbackFunc},
  27. //{AT_CONNECT, "AT+CIPOPEN=0,\"TCP\",\"120.26.68.165\",14319\r\n", at_callbackFunc},
  28. {AT_CONNECT, "AT+CIPOPEN=", at_callbackFunc},
  29. {AT_CONNECTCHK, "AT+CIPOPEN?\r\n", at_callbackFunc},
  30. {AT_SEND, "AT+CIPSEND=", at_callbackFunc},
  31. {AT_DISCON, "AT+CIPCLOSE=0\r\n", at_callbackFunc},
  32. {AT_NETCLOSE, "AT+NETCLOSE\r\n", at_callbackFunc},
  33. {AT_CGNSSPWR, "AT+CGNSSPWR=1\r\n", at_callbackFunc}};
  34. static process_Tcp gProcess_Tcp_Task = PROCESS_TCP_INIT;
  35. #define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a)
  36. void InitFunc(void);
  37. sint8 TcpConnectFunc(sint8 *ConnectId);
  38. sint8 TcpDataSendFunc(sint8 ConnectId);
  39. sint8 TcpRegisterChkFunc(void);
  40. void TcpDataEncode(uint32 *PtrSendAddr, uint16 *SendLen);
  41. void GetUtc8Time(UTC8TimeType *UTC8TimeTcp);
  42. static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, sint8 *retFunc, uint16 timeout);
  43. static void TcpDataInfoRecvHandle(uint8 *DataRecv, uint16 DataRecvLen);
  44. static void GetCSQValue(uint8 *out);
  45. void Fota_Ftp(uint8 *dataPtrIn);
  46. void tcpUdsFunc(uint8 *Ptr, uint8 *AnsPtr);
  47. static void vTimer1000msCallback(TimerHandle_t pxTimer);
  48. bool SendTimerFlg = false;
  49. static uint32 TcpSendTimeCounter = 0;
  50. extern boolean waitForSleepFlag;
  51. boolean Uart_4G_Task_Sleep_FLag = false;
  52. void Uart_4G_Task(void *pvParameters)
  53. {
  54. (void)pvParameters;
  55. uint32 RecvTimerDelay = 0;
  56. uint8 UartRecvPtr[512];
  57. volatile uint16 tcpErrorCounter = 0;
  58. uint16 pReadLen = 0;
  59. // 4G开机
  60. Dio_LevelType _4G_Status = 0; // 0-关机,1-开机
  61. // Dio_WriteChannel(DioConf_DioChannel_PTA6_GPIO_OUT_MCU_4G_POW_EN, STD_ON);
  62. // vTaskDelay(pdMS_TO_TICKS(500));
  63. // Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_ON);
  64. // vTaskDelay(pdMS_TO_TICKS(50));
  65. // AtcmdDelayRecvFunc(UART_LPUART1,(char *)("SMS DONE"),30000);
  66. // Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_OFF);
  67. // _4G_Status = Dio_ReadChannel(DioConf_DioChannel_PTB1_GPIO_IN_MCU_4G_STATUS);
  68. // InitFunc(); // 4G模块初始化,注:AT同步不通过,没有进行次数判定及跳转
  69. TimerHandle_t monitorTimer1000ms;
  70. monitorTimer1000ms = xTimerCreate("monitor1000ms", 1000, pdTRUE, (void *)0, vTimer1000msCallback);
  71. xTimerStart(monitorTimer1000ms, 0);
  72. for (;;)
  73. {
  74. if (waitForSleepFlag == true)
  75. {
  76. PROC_TCP_STATE_SWITCH(PROCESS_TCP_SLEEP);
  77. }
  78. switch (gProcess_Tcp_Task)
  79. {
  80. case PROCESS_TCP_INIT:
  81. {
  82. Dio_WriteChannel(DioConf_DioChannel_PTA6_GPIO_OUT_MCU_4G_POW_EN, STD_ON);
  83. vTaskDelay(pdMS_TO_TICKS(500));
  84. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_ON);
  85. vTaskDelay(pdMS_TO_TICKS(50));
  86. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_OFF);
  87. AtcmdDelayRecvFunc(UART_LPUART1, (char *)("SMS DONE"), 30000);
  88. _4G_Status = Dio_ReadChannel(DioConf_DioChannel_PTB1_GPIO_IN_MCU_4G_STATUS);
  89. InitFunc();
  90. TcpSendTimeCounter = 0;
  91. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  92. break;
  93. }
  94. case PROCESS_TCP_IDLE: // 空闲状态
  95. {
  96. UART_Receive_Data(UART_LPUART1, UartRecvPtr, &pReadLen, 100); // 100ms检测
  97. if (SocketId < 0)
  98. {
  99. PROC_TCP_STATE_SWITCH(PROCESS_TCP_REGCHK);
  100. }
  101. else if (pReadLen > 0)
  102. {
  103. PROC_TCP_STATE_SWITCH(PROCESS_TCP_RECV);
  104. RecvTimerDelay = TimerCounter;
  105. }
  106. else if (AppConfigInfo.eolFlg == 1 && TcpWorkState == 0 && SendTimerFlg == true)
  107. {
  108. SendTimerFlg = false;
  109. PROC_TCP_STATE_SWITCH(PROCESS_TCP_SEND);
  110. }
  111. if ((TimerCounter - RecvTimerDelay) >= 30000 && TcpWorkState == 1) // 10s内没有命令下发,进行正常发送任务
  112. {
  113. TcpWorkState = 0;
  114. }
  115. break;
  116. }
  117. case PROCESS_TCP_REGCHK: // 驻网检查,包括SIM,CPIN检查,ICCID获取
  118. {
  119. RegChkRet = TcpRegisterChkFunc();
  120. if (RegChkRet > 0) // 检查通过,SIM卡已就绪,已进行驻网
  121. {
  122. PROC_TCP_STATE_SWITCH(PROCESS_TCP_CONNECT);
  123. }
  124. else
  125. {
  126. PROC_TCP_STATE_SWITCH(PROCESS_TCP_ERROR);
  127. }
  128. break;
  129. }
  130. case PROCESS_TCP_CONNECT: // 网络连接,包括域名转换
  131. {
  132. if (SocketId < 0)
  133. {
  134. sint8 ConnectRet = 0;
  135. ConnectRet = TcpConnectFunc(&SocketId);
  136. if (ConnectRet > 0)
  137. {
  138. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  139. TcpSendTimeCounter = 0;
  140. }
  141. else
  142. {
  143. PROC_TCP_STATE_SWITCH(PROCESS_TCP_ERROR);
  144. }
  145. }
  146. break;
  147. }
  148. case PROCESS_TCP_SEND: // 网络数据发送
  149. {
  150. sint8 SendRet = -1;
  151. SendRet = TcpDataSendFunc(SocketId);
  152. if (SendRet == 0)
  153. {
  154. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  155. tcpErrorCounter = 0;
  156. }
  157. else
  158. {
  159. SocketId = -1;
  160. PROC_TCP_STATE_SWITCH(PROCESS_TCP_ERROR);
  161. }
  162. break;
  163. }
  164. case PROCESS_TCP_RECV: // 网络数据接收,100ms空闲状态下即可接收
  165. {
  166. if (pReadLen > 0 && SocketId >= 0)
  167. {
  168. TcpDataInfoRecvHandle(UartRecvPtr, pReadLen);
  169. }
  170. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  171. break;
  172. }
  173. case PROCESS_TCP_HEART: // 心跳包发送
  174. {
  175. break;
  176. }
  177. case PROCESS_TCP_SLEEP: // 网络休眠状态
  178. {
  179. while (waitForSleepFlag == true)
  180. {
  181. Uart_4G_Task_Sleep_FLag = true;
  182. vTaskDelay(pdMS_TO_TICKS(1000));
  183. }
  184. PROC_TCP_STATE_SWITCH(PROCESS_TCP_INIT);
  185. break;
  186. }
  187. case PROCESS_TCP_ERROR: // 错误状态
  188. {
  189. vTaskDelay(pdMS_TO_TICKS(1000));
  190. tcpErrorCounter++;
  191. if (tcpErrorCounter > 60 && TcpSysReboot == 1) // 无法驻网或者联网
  192. {
  193. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_ON);
  194. vTaskDelay(pdMS_TO_TICKS(2500));
  195. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_OFF);
  196. vTaskDelay(pdMS_TO_TICKS(2500));
  197. tcpErrorCounter = 0;
  198. PROC_TCP_STATE_SWITCH(PROCESS_TCP_INIT);
  199. }
  200. else
  201. {
  202. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  203. }
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. static void vTimer1000msCallback(TimerHandle_t pxTimer)
  210. {
  211. uint32 ulTimerID;
  212. ulTimerID = (uint32)pvTimerGetTimerID(pxTimer);
  213. if (ulTimerID == 0)
  214. {
  215. SendTimerFlg = true;
  216. TcpSendTimeCounter++;
  217. }
  218. }
  219. sint8 TcpDataSendFunc(sint8 ConnectId)
  220. {
  221. sint8 outValue = -1;
  222. uint32 pSendDataAddr = 0;
  223. uint16 DataSendLen = 0;
  224. TcpDataEncode(&pSendDataAddr, &DataSendLen); // 数据组包,malloc申请在里面,pSendData指向申请的地址
  225. if (DataSendLen == 0)
  226. {
  227. return 0; // 暂时无数据可以发
  228. }
  229. outValue = tcpipConnectionSend(ConnectId, (uint8 *)pSendDataAddr, DataSendLen); // 发送函数
  230. if (pSendDataAddr != 0)
  231. {
  232. vPortFree((uint8 *)(pSendDataAddr));
  233. }
  234. pSendDataAddr = 0;
  235. return outValue;
  236. }
  237. sint8 tcpipConnectionSend(uint8 TcpConnectId, uint8 *SendDataPtr, uint16 SendDataLen)
  238. {
  239. sint8 outValue = -1;
  240. uint8 sendErrConuter = 0;
  241. uint16 ReadLen = 0;
  242. char AtCmdSend[30] = {0};
  243. uint8 AtCmdSendTotalLen = 0;
  244. uint8 UartRecvPtr[128]; // 暂存buffer改大
  245. uint8 ret = 0;
  246. sprintf(AtCmdSend, "AT+CIPSEND=%d,%d\r\n", TcpConnectId, SendDataLen);
  247. AtCmdSendTotalLen = mstrlen(AtCmdSend);
  248. while (outValue != 0 && sendErrConuter < 3)
  249. {
  250. ret = UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)AtCmdSend, AtCmdSendTotalLen, UartRecvPtr, &ReadLen, 1000);
  251. if (((ret == 0) && (ReadLen > 0) && ((uint8 *)strstr((char *)UartRecvPtr, (char *)(">")))) || 1) // 此IF条件默认通过
  252. {
  253. UART_Send_Data(UART_LPUART1, (uint8 *)SendDataPtr, SendDataLen, 1000);
  254. sint8 ret = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CIPSEND"), 2000);
  255. if (ret == 0)
  256. {
  257. outValue = 0;
  258. }
  259. else
  260. {
  261. outValue = -2;
  262. sendErrConuter++;
  263. }
  264. }
  265. else
  266. {
  267. outValue = -1;
  268. sendErrConuter++;
  269. }
  270. }
  271. return outValue;
  272. }
  273. sint8 TcpConnectFunc(sint8 *ConnectId)
  274. {
  275. uint8 ConnectStep = 1;
  276. sint8 ChkState = 0;
  277. sint8 ATRet = -1;
  278. uint8 UartRecvPtr[100];
  279. while (1)
  280. {
  281. switch (ConnectStep)
  282. {
  283. case 1: // AT指令同步
  284. {
  285. char *ATCmdSend = (char *)("ATE0\r\n");
  286. uint8 ATCmdSendLen = mstrlen(ATCmdSend);
  287. uint8 ReadLen = 0;
  288. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)ATCmdSend, ATCmdSendLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(1000));
  289. uint8 *retptr = NULL;
  290. if (ReadLen > 0)
  291. {
  292. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("OK"));
  293. if (retptr)
  294. {
  295. ConnectStep++;
  296. }
  297. else
  298. {
  299. ChkState = -ConnectStep;
  300. return ChkState;
  301. }
  302. }
  303. else
  304. {
  305. ChkState = -ConnectStep;
  306. return ChkState;
  307. }
  308. break;
  309. }
  310. case 2: // Netopen
  311. {
  312. AtcmdTransmit(AT_NETOPEN, NULL, 0, &ATRet, 100);
  313. if (ATRet == 0)
  314. {
  315. ConnectStep++;
  316. }
  317. else
  318. {
  319. ChkState = -ConnectStep;
  320. return ChkState;
  321. }
  322. break;
  323. }
  324. case 3: // 连接检查
  325. {
  326. AtcmdTransmit(AT_CONNECTCHK, NULL, 0, &ATRet, 1000); // ATret返回的值是连接id,如果未连接返回-1
  327. if (ATRet >= 0)
  328. {
  329. *ConnectId = ATRet;
  330. return 2;
  331. }
  332. else
  333. {
  334. ConnectStep++;
  335. }
  336. break;
  337. }
  338. case 4: // 域名转换
  339. {
  340. char AtCmdSend[30] = {0};
  341. uint8 AtCmdSendLen = 0;
  342. AtCmdSendLen = mstrlen(WebSiteName);
  343. memcpy(AtCmdSend, WebSiteName, AtCmdSendLen);
  344. memcpy(AtCmdSend + AtCmdSendLen, (char *)CRLF, sizeof(CRLF));
  345. AtCmdSendLen = AtCmdSendLen + 2;
  346. AtcmdTransmit(AT_CGIP, (uint8 *)AtCmdSend, AtCmdSendLen, &ATRet, 5000);
  347. if (ATRet == 0)
  348. {
  349. ConnectStep++;
  350. }
  351. else
  352. {
  353. ChkState = -ConnectStep;
  354. return ChkState;
  355. }
  356. break;
  357. }
  358. case 5: // 创建连接
  359. {
  360. char AtCmdSend[50] = {0};
  361. uint8 AtCmdSendTotalLen = 0;
  362. *ConnectId = 0;
  363. /*IP测试更改*/
  364. // char *ATCmdSend = (char *)("\"120.26.68.165\"");
  365. // memset(WebSiteIp,0x00,sizeof(WebSiteIp));
  366. // memcpy(WebSiteIp ,ATCmdSend,strlen(ATCmdSend));
  367. // WebSitePort = 14401;
  368. /**/
  369. sprintf(AtCmdSend, "AT+CIPOPEN=%d,\"TCP\",%s,%d\r\n", *ConnectId, WebSiteIp, WebSitePort); // 此处需要优化
  370. AtCmdSendTotalLen = mstrlen(AtCmdSend);
  371. UART_Send_Data(UART_LPUART1, AtCmdSend, AtCmdSendTotalLen, pdMS_TO_TICKS(100));
  372. ATRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CIPOPEN: 0,0"), 20000);
  373. if (ATRet == 0)
  374. {
  375. ConnectStep++;
  376. }
  377. else
  378. {
  379. ChkState = -ConnectStep;
  380. *ConnectId = -1;
  381. return ChkState;
  382. }
  383. break;
  384. }
  385. default:
  386. ChkState = ConnectStep;
  387. return ChkState;
  388. }
  389. }
  390. }
  391. sint8 TcpRegisterChkFunc(void)
  392. {
  393. uint8 RegChkStep = 0;
  394. sint8 ChkState = 0; // 默认为0
  395. sint8 ATRet = -1;
  396. while (1)
  397. {
  398. switch (RegChkStep)
  399. {
  400. case 0: // AT指令同步
  401. {
  402. AtcmdTransmit(AT_CMD_TEST, NULL, 0, &ATRet, 100);
  403. if (ATRet == 0)
  404. {
  405. RegChkStep++;
  406. }
  407. else
  408. {
  409. ChkState = -RegChkStep;
  410. return ChkState;
  411. }
  412. break;
  413. }
  414. case 1: // CPIN检查
  415. {
  416. AtcmdTransmit(AT_SIMREADY, NULL, 0, &ATRet, 100);
  417. if (ATRet == 0)
  418. {
  419. RegChkStep++;
  420. }
  421. else
  422. {
  423. ChkState = -RegChkStep;
  424. return ChkState;
  425. }
  426. break;
  427. }
  428. case 2:
  429. {
  430. AtcmdTransmit(AT_CGREG, NULL, 0, &ATRet, 1000); // 驻网检查,返回值1和5是驻网成功
  431. if (ATRet == 1 || ATRet == 5)
  432. {
  433. RegChkStep++;
  434. }
  435. else
  436. {
  437. ChkState = -RegChkStep;
  438. return ChkState;
  439. }
  440. break;
  441. }
  442. default:
  443. ChkState = RegChkStep;
  444. return ChkState;
  445. break;
  446. }
  447. }
  448. }
  449. void InitFunc(void)
  450. {
  451. // 4G模块初始化
  452. uint8 _4G_InitStep = 0;
  453. sint8 ATRet = -1;
  454. uint16 ReadLen = 0;
  455. char *ATCmdSend = NULL;
  456. uint8 ATCmdSendLen = 0;
  457. uint8 UartRecvPtr[50];
  458. uint16 ATerrorCnt = 0;
  459. uint8 *retptr = NULL;
  460. while (1)
  461. {
  462. switch (_4G_InitStep)
  463. {
  464. case 0: // AT指令同步
  465. {
  466. AtcmdTransmit(AT_CMD_TEST, NULL, 0, &ATRet, 100);
  467. if (ATRet == 0)
  468. {
  469. ATerrorCnt = 0;
  470. _4G_InitStep++;
  471. }
  472. else
  473. {
  474. vTaskDelay(pdMS_TO_TICKS(1000));
  475. ATerrorCnt++;
  476. if (ATerrorCnt > 60 * 5)
  477. {
  478. SystemSoftwareReset();
  479. }
  480. }
  481. break;
  482. }
  483. case 1: // 关闭回显
  484. {
  485. ATCmdSend = (char *)("ATE0\r\n");
  486. ATCmdSendLen = mstrlen(ATCmdSend);
  487. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)ATCmdSend, ATCmdSendLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(10000));
  488. if (ReadLen > 0)
  489. {
  490. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("OK"));
  491. if (retptr)
  492. {
  493. _4G_InitStep++;
  494. }
  495. else
  496. {
  497. _4G_InitStep = 0;
  498. }
  499. }
  500. else
  501. {
  502. _4G_InitStep = 0;
  503. }
  504. break;
  505. }
  506. case 2: // IMEI获取
  507. {
  508. ATCmdSend = (char *)("AT+SIMEI?\r\n");
  509. ATCmdSendLen = mstrlen(ATCmdSend);
  510. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)ATCmdSend, ATCmdSendLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(1000));
  511. if (ReadLen > 0)
  512. {
  513. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("+SIMEI"));
  514. if (retptr)
  515. {
  516. memcpy(ImeiNum, retptr + 8, 15);
  517. _4G_InitStep++;
  518. }
  519. else
  520. {
  521. _4G_InitStep = 0;
  522. }
  523. }
  524. else
  525. {
  526. _4G_InitStep = 0;
  527. }
  528. break;
  529. }
  530. case 3: // 打开时间自动更新
  531. {
  532. ATCmdSend = (char *)("AT+CTZU=1\r\n");
  533. ATCmdSendLen = mstrlen(ATCmdSend);
  534. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)ATCmdSend, ATCmdSendLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(1000));
  535. if (ReadLen > 0)
  536. {
  537. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("OK"));
  538. if (retptr)
  539. {
  540. _4G_InitStep++;
  541. }
  542. }
  543. else
  544. {
  545. _4G_InitStep = 0;
  546. }
  547. break;
  548. }
  549. case 4: // ICCID获取
  550. {
  551. AtcmdTransmit(AT_GETICCID, NULL, 0, &ATRet, 1000);
  552. if (ATRet == 0)
  553. {
  554. _4G_InitStep++;
  555. }
  556. else
  557. {
  558. _4G_InitStep = 0;
  559. }
  560. break;
  561. }
  562. case 5:
  563. {
  564. ATCmdSend = (char *)("AT+CIPSENDMODE=0\r\n");
  565. ATCmdSendLen = mstrlen(ATCmdSend);
  566. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)ATCmdSend, ATCmdSendLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(100));
  567. if (ReadLen > 0)
  568. {
  569. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("OK"));
  570. if (retptr)
  571. {
  572. _4G_InitStep++;
  573. }
  574. }
  575. else
  576. {
  577. _4G_InitStep = 0;
  578. }
  579. }
  580. default:
  581. {
  582. return;
  583. break;
  584. }
  585. }
  586. }
  587. }
  588. static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, sint8 *retFunc, uint16 timeout)
  589. {
  590. uint16 ReadLen = 0;
  591. uint8 PtrATCmdSend[64];
  592. uint8 ATCmdFixedLen = 0;
  593. uint16 ATCmdTotalLen = 0;
  594. uint8 UartRecvPtr[256];
  595. ATCmdFixedLen = mstrlen(Atcmdfunc[CmdIdx].str);
  596. ATCmdTotalLen = ATCmdFixedLen + SetValueLen;
  597. memset(PtrATCmdSend, 0x00, ATCmdTotalLen + 1);
  598. memcpy(PtrATCmdSend, Atcmdfunc[CmdIdx].str, ATCmdFixedLen);
  599. if (SetValuePtr != NULL)
  600. {
  601. memcpy(&PtrATCmdSend[ATCmdFixedLen], SetValuePtr, SetValueLen);
  602. }
  603. UART_Query_Data(UART_LPUART1, UART_LPUART1, PtrATCmdSend, ATCmdTotalLen, UartRecvPtr, &ReadLen, timeout);
  604. if (ReadLen > 0)
  605. {
  606. *retFunc = Atcmdfunc[CmdIdx].cb((char *)PtrATCmdSend, UartRecvPtr, CmdIdx, ReadLen);
  607. }
  608. else
  609. {
  610. *retFunc = -1;
  611. }
  612. return;
  613. }
  614. sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen)
  615. {
  616. sint8 OutValue = -1;
  617. uint8 *retptr = NULL;
  618. char *OkAns = "OK";
  619. retptr = (uint8 *)strstr((char *)pReadStr, OkAns);
  620. switch (CmdIdx)
  621. {
  622. case AT_CMD_TEST:
  623. {
  624. if (retptr)
  625. {
  626. OutValue = 0;
  627. }
  628. break;
  629. }
  630. case AT_SIMREADY:
  631. {
  632. if (retptr)
  633. {
  634. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("READY"));
  635. if (retptr)
  636. {
  637. OutValue = 0;
  638. }
  639. }
  640. break;
  641. }
  642. case AT_GETICCID:
  643. {
  644. if (retptr)
  645. {
  646. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("ICCID:"));
  647. if (retptr)
  648. {
  649. memcpy(IccidNum, retptr + 7, 20);
  650. OutValue = 0;
  651. }
  652. }
  653. break;
  654. }
  655. case AT_CGREG:
  656. {
  657. if (retptr)
  658. {
  659. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("CGREG:"));
  660. if (retptr)
  661. {
  662. uint8 RegN = 0;
  663. uint8 RegState = 0;
  664. RegN = CharToHex(*(retptr + 7));
  665. RegState = CharToHex(*(retptr + 9));
  666. OutValue = (RegState + RegN);
  667. return OutValue;
  668. }
  669. }
  670. break;
  671. }
  672. case AT_NETOPEN:
  673. {
  674. if (retptr)
  675. {
  676. OutValue = 0;
  677. }
  678. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("opened")); // 重复打开
  679. if (retptr)
  680. {
  681. OutValue = 0;
  682. }
  683. break;
  684. }
  685. case AT_CONNECTCHK:
  686. {
  687. if (retptr)
  688. {
  689. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("TCP"));
  690. if (retptr)
  691. {
  692. OutValue = CharToHex(*(retptr - 3));
  693. return OutValue;
  694. }
  695. }
  696. break;
  697. }
  698. case AT_CGIP:
  699. {
  700. if (retptr)
  701. {
  702. memset(WebSiteIp, 0x00, sizeof(WebSiteIp));
  703. for (uint8 i = 0; i < 30; i++)
  704. {
  705. if (*(retptr - i) == ',')
  706. {
  707. memcpy(WebSiteIp, retptr - i + 1, i - 5);
  708. OutValue = 0;
  709. break;
  710. }
  711. }
  712. }
  713. break;
  714. }
  715. case AT_CONNECT:
  716. {
  717. if (retptr)
  718. {
  719. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("CIPOPEN:"));
  720. if (retptr && pReadLen > 9)
  721. {
  722. SocketId = CharToHex(*(retptr + 9));
  723. }
  724. OutValue = 0;
  725. }
  726. break;
  727. }
  728. case AT_SEND:
  729. {
  730. retptr = (uint8 *)strstr((char *)pReadStr, (char *)(">"));
  731. if (retptr)
  732. {
  733. OutValue = 0;
  734. }
  735. else
  736. {
  737. OutValue = -1;
  738. }
  739. break;
  740. }
  741. default:
  742. break;
  743. }
  744. return OutValue;
  745. }
  746. void TcpDataEncode(uint32 *PtrSendAddr, uint16 *SendLen)
  747. {
  748. static UTC8TimeType UTC8TimeTcp;
  749. uint8 *SendBuffer = NULL;
  750. uint8 DataIdx = 0;
  751. if ((TcpSendTimeCounter) % (60 * 60) == 1)
  752. {
  753. DataIdx = VerMsg; // 版本信息发送
  754. }
  755. else if (TcpSendTimeCounter % 10 == 2)
  756. {
  757. DataIdx = TruckBattMsg; // 电池信息发送
  758. }
  759. else if ((TcpSendTimeCounter) % 60 == 3)
  760. {
  761. DataIdx = TruckVehiMsg; // 车辆信息发送
  762. }
  763. else if ((TcpSendTimeCounter) % 60 == 4)
  764. {
  765. DataIdx = TruckAcclMsg; // 累计信息发送
  766. }
  767. else if ((TcpSendTimeCounter) % 10 == 5)
  768. {
  769. DataIdx = GpsMsg; // 定位信息发送
  770. }
  771. else if ((TcpSendTimeCounter) % (60 * 60 * 2) == 6)
  772. {
  773. DataIdx = 0x01; // 时间校准
  774. }
  775. else if ((TcpSendTimeCounter) % 10 == 7)
  776. {
  777. DataIdx = DebugMsg; // 调试信息发送,含FFT结果
  778. }
  779. else
  780. {
  781. *SendLen = 0;
  782. DataIdx = 0; // 不发送,返回
  783. return;
  784. }
  785. memcpy(TcpbattSN, AppConfigInfo.deviceSn, 17);
  786. GetUtc8Time(&UTC8TimeTcp); // 时间获取
  787. vTaskDelay(pdMS_TO_TICKS(100));
  788. switch (DataIdx)
  789. {
  790. case 0x01: // 时间校准
  791. {
  792. char *ATCmdSend = (char *)("AT+CNTP\r\n");
  793. uint8 ATCmdSendLen = mstrlen(ATCmdSend);
  794. uint8 ReadLen = 0;
  795. uint8 UartRecvPtr[20];
  796. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)ATCmdSend, ATCmdSendLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(500));
  797. *SendLen = 0; // 不发送,长度为0
  798. return;
  799. }
  800. case 0x82:
  801. {
  802. GPSInfo GpsRecvData;
  803. GPSMsgtoTcpType GpsToTcpInfo;
  804. *SendLen = sizeof(GpsToTcpInfo);
  805. SendBuffer = pvPortMalloc(*SendLen);
  806. uint16 tac = 0;
  807. uint32 cellId = 0;
  808. uint8 DataLen = (uint16)sizeof(GpsToTcpInfo.gpsInfo);
  809. GpsToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  810. GpsToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  811. GpsToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  812. GpsToTcpInfo.ansSymbol = TCP_ANS_SYM;
  813. memcpy(GpsToTcpInfo.SN, TcpbattSN, BATT_SN_LEN);
  814. GpsToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  815. GpsToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  816. GpsToTcpInfo.dataLength[1] = DataLen & 0xFF;
  817. GpsToTcpInfo.gpsInfo.sendTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  818. GpsToTcpInfo.gpsInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  819. GpsToTcpInfo.gpsInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  820. GpsToTcpInfo.gpsInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  821. GpsToTcpInfo.gpsInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  822. GpsToTcpInfo.gpsInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  823. GpsToTcpInfo.gpsInfo.msgMark = DataIdx;
  824. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  825. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  826. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  827. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  828. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  829. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  830. if (xQueueReceive(GpsDataQueueHandle, &GpsRecvData, 0) == pdPASS)
  831. {
  832. memcpy((uint8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, (uint8 *)&GpsRecvData, sizeof(GPSInfo));
  833. }
  834. else
  835. {
  836. memset((uint8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, 0x00, sizeof(GPSInfo));
  837. }
  838. GpsToTcpInfo.gpsInfo.Tac[0] = tac >> 8;
  839. GpsToTcpInfo.gpsInfo.Tac[1] = tac & 0xFF;
  840. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 24;
  841. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 16;
  842. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 8;
  843. GpsToTcpInfo.gpsInfo.CellID[0] = cellId;
  844. uint16 xyzDatacache[3] = {0};
  845. memcpy(xyzDatacache, xyzData, 3 * sizeof(uint16));
  846. for (uint8 i = 0; i < 3; i++)
  847. {
  848. if (xyzDatacache[i] > 0x8000) // 数据为负
  849. {
  850. xyzDatacache[i] = 20000U + (sint16)xyzDatacache[i];
  851. }
  852. else
  853. {
  854. xyzDatacache[i] = xyzDatacache[i] + 20000U;
  855. }
  856. }
  857. GpsToTcpInfo.gpsInfo.xData[0] = xyzDatacache[0] >> 8;
  858. GpsToTcpInfo.gpsInfo.xData[1] = xyzDatacache[0];
  859. GpsToTcpInfo.gpsInfo.yData[0] = xyzDatacache[1] >> 8;
  860. GpsToTcpInfo.gpsInfo.yData[1] = xyzDatacache[1];
  861. GpsToTcpInfo.gpsInfo.zData[0] = xyzDatacache[2] >> 8;
  862. GpsToTcpInfo.gpsInfo.zData[1] = xyzDatacache[2];
  863. GpsToTcpInfo.CRC = bcc_chk((uint8 *)&GpsToTcpInfo, sizeof(GPSMsgtoTcpType) - 1);
  864. memcpy(SendBuffer, &GpsToTcpInfo, sizeof(GpsToTcpInfo));
  865. *PtrSendAddr = (uint32)SendBuffer;
  866. break;
  867. }
  868. case 0x86:
  869. {
  870. VersionMsgtoTcpType VerMsgToTcpInfo;
  871. *SendLen = sizeof(VersionMsgtoTcpType);
  872. SendBuffer = pvPortMalloc(*SendLen);
  873. uint16 DataLen = 0;
  874. DataLen = (uint16)sizeof(VerMsgToTcpInfo.VerInfo);
  875. VerMsgToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  876. VerMsgToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  877. VerMsgToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  878. VerMsgToTcpInfo.ansSymbol = TCP_ANS_SYM;
  879. memcpy(VerMsgToTcpInfo.SN, TcpbattSN, BATT_SN_LEN);
  880. VerMsgToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  881. VerMsgToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  882. VerMsgToTcpInfo.dataLength[1] = DataLen & 0xFF;
  883. VerMsgToTcpInfo.VerInfo.sendTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  884. VerMsgToTcpInfo.VerInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  885. VerMsgToTcpInfo.VerInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  886. VerMsgToTcpInfo.VerInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  887. VerMsgToTcpInfo.VerInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  888. VerMsgToTcpInfo.VerInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  889. VerMsgToTcpInfo.VerInfo.msgMark = DataIdx;
  890. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  891. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  892. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  893. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  894. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  895. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  896. memcpy(VerMsgToTcpInfo.VerInfo.ICCID, IccidNum, 20);
  897. memcpy(VerMsgToTcpInfo.VerInfo.IMEI, ImeiNum, 15);
  898. VerMsgToTcpInfo.VerInfo.BMSHwVersion[0] = BMS_HardwareVersion >> 8;
  899. VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = BMS_HardwareVersion;
  900. VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = BMS_SoftwareVersion >> 24;
  901. VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = BMS_SoftwareVersion >> 16;
  902. VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = BMS_SoftwareVersion >> 8;
  903. VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = BMS_SoftwareVersion;
  904. VerMsgToTcpInfo.VerInfo.HwVersion[0] = (HwVersion >> 8) & 0xFF;
  905. VerMsgToTcpInfo.VerInfo.HwVersion[1] = (HwVersion)&0xFF;
  906. VerMsgToTcpInfo.VerInfo.BLVersion[0] = (BlSwVersion >> 24) & 0xFF;
  907. VerMsgToTcpInfo.VerInfo.BLVersion[1] = (BlSwVersion >> 16) & 0xFF;
  908. VerMsgToTcpInfo.VerInfo.BLVersion[2] = (BlSwVersion >> 8) & 0xFF;
  909. VerMsgToTcpInfo.VerInfo.BLVersion[3] = (BlSwVersion)&0xFF;
  910. VerMsgToTcpInfo.VerInfo.DRVVersion[0] = (DrvSwVersion >> 24) & 0xFF;
  911. VerMsgToTcpInfo.VerInfo.DRVVersion[1] = (DrvSwVersion >> 16) & 0xFF;
  912. VerMsgToTcpInfo.VerInfo.DRVVersion[2] = (DrvSwVersion >> 8) & 0xFF;
  913. VerMsgToTcpInfo.VerInfo.DRVVersion[3] = (DrvSwVersion)&0xFF;
  914. VerMsgToTcpInfo.VerInfo.APPVersion[0] = (AppSwVersion >> 24) & 0xFF;
  915. VerMsgToTcpInfo.VerInfo.APPVersion[1] = (AppSwVersion >> 16) & 0xFF;
  916. VerMsgToTcpInfo.VerInfo.APPVersion[2] = (AppSwVersion >> 8) & 0xFF;
  917. VerMsgToTcpInfo.VerInfo.APPVersion[3] = (AppSwVersion)&0xFF;
  918. VerMsgToTcpInfo.VerInfo.BmsType = BmsManuFacture;
  919. VerMsgToTcpInfo.VerInfo.BmsInfo = BmsInfo;
  920. VerMsgToTcpInfo.VerInfo.DataModuleType = DataModuleType;
  921. VerMsgToTcpInfo.CRC = bcc_chk((uint8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo) - 1);
  922. memcpy(SendBuffer, &VerMsgToTcpInfo, sizeof(VersionMsgtoTcpType));
  923. *PtrSendAddr = (uint32)SendBuffer;
  924. break;
  925. }
  926. case DebugMsg:
  927. {
  928. char rbuf[512]={0};
  929. DebugMsgtoTcpType DebugMsgInfo;
  930. UINT16 DataLen = 0;
  931. UINT16 BufferLen = 0;
  932. sprintf((char *)rbuf, "{%.2f,%.2f,%.2f,%.2f,%.2f;\
  933. %.2f,%.2f,%.2f,%.2f,%.2f,}\
  934. {%.2f,%.2f,%.2f,%.2f,%.2f;\
  935. %.2f,%.2f,%.2f,%.2f,%.2f,}\
  936. {%.2f,%.2f,%.2f,%.2f,%.2f;\
  937. %.2f,%.2f,%.2f,%.2f,%.2f,}",
  938. returnFreq[0][0], returnFreq[0][1], returnFreq[0][2], returnFreq[0][3], returnFreq[0][4],
  939. returnP[0][0], returnP[0][1], returnP[0][2], returnP[0][3], returnP[0][4],
  940. returnFreq[1][0], returnFreq[1][1], returnFreq[1][2], returnFreq[1][3], returnFreq[1][4],
  941. returnP[1][0], returnP[1][1], returnP[1][2], returnP[1][3], returnP[1][4],
  942. returnFreq[2][0], returnFreq[2][1], returnFreq[2][2], returnFreq[2][3], returnFreq[2][4],
  943. returnP[2][0], returnP[2][1], returnP[2][2], returnP[2][3], returnP[2][4]);
  944. BufferLen = strlen((const char *)rbuf);
  945. *SendLen = BufferLen + sizeof(DebugMsgInfo);
  946. SendBuffer = pvPortMalloc(*SendLen);
  947. memcpy(SendBuffer + sizeof(DebugMsgInfo) - 1, rbuf, BufferLen);
  948. DataLen = sizeof(DebugMsgInfo.DebugInfo) + BufferLen;
  949. DebugMsgInfo.startSymbol[0] = TCP_START_SYM1;
  950. DebugMsgInfo.startSymbol[1] = TCP_START_SYM2;
  951. DebugMsgInfo.cmdSymbol = TCP_CMD_SYM;
  952. DebugMsgInfo.ansSymbol = TCP_ANS_SYM;
  953. memcpy(DebugMsgInfo.SN, TcpbattSN, BATT_SN_LEN);
  954. DebugMsgInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  955. DebugMsgInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  956. DebugMsgInfo.dataLength[1] = DataLen & 0xFF;
  957. DebugMsgInfo.DebugInfo.sendTimeUTC[0] = UTC8TimeTcp.year & 0xFF; // year
  958. DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  959. DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  960. DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  961. DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  962. DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  963. DebugMsgInfo.DebugInfo.msgMark = DebugMsg;
  964. DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen >> 8;
  965. DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen;
  966. memcpy(SendBuffer, (UINT8 *)&DebugMsgInfo, sizeof(DebugMsgInfo) - 1);
  967. DebugMsgInfo._CRC = bcc_chk(SendBuffer, BufferLen + sizeof(DebugMsgInfo) - 1);
  968. memcpy(SendBuffer + BufferLen + sizeof(DebugMsgInfo) - 1, &DebugMsgInfo._CRC, 1);
  969. *PtrSendAddr = (uint32)SendBuffer;
  970. break;
  971. }
  972. case 0x90:
  973. {
  974. *SendLen = 0x52;
  975. SendBuffer = pvPortMalloc(*SendLen);
  976. *(SendBuffer + 0) = TCP_START_SYM1;
  977. *(SendBuffer + 1) = TCP_START_SYM2;
  978. *(SendBuffer + 2) = TCP_CMD_SYM;
  979. *(SendBuffer + 3) = TCP_ANS_SYM;
  980. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  981. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  982. uint16 DataLen = 0x39; // 57
  983. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  984. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  985. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  986. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  987. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  988. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  989. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  990. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  991. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8 BATTMSG
  992. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  993. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  994. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  995. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  996. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  997. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  998. memcpy(SendBuffer + 0x25, VIN, 17);
  999. *(SendBuffer + 0x36) = (vehicleStatus & 0xFF); // uint8 整车状态
  1000. if (BMS_Mode == 1)
  1001. {
  1002. bmsHVOn = 1;
  1003. }
  1004. else if (BMS_Mode == 2)
  1005. {
  1006. bmsHVOn = 0;
  1007. }
  1008. else
  1009. {
  1010. bmsHVOn = 0xFF;
  1011. }
  1012. *(SendBuffer + 0x37) = (bmsHVOn & 0xFF); // uint8 bms上高压指令
  1013. *(SendBuffer + 0x38) = (currentGearPosition & 0xFF); // uint8 当前档位
  1014. *(SendBuffer + 0x39) = (parkingBreakStatus & 0xFF); // uint8 手刹信号
  1015. *(SendBuffer + 0x3A) = (breakingStatus & 0xFF); // uint8 制动开关
  1016. *(SendBuffer + 0x3B) = ((ODO >> 24) & 0xFF); // uint32 总里程
  1017. *(SendBuffer + 0x3C) = ((ODO >> 16) & 0xFF);
  1018. *(SendBuffer + 0x3D) = ((ODO >> 8) & 0xFF);
  1019. *(SendBuffer + 0x3E) = (ODO & 0xFF);
  1020. *(SendBuffer + 0x3F) = (dcdcWorkStatus & 0xFF); // uint8 DCDC状态
  1021. *(SendBuffer + 0x40) = (numOfChrgableSubsys & 0xFF); // uint8 可充电子系统数
  1022. *(SendBuffer + 0x41) = (chrgableSubsysCode & 0xFF); // uint8 可充电储能子系统号
  1023. *(SendBuffer + 0x42) = (BMS_MaxCellTempCSC & 0xFF); // uint8 最高温度子系统号 精度_1,偏移量_0,单位_
  1024. *(SendBuffer + 0x43) = (BMS_MaxCellTempNum & 0xFF); // uint8 最高温度探针单体代号 精度_1,偏移量_0,单位_
  1025. *(SendBuffer + 0x44) = (BMS_MinCellTempCSC & 0xFF); // uint8 最低温度子系统号 精度_1,偏移量_0,单位_
  1026. *(SendBuffer + 0x45) = (BMS_MinCellTempNum & 0xFF); // uint8 最低温度探针子系统代号 精度_1,偏移量_0,单位_
  1027. *(SendBuffer + 0x46) = (BMS_MaxCellVoltCSC & 0xFF); // uint8 最高电压电池子系统号 精度_1,偏移量_0,单位_
  1028. *(SendBuffer + 0x47) = ((BMS_MaxCellVoltNum >> 8) & 0xFF); // uint16 最高电压电池单体代号 精度_1,偏移量_0,单位_
  1029. *(SendBuffer + 0x48) = (BMS_MaxCellVoltNum & 0xFF);
  1030. *(SendBuffer + 0x49) = (BMS_MinCellVoltCSC & 0xFF); // uint8 最低电压电池子系统号 精度_1,偏移量_0,单位_
  1031. *(SendBuffer + 0x4A) = ((BMS_MinCellVoltNum >> 8) & 0xFF); // uint16 最低电压电池单体代号 精度_1,偏移量_0,单位_
  1032. *(SendBuffer + 0x4B) = (BMS_MinCellVoltNum & 0xFF);
  1033. *(SendBuffer + 0x4C) = (ebcStatus & 0xFF); // uint8 换电控制器状态
  1034. *(SendBuffer + 0x4D) = (uint8)((ebcAskHVOn & 0x01) | ((ebcAskHVOff & 0x01) << 1) | ((retainLockSignal & 0x01) << 3) | ((dischargeLockSignal & 0x01) << 5) | ((chargeLockSignal & 0x01) << 6));
  1035. *(SendBuffer + 0x4E) = (chargeFlag & 0xFF); // uint8 充电标志位
  1036. *(SendBuffer + 0x4F) = ((vcuDCVol >> 8) & 0xFF); // uint16 电机直流母线电压
  1037. *(SendBuffer + 0x50) = (vcuDCVol & 0xFF);
  1038. *(SendBuffer + 0x51) = bcc_chk(SendBuffer, 0x51);
  1039. *PtrSendAddr = (uint32)SendBuffer;
  1040. break;
  1041. }
  1042. case 0x91:
  1043. {
  1044. GetCSQValue(&CSQValue);
  1045. *SendLen = 0x54 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM);
  1046. SendBuffer = pvPortMalloc(*SendLen);
  1047. *(SendBuffer + 0) = TCP_START_SYM1;
  1048. *(SendBuffer + 1) = TCP_START_SYM2;
  1049. *(SendBuffer + 2) = TCP_CMD_SYM;
  1050. *(SendBuffer + 3) = TCP_ANS_SYM;
  1051. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1052. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1053. uint16 DataLen = 0x3B + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM);
  1054. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1055. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1056. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1057. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1058. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1059. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1060. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1061. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1062. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8 BATTMSG
  1063. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1064. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1065. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1066. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1067. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1068. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1069. *(SendBuffer + 0x25) = (CSQValue & 0xFF); // uint8 csq
  1070. *(SendBuffer + 0x26) = (BMS_Mode & 0xFF); // uint8 电池管理系统工作状态,0-初始化 1-自检完成允许上电 2-运行状态 3-高压切断 4-故障状态,精度_1,偏移量_0,单位_
  1071. *(SendBuffer + 0x27) = (BMS_CharGunSt & 0xFF); // uint8 直流充电枪连接状态,0-未连接 1-单枪连接 2-双枪连接 3-无效,精度_1,偏移量_0,单位_
  1072. *(SendBuffer + 0x28) = (BMS_FtLvl & 0xFF); // uint8 当前最高故障等级,0-正常 1-1级 轻微故障 2-2级 较严重故障 3-3级 最严重故障,精度_1,偏移量_0,单位_
  1073. *(SendBuffer + 0x29) = (BMS_FtCode & 0xFF); // uint8 故障码,见BMS_ErrCode,精度_1,偏移量_0,单位_
  1074. *(SendBuffer + 0x2A) = (BMS_ReqHVOff & 0xFF); // uint8 BMS下高压请求,0-无效 1-请求下高压 2-不请求下高压 3-无效,精度_1,偏移量_0,单位_
  1075. *(SendBuffer + 0x2B) = (BMS_CharSt & 0xFF); // uint8 充电状态,0-未充电 1-充电中 2-充电已完成 3-充电错误故障,精度_1,偏移量_0,单位_
  1076. *(SendBuffer + 0x2C) = (BMS_PackSOC & 0xFF); // uint8 电池包SOC 精度_0.4,偏移量_0,单位_
  1077. *(SendBuffer + 0x2D) = (BMS_PackSOH / 25 * 10) & 0xFF; // SOH 精度1%
  1078. *(SendBuffer + 0x2E) = (BMS_BattVolt >> 8) & 0xFF; // 电池包总电压
  1079. *(SendBuffer + 0x2F) = (BMS_BattVolt & 0xFF);
  1080. *(SendBuffer + 0x30) = ((BMS_PackCurr >> 8) & 0xFF); // uint16 电池包总电流,充电为负值,放电为正值 精度_0.1,偏移量_-1000,单位_A
  1081. *(SendBuffer + 0x31) = (BMS_PackCurr & 0xFF);
  1082. *(SendBuffer + 0x32) = ((BMS_SysInsRes >> 8) & 0xFF); // uint16 系统绝缘电阻 精度_1,偏移量_0,单位_KΩ
  1083. *(SendBuffer + 0x33) = (BMS_SysInsRes & 0xFF);
  1084. /*28 Byte 继电器状态*/
  1085. *(SendBuffer + 0x34) = (BMS_StPosRly & 0xFF); // uint8 主正继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1086. *(SendBuffer + 0x35) = (BMS_StPreCharRly & 0xFF); // uint8 预充继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1087. *(SendBuffer + 0x36) = (BMS_StNegRly & 0xFF); // uint8 主负继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1088. *(SendBuffer + 0x37) = 0; // 三合一
  1089. *(SendBuffer + 0x38) = 0; // PTC
  1090. *(SendBuffer + 0x39) = 0; // 空调
  1091. *(SendBuffer + 0x3A) = (BMS_StPosCharRly1 & 0xFF); // uint8 直流充正继电器1状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1092. *(SendBuffer + 0x3B) = (BMS_StNegCharRly1 & 0xFF); // uint8 直流充负继电器1状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1093. *(SendBuffer + 0x3C) = (BMS_StPosCharRly2 & 0xFF); // uint8 直流充正继电器2状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1094. *(SendBuffer + 0x3D) = (BMS_StNegCharRly2 & 0xFF); // uint8 直流充负继电器2状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1095. *(SendBuffer + 0x3E) = (BMS_StPosHeatRly & 0xFF); // uint8 加热正继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1096. *(SendBuffer + 0x3F) = (BMS_StNegHeatRly & 0xFF); // uint8 加热负继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1097. *(SendBuffer + 0x40) = 0;
  1098. /*28-Byte继电器状态结束*/
  1099. *(SendBuffer + 0x50) = ((BMS_CellTotal >> 8) & 0xFF); // uint16 PACK中单体电芯的总数目
  1100. *(SendBuffer + 0x51) = (BMS_CellTotal & 0xFF);
  1101. for (uint16 index = 0; index < min(BMS_CellTotal, BMS_CELL_MAX_NUM); index++)
  1102. {
  1103. *(SendBuffer + 0x52 + index * 2) = ((BMS_CellVolt[index] >> 8) & 0xFF);
  1104. *(SendBuffer + 0x52 + index * 2 + 1) = ((BMS_CellVolt[index]) & 0xFF);
  1105. }
  1106. *(SendBuffer + 0x52 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2) = (BMS_TempTotal & 0xFF); // uint8 PACK中电芯温度点(探针)的总数目
  1107. memcpy(SendBuffer + 0x53 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2, BMS_CellTemp, min(BMS_TempTotal, BMS_TEMP_MAX_NUM));
  1108. *(SendBuffer + 0x53 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM)) = bcc_chk(SendBuffer, 0x53 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM));
  1109. *PtrSendAddr = (uint32)SendBuffer;
  1110. break;
  1111. }
  1112. case 0x92:
  1113. {
  1114. *SendLen = 0x4E;
  1115. SendBuffer = pvPortMalloc(*SendLen);
  1116. *(SendBuffer + 0) = TCP_START_SYM1;
  1117. *(SendBuffer + 1) = TCP_START_SYM2;
  1118. *(SendBuffer + 2) = TCP_CMD_SYM;
  1119. *(SendBuffer + 3) = TCP_ANS_SYM;
  1120. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1121. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1122. uint16 DataLen = 0x35;
  1123. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1124. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1125. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1126. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1127. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1128. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1129. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1130. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1131. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8
  1132. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1133. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1134. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1135. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1136. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1137. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1138. *(SendBuffer + 0x25) = ((BMS_TotalCharCapy >> 24) & 0xFF); // uint32 累积充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1139. *(SendBuffer + 0x26) = ((BMS_TotalCharCapy >> 16) & 0xFF);
  1140. *(SendBuffer + 0x27) = ((BMS_TotalCharCapy >> 8) & 0xFF);
  1141. *(SendBuffer + 0x28) = (BMS_TotalCharCapy & 0xFF);
  1142. *(SendBuffer + 0x29) = ((BMS_TotalDisCharCapy >> 24) & 0xFF); // uint32 累积放出Ah 数 精度_0.1,偏移量_0,单位_Ah
  1143. *(SendBuffer + 0x2A) = ((BMS_TotalDisCharCapy >> 16) & 0xFF);
  1144. *(SendBuffer + 0x2B) = ((BMS_TotalDisCharCapy >> 8) & 0xFF);
  1145. *(SendBuffer + 0x2C) = (BMS_TotalDisCharCapy & 0xFF);
  1146. *(SendBuffer + 0x2D) = ((BMS_TotalCharEngy >> 24) & 0xFF); // uint32 累计充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1147. *(SendBuffer + 0x2E) = ((BMS_TotalCharEngy >> 16) & 0xFF);
  1148. *(SendBuffer + 0x2F) = ((BMS_TotalCharEngy >> 8) & 0xFF);
  1149. *(SendBuffer + 0x30) = (BMS_TotalCharEngy & 0xFF);
  1150. *(SendBuffer + 0x31) = ((BMS_TotalDisCharEngy >> 24) & 0xFF); // uint32 累计放出kWh 数 精度_0.1,偏移量_0,单位_KWh
  1151. *(SendBuffer + 0x32) = ((BMS_TotalDisCharEngy >> 16) & 0xFF);
  1152. *(SendBuffer + 0x33) = ((BMS_TotalDisCharEngy >> 8) & 0xFF);
  1153. *(SendBuffer + 0x34) = (BMS_TotalDisCharEngy & 0xFF);
  1154. *(SendBuffer + 0x35) = ((BMS_TotalBackCharCapy >> 24) & 0xFF); // uint32 累计动能回馈充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1155. *(SendBuffer + 0x36) = ((BMS_TotalBackCharCapy >> 16) & 0xFF);
  1156. *(SendBuffer + 0x37) = ((BMS_TotalBackCharCapy >> 8) & 0xFF);
  1157. *(SendBuffer + 0x38) = (BMS_TotalBackCharCapy & 0xFF);
  1158. *(SendBuffer + 0x39) = ((BMS_TotalBackCharEngy >> 24) & 0xFF); // uint32 累计动能回馈充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1159. *(SendBuffer + 0x3A) = ((BMS_TotalBackCharEngy >> 16) & 0xFF);
  1160. *(SendBuffer + 0x3B) = ((BMS_TotalBackCharEngy >> 8) & 0xFF);
  1161. *(SendBuffer + 0x3C) = (BMS_TotalBackCharEngy & 0xFF);
  1162. *(SendBuffer + 0x3D) = ((BMS_TotalStaCharCapy >> 24) & 0xFF); // uint32 累计换电站充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1163. *(SendBuffer + 0x3E) = ((BMS_TotalStaCharCapy >> 16) & 0xFF);
  1164. *(SendBuffer + 0x3F) = ((BMS_TotalStaCharCapy >> 8) & 0xFF);
  1165. *(SendBuffer + 0x40) = (BMS_TotalStaCharCapy & 0xFF);
  1166. *(SendBuffer + 0x41) = ((BMS_TotalStaCharEngy >> 24) & 0xFF); // uint32 累计换电站充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1167. *(SendBuffer + 0x42) = ((BMS_TotalStaCharEngy >> 16) & 0xFF);
  1168. *(SendBuffer + 0x43) = ((BMS_TotalStaCharEngy >> 8) & 0xFF);
  1169. *(SendBuffer + 0x44) = (BMS_TotalStaCharEngy & 0xFF);
  1170. *(SendBuffer + 0x45) = ((BMS_TotalGunCharCapy >> 24) & 0xFF); // uint32 累计插枪充电充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1171. *(SendBuffer + 0x46) = ((BMS_TotalGunCharCapy >> 16) & 0xFF);
  1172. *(SendBuffer + 0x47) = ((BMS_TotalGunCharCapy >> 8) & 0xFF);
  1173. *(SendBuffer + 0x48) = (BMS_TotalGunCharCapy & 0xFF);
  1174. *(SendBuffer + 0x49) = ((BMS_TotalGunCharEngy >> 24) & 0xFF); // uint32 累计插枪充电充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1175. *(SendBuffer + 0x4A) = ((BMS_TotalGunCharEngy >> 16) & 0xFF);
  1176. *(SendBuffer + 0x4B) = ((BMS_TotalGunCharEngy >> 8) & 0xFF);
  1177. *(SendBuffer + 0x4C) = (BMS_TotalGunCharEngy & 0xFF);
  1178. *(SendBuffer + 0x4D) = bcc_chk(SendBuffer, 0x4D);
  1179. *PtrSendAddr = (uint32)SendBuffer;
  1180. break;
  1181. }
  1182. default:
  1183. break;
  1184. }
  1185. }
  1186. void GetUtc8Time(UTC8TimeType *UTC8TimeTcp)
  1187. {
  1188. char *AtCmdAsk = (char *)("AT+CCLK?\r\n");
  1189. uint8 AtCmdLen = mstrlen(AtCmdAsk);
  1190. uint16 ReadLen = 0;
  1191. uint8 *retptr = NULL;
  1192. uint8 UartRecvPtr[128];
  1193. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)AtCmdAsk, AtCmdLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(100));
  1194. if (ReadLen > 0)
  1195. {
  1196. if ((uint8 *)strstr((char *)UartRecvPtr, (char *)("OK")))
  1197. {
  1198. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("+CCLK:"));
  1199. if (retptr)
  1200. {
  1201. UTC8TimeTcp->year = CharToHex(*(retptr + 8)) * 10 + CharToHex(*(retptr + 9));
  1202. UTC8TimeTcp->month = CharToHex(*(retptr + 11)) * 10 + CharToHex(*(retptr + 12));
  1203. UTC8TimeTcp->day = CharToHex(*(retptr + 14)) * 10 + CharToHex(*(retptr + 15));
  1204. UTC8TimeTcp->hour = CharToHex(*(retptr + 17)) * 10 + CharToHex(*(retptr + 18));
  1205. UTC8TimeTcp->minute = CharToHex(*(retptr + 20)) * 10 + CharToHex(*(retptr + 21));
  1206. UTC8TimeTcp->second = CharToHex(*(retptr + 23)) * 10 + CharToHex(*(retptr + 24));
  1207. }
  1208. }
  1209. }
  1210. }
  1211. static void GetCSQValue(uint8 *out)
  1212. {
  1213. char *AtCmdAsk = (char *)("AT+CSQ\r\n");
  1214. uint8 AtCmdLen = mstrlen(AtCmdAsk);
  1215. uint16 ReadLen = 0;
  1216. uint8 *retptr = NULL;
  1217. uint8 UartRecvPtr[30];
  1218. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)AtCmdAsk, AtCmdLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(100));
  1219. *out = 99;
  1220. if (ReadLen > 0)
  1221. {
  1222. if ((uint8 *)strstr((char *)UartRecvPtr, (char *)("OK")))
  1223. {
  1224. *out = 0;
  1225. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("+CSQ:"));
  1226. char CsqStr[5];
  1227. for (uint8 i = 0; i < 5; i++)
  1228. {
  1229. if (*(retptr + i + 6) == ',')
  1230. {
  1231. break;
  1232. }
  1233. memcpy((CsqStr + i), (retptr + i + 6), 1);
  1234. }
  1235. *out = atoi(CsqStr);
  1236. }
  1237. }
  1238. return;
  1239. }
  1240. static void TcpDataInfoRecvHandle(uint8 *DataRecv, uint16 DataRecvLen)
  1241. {
  1242. uint8 Tcp_Cmd;
  1243. uint8 *Ptr = NULL, *retptr = NULL;
  1244. uint8 TcpCmdAnswer[50] = {0};
  1245. uint16 TcpDataLen = 0;
  1246. uint16 NumCalTemp = 1;
  1247. retptr = (uint8 *)strstr((char *)DataRecv, (char *)("\r\n##"));
  1248. if (retptr == NULL || DataRecvLen < 5)
  1249. return;
  1250. for (uint8 i = 0; i < 5; i++)
  1251. {
  1252. if (*(retptr - i - 1) == 'D')
  1253. {
  1254. break;
  1255. }
  1256. TcpDataLen = TcpDataLen + CharToHex(*(retptr - i - 1)) * NumCalTemp;
  1257. NumCalTemp = NumCalTemp * 10;
  1258. }
  1259. if (TcpDataLen > 0)
  1260. {
  1261. Ptr = retptr + 2;
  1262. if ((*(Ptr + 0) == TCP_START_SYM1) && (*(Ptr + 1) == TCP_START_SYM2)) // 服务器起始信息
  1263. {
  1264. Tcp_Cmd = *(Ptr + 2); // 命令标志
  1265. if (*(Ptr + 3) == 0xFE)
  1266. {
  1267. TcpWorkState = 0x01; // 需要暂停发送,进行命令应答的标志
  1268. }
  1269. else
  1270. {
  1271. return;
  1272. }
  1273. switch (Tcp_Cmd)
  1274. {
  1275. case TCP_QUERY_SYM:
  1276. break;
  1277. case TCP_SETCMD_SYM:
  1278. break;
  1279. case TCP_CONCMD_SYM:
  1280. {
  1281. TcpCmdAnswer[0] = TCP_START_SYM1;
  1282. TcpCmdAnswer[1] = TCP_START_SYM1;
  1283. TcpCmdAnswer[2] = TCP_CONCMD_SYM;
  1284. if (*(Ptr + 30) == 0x83) // 远程升级指令
  1285. {
  1286. TcpCmdAnswer[3] = 0x01;
  1287. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1288. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1289. TcpCmdAnswer[22] = 0x00;
  1290. TcpCmdAnswer[23] = 0x06;
  1291. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1292. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1293. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1294. Fota_Process_Going = true;
  1295. vTaskDelay(pdMS_TO_TICKS(5000));
  1296. Fota_Ftp(Ptr + 32);
  1297. Fota_Process_Going = false;
  1298. }
  1299. else if (*(Ptr + 30) == 0x80) // 远程锁定命令
  1300. {
  1301. TcpCmdAnswer[3] = 0x01;
  1302. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1303. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1304. TcpCmdAnswer[22] = 0x00;
  1305. TcpCmdAnswer[23] = 0x06;
  1306. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1307. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1308. if (*(Ptr + 31) == 0x01) // 0x01代表锁定
  1309. {
  1310. // battSeparateEnable = 1;
  1311. // battSeparateCtlState = 1;
  1312. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1313. }
  1314. else if (*(Ptr + 31) == 0x02) // 0x02代表解锁
  1315. {
  1316. // battSeparateEnable = 1;
  1317. // battSeparateCtlState = 0;
  1318. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1319. }
  1320. }
  1321. else
  1322. {
  1323. TcpCmdAnswer[3] = 0x0f;
  1324. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1325. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1326. TcpCmdAnswer[22] = 0x00;
  1327. TcpCmdAnswer[23] = 0x06;
  1328. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1329. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1330. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1331. }
  1332. break;
  1333. }
  1334. case TCP_UDSCMD_SYM:
  1335. {
  1336. TcpCmdAnswer[0] = TCP_START_SYM1;
  1337. TcpCmdAnswer[1] = TCP_START_SYM1;
  1338. TcpCmdAnswer[2] = TCP_UDSCMD_SYM;
  1339. tcpUdsFunc(Ptr, TcpCmdAnswer);
  1340. // tcpipConnectionSend(SocketId, TcpCmdAnswer, 30);
  1341. break;
  1342. }
  1343. default:
  1344. {
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }
  1351. void tcpUdsFunc(uint8 *Ptr, uint8 *AnsPtr)
  1352. {
  1353. uint8 contrlType = 0;
  1354. uint16 udsDataLen = 0;
  1355. uint16 DIDBuffer = 0;
  1356. uint8 udsType_SID = 0;
  1357. uint8 DataBuffer[10];
  1358. contrlType = *(Ptr + 30);
  1359. udsDataLen = *(Ptr + 31);
  1360. udsType_SID = *(Ptr + 32);
  1361. DIDBuffer = *(Ptr + 33) << 8 | *(Ptr + 34);
  1362. memcpy(DataBuffer, Ptr + 35, udsDataLen);
  1363. switch (contrlType)
  1364. {
  1365. case 0x00:
  1366. {
  1367. break;
  1368. }
  1369. case 0x01:
  1370. {
  1371. switch (udsType_SID)
  1372. {
  1373. case 0x2E:
  1374. {
  1375. switch (DIDBuffer)
  1376. {
  1377. case 0x01:
  1378. {
  1379. // battSeparateEnable = 1;
  1380. if (DataBuffer[0] == 0x01)
  1381. {
  1382. // battSeparateCtlState = 1;
  1383. }
  1384. else if (DataBuffer[0] == 0x00)
  1385. {
  1386. // battSeparateCtlState = 0;
  1387. }
  1388. *(AnsPtr + 3) = 0x0f;
  1389. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1390. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1391. *(AnsPtr + 22) = 0x00;
  1392. *(AnsPtr + 23) = 0x06;
  1393. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1394. *(AnsPtr + 30) = contrlType;
  1395. *(AnsPtr + 31) = udsDataLen;
  1396. *(AnsPtr + 32) = udsType_SID;
  1397. *(AnsPtr + 33) = DIDBuffer >> 8;
  1398. *(AnsPtr + 34) = DIDBuffer;
  1399. *(AnsPtr + 36) = DataBuffer[0];
  1400. *(AnsPtr + 37) = bcc_chk(AnsPtr, 37);
  1401. return;
  1402. break;
  1403. }
  1404. default:
  1405. break;
  1406. }
  1407. break;
  1408. }
  1409. case 0x22:
  1410. {
  1411. break;
  1412. }
  1413. default:
  1414. break;
  1415. }
  1416. break;
  1417. }
  1418. default:
  1419. break;
  1420. }
  1421. *(AnsPtr + 3) = 0x0f;
  1422. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1423. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1424. *(AnsPtr + 22) = 0x00;
  1425. *(AnsPtr + 23) = 0x06;
  1426. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1427. *(AnsPtr + 30) = contrlType;
  1428. *(AnsPtr + 31) = udsDataLen;
  1429. *(AnsPtr + 32) = 0x7F;
  1430. *(AnsPtr + 33) = DIDBuffer >> 8;
  1431. *(AnsPtr + 34) = DIDBuffer;
  1432. *(AnsPtr + 36) = 0x31;
  1433. *(AnsPtr + 37) = bcc_chk(AnsPtr, 37);
  1434. }
  1435. void Fota_Ftp(uint8 *dataPtrIn)
  1436. {
  1437. static uint8 ftp_process = 0;
  1438. char *ATCmdSend = NULL;
  1439. sint8 recvRet = -1;
  1440. char ATSendDataBuffer[100] = {0};
  1441. uint8 UartRecvBuffer[50];
  1442. uint16 ReadLen = 0;
  1443. uint8 *retptr = NULL;
  1444. // char temp[70]=",qx,qx900,120.27.243.131,21,0,0.0.1.5,0.0.1.5,/Debug/V0.0.1.5.bin,";
  1445. /*URL信息解析*/
  1446. char *databuffer[20];
  1447. char *accountPtr;
  1448. char *passwordPtr;
  1449. char *ftpServerIpPtr;
  1450. char *ftpServerPort;
  1451. char *filePathPtr;
  1452. char *filenamePtr;
  1453. char keyFilename[20] = {0};
  1454. char *p = NULL;
  1455. p = strtok((char *)dataPtrIn, ",");
  1456. uint8 index = 0;
  1457. boolean ftp_EndFlg = false;
  1458. while (p && index < 20)
  1459. {
  1460. databuffer[index] = p;
  1461. p = strtok(NULL, ",");
  1462. index++;
  1463. }
  1464. accountPtr = databuffer[0];
  1465. passwordPtr = databuffer[1];
  1466. ftpServerIpPtr = databuffer[2];
  1467. ftpServerPort = databuffer[3];
  1468. filePathPtr = databuffer[7];
  1469. for (sint8 i = strlen(filePathPtr); i--; i < 0)
  1470. {
  1471. if (*(filePathPtr + i) == '/')
  1472. {
  1473. *(filePathPtr + i) = 0;
  1474. filenamePtr = filePathPtr + i + 1;
  1475. break;
  1476. }
  1477. }
  1478. memcpy(keyFilename, filenamePtr, mstrlen(filenamePtr) - 3);
  1479. memcpy(&keyFilename[mstrlen(filenamePtr) - 3], "zl", strlen("zl"));
  1480. while (!ftp_EndFlg)
  1481. {
  1482. vTaskDelay(pdMS_TO_TICKS(100));
  1483. switch (ftp_process)
  1484. {
  1485. case 0: // start
  1486. {
  1487. ATCmdSend = (char *)("AT+CFTPSSTART\r\n");
  1488. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1489. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSSTART: 0"), 2000);
  1490. if (recvRet == 0)
  1491. {
  1492. ftp_process++;
  1493. }
  1494. else
  1495. {
  1496. ftp_EndFlg = 1;
  1497. }
  1498. break;
  1499. }
  1500. case 1: // login
  1501. {
  1502. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1503. sprintf(ATSendDataBuffer, "AT+CFTPSLOGIN=\"%s\",%s,\"%s\",\"%s\",0\r\n", ftpServerIpPtr, ftpServerPort, accountPtr, passwordPtr);
  1504. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1505. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSLOGIN: 0"), 30000);
  1506. if (recvRet == 0)
  1507. {
  1508. ftp_process++;
  1509. }
  1510. else
  1511. {
  1512. ftp_EndFlg = 1;
  1513. }
  1514. break;
  1515. }
  1516. case 2: // transmit bin file from server to module
  1517. {
  1518. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1519. sprintf(ATSendDataBuffer, "AT+CFTPSGETFILE=\"%s/%s\"\r\n", filePathPtr, filenamePtr);
  1520. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1521. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSGETFILE: 0"), 10000);
  1522. if (recvRet == 0)
  1523. {
  1524. ftp_process++;
  1525. }
  1526. else
  1527. {
  1528. ftp_EndFlg = 1;
  1529. }
  1530. break;
  1531. }
  1532. case 3: // transmit zl file from server to module
  1533. {
  1534. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1535. sprintf(ATSendDataBuffer, "AT+CFTPSGETFILE=\"%s/%s\"\r\n", filePathPtr, keyFilename);
  1536. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1537. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSGETFILE: 0"), 10000);
  1538. if (recvRet == 0)
  1539. {
  1540. ftp_process++;
  1541. }
  1542. else
  1543. {
  1544. ftp_EndFlg = 1;
  1545. }
  1546. break;
  1547. }
  1548. case 4: // logout
  1549. {
  1550. ATCmdSend = (char *)("AT+CFTPSLOGOUT\r\n");
  1551. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1552. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSLOGOUT: 0"), 2000);
  1553. if (recvRet == 0)
  1554. {
  1555. ftp_process++;
  1556. }
  1557. else
  1558. {
  1559. ftp_process++;
  1560. }
  1561. break;
  1562. }
  1563. case 5: // stop
  1564. {
  1565. ATCmdSend = (char *)("AT+CFTPSSTOP\r\n");
  1566. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1567. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("CFTPSSTOP: 0"), 2000);
  1568. if (recvRet == 0)
  1569. {
  1570. ftp_process++;
  1571. }
  1572. else
  1573. {
  1574. ftp_process++;
  1575. }
  1576. break;
  1577. }
  1578. case 6: // get data from module
  1579. {
  1580. char findDataBuffer[25] = {0};
  1581. uint32 currentAddr = 0;
  1582. uint16 readLenAsk = 256;
  1583. uint16 FotaRecvDataLen_8 = 0;
  1584. uint32 fileLen = 65535;
  1585. uint32 FlashAddStart = 0;
  1586. uint32 appReceviedCRC;
  1587. uint16 getDataLenErrCount = 0;
  1588. uint8 UartData[512] = {0};
  1589. uint8 FlashData[512] = {0};
  1590. uint8 *fileDataPtr = NULL;
  1591. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1592. sprintf(ATSendDataBuffer, "AT+CFTRANTX=\"c:/%s\"\r\n", keyFilename);
  1593. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartRecvBuffer, &ReadLen, pdMS_TO_TICKS(5000));
  1594. if (ReadLen == (4 + 45))
  1595. {
  1596. retptr = (uint8 *)strstr((char *)UartRecvBuffer, (char *)("+CFTRANTX: DATA"));
  1597. if (retptr)
  1598. {
  1599. fileLen = (*(retptr + 21) << 24) | (*(retptr + 22) << 16) | (*(retptr + 23) << 8) | (*(retptr + 24) << 0);
  1600. appReceviedCRC = (*(retptr + 19) << 8) | (*(retptr + 20) << 0);
  1601. Hal_FlsGetAppVectorTableStartAddr(&FlashAddStart);
  1602. Hal_OTAFlashAppInfoInit();
  1603. Hal_FlsErase(0, FlashAddStart + fileLen + FLS_INTERNAL_WRITE_SIZE, 100);
  1604. Hal_SetAppInfo(fileLen, appReceviedCRC, CONTROLLER_SELF);
  1605. while (readLenAsk != 0)
  1606. {
  1607. memset(UartData, 0x00, 512);
  1608. memset(FlashData, 0x00, 512);
  1609. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1610. sprintf(ATSendDataBuffer, "AT+CFTRANTX=\"c:/%s\",%d,%d\r\n", filenamePtr, currentAddr, readLenAsk);
  1611. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartData, &ReadLen, pdMS_TO_TICKS(5000));
  1612. sprintf(findDataBuffer, "+CFTRANTX: DATA,%d", readLenAsk);
  1613. if (currentAddr % 0x100 == 0)
  1614. {
  1615. Dio_FlipChannel(LED_INDEX3);
  1616. }
  1617. if (ReadLen == (readLenAsk + strlen(findDataBuffer) + 26))
  1618. {
  1619. retptr = (uint8 *)strstr((char *)UartData, findDataBuffer);
  1620. if (retptr)
  1621. {
  1622. uint16 uartcrc = 0x00;
  1623. uint16 flashcrc = 0xff;
  1624. fileDataPtr = retptr + 2 + mstrlen(findDataBuffer);
  1625. FotaRecvDataLen_8 = ((readLenAsk + 7) / 8) * 8;
  1626. Hal_FlsWrite(FlashAddStart + currentAddr, fileDataPtr, FotaRecvDataLen_8, 100);
  1627. Hal_FlsRead(FlashAddStart + currentAddr, FlashData, FotaRecvDataLen_8, 100);
  1628. uartcrc = CRC16_Modbus(fileDataPtr, FotaRecvDataLen_8);
  1629. flashcrc = CRC16_Modbus(FlashData, FotaRecvDataLen_8);
  1630. if (flashcrc == uartcrc)
  1631. {
  1632. currentAddr = currentAddr + readLenAsk;
  1633. getDataLenErrCount = 0;
  1634. }
  1635. else
  1636. {
  1637. uint32 secflash = (FlashAddStart + currentAddr) & 0xFFFFF000;
  1638. if (secflash > 0)
  1639. {
  1640. currentAddr = secflash - FlashAddStart;
  1641. }
  1642. else
  1643. {
  1644. currentAddr = 0;
  1645. }
  1646. MemIf_JobResultType ret = Hal_FlsErase(secflash, 4096, 100);
  1647. getDataLenErrCount++;
  1648. }
  1649. readLenAsk = min(fileLen - currentAddr, readLenAsk);
  1650. }
  1651. else
  1652. {
  1653. getDataLenErrCount++;
  1654. }
  1655. }
  1656. else
  1657. {
  1658. getDataLenErrCount++;
  1659. }
  1660. if (getDataLenErrCount >= 100)
  1661. {
  1662. break;
  1663. }
  1664. }
  1665. Dio_WriteChannel(LED_INDEX3, STD_ON); // switch off the led
  1666. if (getDataLenErrCount < 50 && Hal_FlsCheckIsTransferSucceed() == TRUE)
  1667. {
  1668. ftp_process++;
  1669. }
  1670. else
  1671. {
  1672. ftp_process++;
  1673. }
  1674. }
  1675. }
  1676. else
  1677. {
  1678. ftp_process++;
  1679. }
  1680. break;
  1681. }
  1682. case 7: // delete the bin and zl files
  1683. {
  1684. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1685. sprintf(ATSendDataBuffer, "AT+FSDEL=*.*\r\n");
  1686. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartRecvBuffer, &ReadLen, pdMS_TO_TICKS(5000));
  1687. ftp_process++;
  1688. Fota_update_flag = TRUE;
  1689. break;
  1690. }
  1691. default:
  1692. ftp_EndFlg = 1;
  1693. break;
  1694. }
  1695. }
  1696. if (ftp_process < 6)
  1697. {
  1698. Fota_error_flag = 1;
  1699. }
  1700. }