AppTaskUart1.c 56 KB

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