AppTaskUart1.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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. uint16 GpsFeq = 10;
  752. if(DeviceSpeed==0)
  753. {
  754. GpsFeq = 3600;
  755. }
  756. else if(DeviceSpeed>0 && DeviceSpeed<=100)
  757. {
  758. GpsFeq = 60;
  759. }
  760. else if(DeviceSpeed>100 && DeviceSpeed<=200)
  761. {
  762. GpsFeq = 10;
  763. }
  764. else if(DeviceSpeed>200)
  765. {
  766. GpsFeq = 1;
  767. }
  768. GpsFeq = Saturation_u(GpsFeq,1,3600);
  769. if ((TcpSendTimeCounter) % (60 * 60) == 1)
  770. {
  771. DataIdx = VerMsg; // 版本信息发送
  772. }
  773. // else if (TcpSendTimeCounter % 10 == 2)
  774. // {
  775. // DataIdx = TruckBattMsg; // 电池信息发送
  776. // }
  777. // else if ((TcpSendTimeCounter) % 60 == 3)
  778. // {
  779. // DataIdx = TruckVehiMsg; // 车辆信息发送
  780. // }
  781. // else if ((TcpSendTimeCounter) % 60 == 4)
  782. // {
  783. // DataIdx = TruckAcclMsg; // 累计信息发送
  784. // }
  785. else if ((TcpSendTimeCounter) % GpsFeq == 0)
  786. {
  787. DataIdx = GpsMsg; // 定位信息发送
  788. }
  789. else if ((TcpSendTimeCounter) % (60 * 60 * 2) == 6)
  790. {
  791. DataIdx = 0x01; // 时间校准
  792. }
  793. else if ((TcpSendTimeCounter) % 10 == 7)
  794. {
  795. DataIdx = DebugMsg; // 调试信息发送,含FFT结果
  796. }
  797. else
  798. {
  799. *SendLen = 0;
  800. DataIdx = 0; // 不发送,返回
  801. return;
  802. }
  803. memcpy(TcpbattSN, AppConfigInfo.deviceSn, 17);
  804. GetUtc8Time(&UTC8TimeTcp); // 时间获取
  805. vTaskDelay(pdMS_TO_TICKS(100));
  806. switch (DataIdx)
  807. {
  808. case 0x01: // 时间校准
  809. {
  810. char *ATCmdSend = (char *)("AT+CNTP\r\n");
  811. uint8 ATCmdSendLen = mstrlen(ATCmdSend);
  812. uint8 ReadLen = 0;
  813. uint8 UartRecvPtr[20];
  814. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)ATCmdSend, ATCmdSendLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(500));
  815. *SendLen = 0; // 不发送,长度为0
  816. return;
  817. }
  818. case 0x82:
  819. {
  820. GPSInfo GpsRecvData;
  821. GPSMsgtoTcpType GpsToTcpInfo;
  822. *SendLen = sizeof(GpsToTcpInfo);
  823. SendBuffer = pvPortMalloc(*SendLen);
  824. uint16 tac = 0;
  825. uint32 cellId = 0;
  826. uint8 DataLen = (uint16)sizeof(GpsToTcpInfo.gpsInfo);
  827. GpsToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  828. GpsToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  829. GpsToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  830. GpsToTcpInfo.ansSymbol = TCP_ANS_SYM;
  831. memcpy(GpsToTcpInfo.SN, TcpbattSN, BATT_SN_LEN);
  832. GpsToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  833. GpsToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  834. GpsToTcpInfo.dataLength[1] = DataLen & 0xFF;
  835. GpsToTcpInfo.gpsInfo.sendTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  836. GpsToTcpInfo.gpsInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  837. GpsToTcpInfo.gpsInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  838. GpsToTcpInfo.gpsInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  839. GpsToTcpInfo.gpsInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  840. GpsToTcpInfo.gpsInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  841. GpsToTcpInfo.gpsInfo.msgMark = DataIdx;
  842. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  843. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  844. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  845. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  846. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  847. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  848. if (xQueueReceive(GpsDataQueueHandle, &GpsRecvData, 0) == pdPASS)
  849. {
  850. memcpy((uint8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, (uint8 *)&GpsRecvData, sizeof(GPSInfo));
  851. }
  852. else
  853. {
  854. memset((uint8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, 0x00, sizeof(GPSInfo));
  855. }
  856. GpsToTcpInfo.gpsInfo.Tac[0] = tac >> 8;
  857. GpsToTcpInfo.gpsInfo.Tac[1] = tac & 0xFF;
  858. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 24;
  859. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 16;
  860. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 8;
  861. GpsToTcpInfo.gpsInfo.CellID[0] = cellId;
  862. uint16 xyzDatacache[3] = {0};
  863. memcpy(xyzDatacache, xyzData, 3 * sizeof(uint16));
  864. for (uint8 i = 0; i < 3; i++)
  865. {
  866. if (xyzDatacache[i] > 0x8000) // 数据为负
  867. {
  868. xyzDatacache[i] = 20000U + (sint16)xyzDatacache[i];
  869. }
  870. else
  871. {
  872. xyzDatacache[i] = xyzDatacache[i] + 20000U;
  873. }
  874. }
  875. GpsToTcpInfo.gpsInfo.xData[0] = xyzDatacache[0] >> 8;
  876. GpsToTcpInfo.gpsInfo.xData[1] = xyzDatacache[0];
  877. GpsToTcpInfo.gpsInfo.yData[0] = xyzDatacache[1] >> 8;
  878. GpsToTcpInfo.gpsInfo.yData[1] = xyzDatacache[1];
  879. GpsToTcpInfo.gpsInfo.zData[0] = xyzDatacache[2] >> 8;
  880. GpsToTcpInfo.gpsInfo.zData[1] = xyzDatacache[2];
  881. GpsToTcpInfo.CRC = bcc_chk((uint8 *)&GpsToTcpInfo, sizeof(GPSMsgtoTcpType) - 1);
  882. memcpy(SendBuffer, &GpsToTcpInfo, sizeof(GpsToTcpInfo));
  883. *PtrSendAddr = (uint32)SendBuffer;
  884. break;
  885. }
  886. case 0x86:
  887. {
  888. VersionMsgtoTcpType VerMsgToTcpInfo;
  889. *SendLen = sizeof(VersionMsgtoTcpType);
  890. SendBuffer = pvPortMalloc(*SendLen);
  891. uint16 DataLen = 0;
  892. DataLen = (uint16)sizeof(VerMsgToTcpInfo.VerInfo);
  893. VerMsgToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  894. VerMsgToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  895. VerMsgToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  896. VerMsgToTcpInfo.ansSymbol = TCP_ANS_SYM;
  897. memcpy(VerMsgToTcpInfo.SN, TcpbattSN, BATT_SN_LEN);
  898. VerMsgToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  899. VerMsgToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  900. VerMsgToTcpInfo.dataLength[1] = DataLen & 0xFF;
  901. VerMsgToTcpInfo.VerInfo.sendTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  902. VerMsgToTcpInfo.VerInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  903. VerMsgToTcpInfo.VerInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  904. VerMsgToTcpInfo.VerInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  905. VerMsgToTcpInfo.VerInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  906. VerMsgToTcpInfo.VerInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  907. VerMsgToTcpInfo.VerInfo.msgMark = DataIdx;
  908. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  909. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  910. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  911. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  912. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  913. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  914. memcpy(VerMsgToTcpInfo.VerInfo.ICCID, IccidNum, 20);
  915. memcpy(VerMsgToTcpInfo.VerInfo.IMEI, ImeiNum, 15);
  916. VerMsgToTcpInfo.VerInfo.BMSHwVersion[0] = BMS_HardwareVersion >> 8;
  917. VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = BMS_HardwareVersion;
  918. VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = BMS_SoftwareVersion >> 24;
  919. VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = BMS_SoftwareVersion >> 16;
  920. VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = BMS_SoftwareVersion >> 8;
  921. VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = BMS_SoftwareVersion;
  922. VerMsgToTcpInfo.VerInfo.HwVersion[0] = (HwVersion >> 8) & 0xFF;
  923. VerMsgToTcpInfo.VerInfo.HwVersion[1] = (HwVersion)&0xFF;
  924. VerMsgToTcpInfo.VerInfo.BLVersion[0] = (BlSwVersion >> 24) & 0xFF;
  925. VerMsgToTcpInfo.VerInfo.BLVersion[1] = (BlSwVersion >> 16) & 0xFF;
  926. VerMsgToTcpInfo.VerInfo.BLVersion[2] = (BlSwVersion >> 8) & 0xFF;
  927. VerMsgToTcpInfo.VerInfo.BLVersion[3] = (BlSwVersion)&0xFF;
  928. VerMsgToTcpInfo.VerInfo.DRVVersion[0] = (DrvSwVersion >> 24) & 0xFF;
  929. VerMsgToTcpInfo.VerInfo.DRVVersion[1] = (DrvSwVersion >> 16) & 0xFF;
  930. VerMsgToTcpInfo.VerInfo.DRVVersion[2] = (DrvSwVersion >> 8) & 0xFF;
  931. VerMsgToTcpInfo.VerInfo.DRVVersion[3] = (DrvSwVersion)&0xFF;
  932. VerMsgToTcpInfo.VerInfo.APPVersion[0] = (AppSwVersion >> 24) & 0xFF;
  933. VerMsgToTcpInfo.VerInfo.APPVersion[1] = (AppSwVersion >> 16) & 0xFF;
  934. VerMsgToTcpInfo.VerInfo.APPVersion[2] = (AppSwVersion >> 8) & 0xFF;
  935. VerMsgToTcpInfo.VerInfo.APPVersion[3] = (AppSwVersion)&0xFF;
  936. VerMsgToTcpInfo.VerInfo.BmsType = BmsManuFacture;
  937. VerMsgToTcpInfo.VerInfo.BmsInfo = BmsInfo;
  938. VerMsgToTcpInfo.VerInfo.DataModuleType = DataModuleType;
  939. VerMsgToTcpInfo.CRC = bcc_chk((uint8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo) - 1);
  940. memcpy(SendBuffer, &VerMsgToTcpInfo, sizeof(VersionMsgtoTcpType));
  941. *PtrSendAddr = (uint32)SendBuffer;
  942. break;
  943. }
  944. case DebugMsg:
  945. {
  946. char rbuf[512]={0};
  947. DebugMsgtoTcpType DebugMsgInfo;
  948. UINT16 DataLen = 0;
  949. UINT16 BufferLen = 0;
  950. sprintf((char *)rbuf, "{%.2f,%.2f,%.2f,%.2f,%.2f;\
  951. %.2f,%.2f,%.2f,%.2f,%.2f,}\
  952. {%.2f,%.2f,%.2f,%.2f,%.2f;\
  953. %.2f,%.2f,%.2f,%.2f,%.2f,}\
  954. {%.2f,%.2f,%.2f,%.2f,%.2f;\
  955. %.2f,%.2f,%.2f,%.2f,%.2f,}",
  956. returnFreq[0][0], returnFreq[0][1], returnFreq[0][2], returnFreq[0][3], returnFreq[0][4],
  957. returnP[0][0], returnP[0][1], returnP[0][2], returnP[0][3], returnP[0][4],
  958. returnFreq[1][0], returnFreq[1][1], returnFreq[1][2], returnFreq[1][3], returnFreq[1][4],
  959. returnP[1][0], returnP[1][1], returnP[1][2], returnP[1][3], returnP[1][4],
  960. returnFreq[2][0], returnFreq[2][1], returnFreq[2][2], returnFreq[2][3], returnFreq[2][4],
  961. returnP[2][0], returnP[2][1], returnP[2][2], returnP[2][3], returnP[2][4]);
  962. BufferLen = strlen((const char *)rbuf);
  963. #ifdef SEGGER_RTT_PRINTF
  964. SEGGER_RTT_printf("[%d]BufferLen=%d\r\n",__LINE__,BufferLen);
  965. SEGGER_RTT_printf("[%d]rbuf=%s\r\n",__LINE__,rbuf);
  966. #endif
  967. *SendLen = BufferLen + sizeof(DebugMsgInfo);
  968. SendBuffer = pvPortMalloc(*SendLen);
  969. memcpy(SendBuffer + sizeof(DebugMsgInfo) - 1, rbuf, BufferLen);
  970. DataLen = sizeof(DebugMsgInfo.DebugInfo) + BufferLen;
  971. DebugMsgInfo.startSymbol[0] = TCP_START_SYM1;
  972. DebugMsgInfo.startSymbol[1] = TCP_START_SYM2;
  973. DebugMsgInfo.cmdSymbol = TCP_CMD_SYM;
  974. DebugMsgInfo.ansSymbol = TCP_ANS_SYM;
  975. memcpy(DebugMsgInfo.SN, TcpbattSN, BATT_SN_LEN);
  976. DebugMsgInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  977. DebugMsgInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  978. DebugMsgInfo.dataLength[1] = DataLen & 0xFF;
  979. DebugMsgInfo.DebugInfo.sendTimeUTC[0] = UTC8TimeTcp.year & 0xFF; // year
  980. DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  981. DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  982. DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  983. DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  984. DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  985. DebugMsgInfo.DebugInfo.msgMark = DebugMsg;
  986. DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen >> 8;
  987. DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen;
  988. memcpy(SendBuffer, (UINT8 *)&DebugMsgInfo, sizeof(DebugMsgInfo) - 1);
  989. DebugMsgInfo._CRC = bcc_chk(SendBuffer, BufferLen + sizeof(DebugMsgInfo) - 1);
  990. memcpy(SendBuffer + BufferLen + sizeof(DebugMsgInfo) - 1, &DebugMsgInfo._CRC, 1);
  991. *PtrSendAddr = (uint32)SendBuffer;
  992. break;
  993. }
  994. case 0x90:
  995. {
  996. *SendLen = 0x52;
  997. SendBuffer = pvPortMalloc(*SendLen);
  998. *(SendBuffer + 0) = TCP_START_SYM1;
  999. *(SendBuffer + 1) = TCP_START_SYM2;
  1000. *(SendBuffer + 2) = TCP_CMD_SYM;
  1001. *(SendBuffer + 3) = TCP_ANS_SYM;
  1002. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1003. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1004. uint16 DataLen = 0x39; // 57
  1005. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1006. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1007. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1008. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1009. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1010. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1011. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1012. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1013. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8 BATTMSG
  1014. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1015. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1016. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1017. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1018. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1019. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1020. memcpy(SendBuffer + 0x25, VIN, 17);
  1021. *(SendBuffer + 0x36) = (vehicleStatus & 0xFF); // uint8 整车状态
  1022. if (BMS_Mode == 1)
  1023. {
  1024. bmsHVOn = 1;
  1025. }
  1026. else if (BMS_Mode == 2)
  1027. {
  1028. bmsHVOn = 0;
  1029. }
  1030. else
  1031. {
  1032. bmsHVOn = 0xFF;
  1033. }
  1034. *(SendBuffer + 0x37) = (bmsHVOn & 0xFF); // uint8 bms上高压指令
  1035. *(SendBuffer + 0x38) = (currentGearPosition & 0xFF); // uint8 当前档位
  1036. *(SendBuffer + 0x39) = (parkingBreakStatus & 0xFF); // uint8 手刹信号
  1037. *(SendBuffer + 0x3A) = (breakingStatus & 0xFF); // uint8 制动开关
  1038. *(SendBuffer + 0x3B) = ((ODO >> 24) & 0xFF); // uint32 总里程
  1039. *(SendBuffer + 0x3C) = ((ODO >> 16) & 0xFF);
  1040. *(SendBuffer + 0x3D) = ((ODO >> 8) & 0xFF);
  1041. *(SendBuffer + 0x3E) = (ODO & 0xFF);
  1042. *(SendBuffer + 0x3F) = (dcdcWorkStatus & 0xFF); // uint8 DCDC状态
  1043. *(SendBuffer + 0x40) = (numOfChrgableSubsys & 0xFF); // uint8 可充电子系统数
  1044. *(SendBuffer + 0x41) = (chrgableSubsysCode & 0xFF); // uint8 可充电储能子系统号
  1045. *(SendBuffer + 0x42) = (BMS_MaxCellTempCSC & 0xFF); // uint8 最高温度子系统号 精度_1,偏移量_0,单位_
  1046. *(SendBuffer + 0x43) = (BMS_MaxCellTempNum & 0xFF); // uint8 最高温度探针单体代号 精度_1,偏移量_0,单位_
  1047. *(SendBuffer + 0x44) = (BMS_MinCellTempCSC & 0xFF); // uint8 最低温度子系统号 精度_1,偏移量_0,单位_
  1048. *(SendBuffer + 0x45) = (BMS_MinCellTempNum & 0xFF); // uint8 最低温度探针子系统代号 精度_1,偏移量_0,单位_
  1049. *(SendBuffer + 0x46) = (BMS_MaxCellVoltCSC & 0xFF); // uint8 最高电压电池子系统号 精度_1,偏移量_0,单位_
  1050. *(SendBuffer + 0x47) = ((BMS_MaxCellVoltNum >> 8) & 0xFF); // uint16 最高电压电池单体代号 精度_1,偏移量_0,单位_
  1051. *(SendBuffer + 0x48) = (BMS_MaxCellVoltNum & 0xFF);
  1052. *(SendBuffer + 0x49) = (BMS_MinCellVoltCSC & 0xFF); // uint8 最低电压电池子系统号 精度_1,偏移量_0,单位_
  1053. *(SendBuffer + 0x4A) = ((BMS_MinCellVoltNum >> 8) & 0xFF); // uint16 最低电压电池单体代号 精度_1,偏移量_0,单位_
  1054. *(SendBuffer + 0x4B) = (BMS_MinCellVoltNum & 0xFF);
  1055. *(SendBuffer + 0x4C) = (ebcStatus & 0xFF); // uint8 换电控制器状态
  1056. *(SendBuffer + 0x4D) = (uint8)((ebcAskHVOn & 0x01) | ((ebcAskHVOff & 0x01) << 1) | ((retainLockSignal & 0x01) << 3) | ((dischargeLockSignal & 0x01) << 5) | ((chargeLockSignal & 0x01) << 6));
  1057. *(SendBuffer + 0x4E) = (chargeFlag & 0xFF); // uint8 充电标志位
  1058. *(SendBuffer + 0x4F) = ((vcuDCVol >> 8) & 0xFF); // uint16 电机直流母线电压
  1059. *(SendBuffer + 0x50) = (vcuDCVol & 0xFF);
  1060. *(SendBuffer + 0x51) = bcc_chk(SendBuffer, 0x51);
  1061. *PtrSendAddr = (uint32)SendBuffer;
  1062. break;
  1063. }
  1064. case 0x91:
  1065. {
  1066. GetCSQValue(&CSQValue);
  1067. *SendLen = 0x54 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM);
  1068. SendBuffer = pvPortMalloc(*SendLen);
  1069. *(SendBuffer + 0) = TCP_START_SYM1;
  1070. *(SendBuffer + 1) = TCP_START_SYM2;
  1071. *(SendBuffer + 2) = TCP_CMD_SYM;
  1072. *(SendBuffer + 3) = TCP_ANS_SYM;
  1073. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1074. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1075. uint16 DataLen = 0x3B + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM);
  1076. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1077. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1078. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1079. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1080. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1081. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1082. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1083. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1084. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8 BATTMSG
  1085. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1086. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1087. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1088. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1089. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1090. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1091. *(SendBuffer + 0x25) = (CSQValue & 0xFF); // uint8 csq
  1092. *(SendBuffer + 0x26) = (BMS_Mode & 0xFF); // uint8 电池管理系统工作状态,0-初始化 1-自检完成允许上电 2-运行状态 3-高压切断 4-故障状态,精度_1,偏移量_0,单位_
  1093. *(SendBuffer + 0x27) = (BMS_CharGunSt & 0xFF); // uint8 直流充电枪连接状态,0-未连接 1-单枪连接 2-双枪连接 3-无效,精度_1,偏移量_0,单位_
  1094. *(SendBuffer + 0x28) = (BMS_FtLvl & 0xFF); // uint8 当前最高故障等级,0-正常 1-1级 轻微故障 2-2级 较严重故障 3-3级 最严重故障,精度_1,偏移量_0,单位_
  1095. *(SendBuffer + 0x29) = (BMS_FtCode & 0xFF); // uint8 故障码,见BMS_ErrCode,精度_1,偏移量_0,单位_
  1096. *(SendBuffer + 0x2A) = (BMS_ReqHVOff & 0xFF); // uint8 BMS下高压请求,0-无效 1-请求下高压 2-不请求下高压 3-无效,精度_1,偏移量_0,单位_
  1097. *(SendBuffer + 0x2B) = (BMS_CharSt & 0xFF); // uint8 充电状态,0-未充电 1-充电中 2-充电已完成 3-充电错误故障,精度_1,偏移量_0,单位_
  1098. *(SendBuffer + 0x2C) = (BMS_PackSOC & 0xFF); // uint8 电池包SOC 精度_0.4,偏移量_0,单位_
  1099. *(SendBuffer + 0x2D) = (BMS_PackSOH / 25 * 10) & 0xFF; // SOH 精度1%
  1100. *(SendBuffer + 0x2E) = (BMS_BattVolt >> 8) & 0xFF; // 电池包总电压
  1101. *(SendBuffer + 0x2F) = (BMS_BattVolt & 0xFF);
  1102. *(SendBuffer + 0x30) = ((BMS_PackCurr >> 8) & 0xFF); // uint16 电池包总电流,充电为负值,放电为正值 精度_0.1,偏移量_-1000,单位_A
  1103. *(SendBuffer + 0x31) = (BMS_PackCurr & 0xFF);
  1104. *(SendBuffer + 0x32) = ((BMS_SysInsRes >> 8) & 0xFF); // uint16 系统绝缘电阻 精度_1,偏移量_0,单位_KΩ
  1105. *(SendBuffer + 0x33) = (BMS_SysInsRes & 0xFF);
  1106. /*28 Byte 继电器状态*/
  1107. *(SendBuffer + 0x34) = (BMS_StPosRly & 0xFF); // uint8 主正继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1108. *(SendBuffer + 0x35) = (BMS_StPreCharRly & 0xFF); // uint8 预充继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1109. *(SendBuffer + 0x36) = (BMS_StNegRly & 0xFF); // uint8 主负继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1110. *(SendBuffer + 0x37) = 0; // 三合一
  1111. *(SendBuffer + 0x38) = 0; // PTC
  1112. *(SendBuffer + 0x39) = 0; // 空调
  1113. *(SendBuffer + 0x3A) = (BMS_StPosCharRly1 & 0xFF); // uint8 直流充正继电器1状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1114. *(SendBuffer + 0x3B) = (BMS_StNegCharRly1 & 0xFF); // uint8 直流充负继电器1状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1115. *(SendBuffer + 0x3C) = (BMS_StPosCharRly2 & 0xFF); // uint8 直流充正继电器2状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1116. *(SendBuffer + 0x3D) = (BMS_StNegCharRly2 & 0xFF); // uint8 直流充负继电器2状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1117. *(SendBuffer + 0x3E) = (BMS_StPosHeatRly & 0xFF); // uint8 加热正继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1118. *(SendBuffer + 0x3F) = (BMS_StNegHeatRly & 0xFF); // uint8 加热负继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1119. *(SendBuffer + 0x40) = 0;
  1120. /*28-Byte继电器状态结束*/
  1121. *(SendBuffer + 0x50) = ((BMS_CellTotal >> 8) & 0xFF); // uint16 PACK中单体电芯的总数目
  1122. *(SendBuffer + 0x51) = (BMS_CellTotal & 0xFF);
  1123. for (uint16 index = 0; index < min(BMS_CellTotal, BMS_CELL_MAX_NUM); index++)
  1124. {
  1125. *(SendBuffer + 0x52 + index * 2) = ((BMS_CellVolt[index] >> 8) & 0xFF);
  1126. *(SendBuffer + 0x52 + index * 2 + 1) = ((BMS_CellVolt[index]) & 0xFF);
  1127. }
  1128. *(SendBuffer + 0x52 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2) = (BMS_TempTotal & 0xFF); // uint8 PACK中电芯温度点(探针)的总数目
  1129. memcpy(SendBuffer + 0x53 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2, BMS_CellTemp, min(BMS_TempTotal, BMS_TEMP_MAX_NUM));
  1130. *(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));
  1131. *PtrSendAddr = (uint32)SendBuffer;
  1132. break;
  1133. }
  1134. case 0x92:
  1135. {
  1136. *SendLen = 0x4E;
  1137. SendBuffer = pvPortMalloc(*SendLen);
  1138. *(SendBuffer + 0) = TCP_START_SYM1;
  1139. *(SendBuffer + 1) = TCP_START_SYM2;
  1140. *(SendBuffer + 2) = TCP_CMD_SYM;
  1141. *(SendBuffer + 3) = TCP_ANS_SYM;
  1142. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1143. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1144. uint16 DataLen = 0x35;
  1145. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1146. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1147. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1148. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1149. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1150. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1151. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1152. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1153. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8
  1154. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1155. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1156. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1157. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1158. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1159. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1160. *(SendBuffer + 0x25) = ((BMS_TotalCharCapy >> 24) & 0xFF); // uint32 累积充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1161. *(SendBuffer + 0x26) = ((BMS_TotalCharCapy >> 16) & 0xFF);
  1162. *(SendBuffer + 0x27) = ((BMS_TotalCharCapy >> 8) & 0xFF);
  1163. *(SendBuffer + 0x28) = (BMS_TotalCharCapy & 0xFF);
  1164. *(SendBuffer + 0x29) = ((BMS_TotalDisCharCapy >> 24) & 0xFF); // uint32 累积放出Ah 数 精度_0.1,偏移量_0,单位_Ah
  1165. *(SendBuffer + 0x2A) = ((BMS_TotalDisCharCapy >> 16) & 0xFF);
  1166. *(SendBuffer + 0x2B) = ((BMS_TotalDisCharCapy >> 8) & 0xFF);
  1167. *(SendBuffer + 0x2C) = (BMS_TotalDisCharCapy & 0xFF);
  1168. *(SendBuffer + 0x2D) = ((BMS_TotalCharEngy >> 24) & 0xFF); // uint32 累计充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1169. *(SendBuffer + 0x2E) = ((BMS_TotalCharEngy >> 16) & 0xFF);
  1170. *(SendBuffer + 0x2F) = ((BMS_TotalCharEngy >> 8) & 0xFF);
  1171. *(SendBuffer + 0x30) = (BMS_TotalCharEngy & 0xFF);
  1172. *(SendBuffer + 0x31) = ((BMS_TotalDisCharEngy >> 24) & 0xFF); // uint32 累计放出kWh 数 精度_0.1,偏移量_0,单位_KWh
  1173. *(SendBuffer + 0x32) = ((BMS_TotalDisCharEngy >> 16) & 0xFF);
  1174. *(SendBuffer + 0x33) = ((BMS_TotalDisCharEngy >> 8) & 0xFF);
  1175. *(SendBuffer + 0x34) = (BMS_TotalDisCharEngy & 0xFF);
  1176. *(SendBuffer + 0x35) = ((BMS_TotalBackCharCapy >> 24) & 0xFF); // uint32 累计动能回馈充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1177. *(SendBuffer + 0x36) = ((BMS_TotalBackCharCapy >> 16) & 0xFF);
  1178. *(SendBuffer + 0x37) = ((BMS_TotalBackCharCapy >> 8) & 0xFF);
  1179. *(SendBuffer + 0x38) = (BMS_TotalBackCharCapy & 0xFF);
  1180. *(SendBuffer + 0x39) = ((BMS_TotalBackCharEngy >> 24) & 0xFF); // uint32 累计动能回馈充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1181. *(SendBuffer + 0x3A) = ((BMS_TotalBackCharEngy >> 16) & 0xFF);
  1182. *(SendBuffer + 0x3B) = ((BMS_TotalBackCharEngy >> 8) & 0xFF);
  1183. *(SendBuffer + 0x3C) = (BMS_TotalBackCharEngy & 0xFF);
  1184. *(SendBuffer + 0x3D) = ((BMS_TotalStaCharCapy >> 24) & 0xFF); // uint32 累计换电站充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1185. *(SendBuffer + 0x3E) = ((BMS_TotalStaCharCapy >> 16) & 0xFF);
  1186. *(SendBuffer + 0x3F) = ((BMS_TotalStaCharCapy >> 8) & 0xFF);
  1187. *(SendBuffer + 0x40) = (BMS_TotalStaCharCapy & 0xFF);
  1188. *(SendBuffer + 0x41) = ((BMS_TotalStaCharEngy >> 24) & 0xFF); // uint32 累计换电站充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1189. *(SendBuffer + 0x42) = ((BMS_TotalStaCharEngy >> 16) & 0xFF);
  1190. *(SendBuffer + 0x43) = ((BMS_TotalStaCharEngy >> 8) & 0xFF);
  1191. *(SendBuffer + 0x44) = (BMS_TotalStaCharEngy & 0xFF);
  1192. *(SendBuffer + 0x45) = ((BMS_TotalGunCharCapy >> 24) & 0xFF); // uint32 累计插枪充电充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1193. *(SendBuffer + 0x46) = ((BMS_TotalGunCharCapy >> 16) & 0xFF);
  1194. *(SendBuffer + 0x47) = ((BMS_TotalGunCharCapy >> 8) & 0xFF);
  1195. *(SendBuffer + 0x48) = (BMS_TotalGunCharCapy & 0xFF);
  1196. *(SendBuffer + 0x49) = ((BMS_TotalGunCharEngy >> 24) & 0xFF); // uint32 累计插枪充电充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1197. *(SendBuffer + 0x4A) = ((BMS_TotalGunCharEngy >> 16) & 0xFF);
  1198. *(SendBuffer + 0x4B) = ((BMS_TotalGunCharEngy >> 8) & 0xFF);
  1199. *(SendBuffer + 0x4C) = (BMS_TotalGunCharEngy & 0xFF);
  1200. *(SendBuffer + 0x4D) = bcc_chk(SendBuffer, 0x4D);
  1201. *PtrSendAddr = (uint32)SendBuffer;
  1202. break;
  1203. }
  1204. default:
  1205. break;
  1206. }
  1207. }
  1208. void GetUtc8Time(UTC8TimeType *UTC8TimeTcp)
  1209. {
  1210. char *AtCmdAsk = (char *)("AT+CCLK?\r\n");
  1211. uint8 AtCmdLen = mstrlen(AtCmdAsk);
  1212. uint16 ReadLen = 0;
  1213. uint8 *retptr = NULL;
  1214. uint8 UartRecvPtr[128];
  1215. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)AtCmdAsk, AtCmdLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(100));
  1216. if (ReadLen > 0)
  1217. {
  1218. if ((uint8 *)strstr((char *)UartRecvPtr, (char *)("OK")))
  1219. {
  1220. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("+CCLK:"));
  1221. if (retptr)
  1222. {
  1223. UTC8TimeTcp->year = CharToHex(*(retptr + 8)) * 10 + CharToHex(*(retptr + 9));
  1224. UTC8TimeTcp->month = CharToHex(*(retptr + 11)) * 10 + CharToHex(*(retptr + 12));
  1225. UTC8TimeTcp->day = CharToHex(*(retptr + 14)) * 10 + CharToHex(*(retptr + 15));
  1226. UTC8TimeTcp->hour = CharToHex(*(retptr + 17)) * 10 + CharToHex(*(retptr + 18));
  1227. UTC8TimeTcp->minute = CharToHex(*(retptr + 20)) * 10 + CharToHex(*(retptr + 21));
  1228. UTC8TimeTcp->second = CharToHex(*(retptr + 23)) * 10 + CharToHex(*(retptr + 24));
  1229. }
  1230. }
  1231. }
  1232. }
  1233. static void GetCSQValue(uint8 *out)
  1234. {
  1235. char *AtCmdAsk = (char *)("AT+CSQ\r\n");
  1236. uint8 AtCmdLen = mstrlen(AtCmdAsk);
  1237. uint16 ReadLen = 0;
  1238. uint8 *retptr = NULL;
  1239. uint8 UartRecvPtr[30];
  1240. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)AtCmdAsk, AtCmdLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(100));
  1241. *out = 99;
  1242. if (ReadLen > 0)
  1243. {
  1244. if ((uint8 *)strstr((char *)UartRecvPtr, (char *)("OK")))
  1245. {
  1246. *out = 0;
  1247. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("+CSQ:"));
  1248. char CsqStr[5];
  1249. for (uint8 i = 0; i < 5; i++)
  1250. {
  1251. if (*(retptr + i + 6) == ',')
  1252. {
  1253. break;
  1254. }
  1255. memcpy((CsqStr + i), (retptr + i + 6), 1);
  1256. }
  1257. *out = atoi(CsqStr);
  1258. }
  1259. }
  1260. return;
  1261. }
  1262. static void TcpDataInfoRecvHandle(uint8 *DataRecv, uint16 DataRecvLen)
  1263. {
  1264. uint8 Tcp_Cmd;
  1265. uint8 *Ptr = NULL, *retptr = NULL;
  1266. uint8 TcpCmdAnswer[50] = {0};
  1267. uint16 TcpDataLen = 0;
  1268. uint16 NumCalTemp = 1;
  1269. retptr = (uint8 *)strstr((char *)DataRecv, (char *)("\r\n##"));
  1270. if (retptr == NULL || DataRecvLen < 5)
  1271. return;
  1272. for (uint8 i = 0; i < 5; i++)
  1273. {
  1274. if (*(retptr - i - 1) == 'D')
  1275. {
  1276. break;
  1277. }
  1278. TcpDataLen = TcpDataLen + CharToHex(*(retptr - i - 1)) * NumCalTemp;
  1279. NumCalTemp = NumCalTemp * 10;
  1280. }
  1281. if (TcpDataLen > 0)
  1282. {
  1283. Ptr = retptr + 2;
  1284. if ((*(Ptr + 0) == TCP_START_SYM1) && (*(Ptr + 1) == TCP_START_SYM2)) // 服务器起始信息
  1285. {
  1286. Tcp_Cmd = *(Ptr + 2); // 命令标志
  1287. if (*(Ptr + 3) == 0xFE)
  1288. {
  1289. TcpWorkState = 0x01; // 需要暂停发送,进行命令应答的标志
  1290. }
  1291. else
  1292. {
  1293. return;
  1294. }
  1295. switch (Tcp_Cmd)
  1296. {
  1297. case TCP_QUERY_SYM:
  1298. break;
  1299. case TCP_SETCMD_SYM:
  1300. break;
  1301. case TCP_CONCMD_SYM:
  1302. {
  1303. TcpCmdAnswer[0] = TCP_START_SYM1;
  1304. TcpCmdAnswer[1] = TCP_START_SYM1;
  1305. TcpCmdAnswer[2] = TCP_CONCMD_SYM;
  1306. if (*(Ptr + 30) == 0x83) // 远程升级指令
  1307. {
  1308. TcpCmdAnswer[3] = 0x01;
  1309. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1310. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1311. TcpCmdAnswer[22] = 0x00;
  1312. TcpCmdAnswer[23] = 0x06;
  1313. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1314. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1315. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1316. Fota_Process_Going = true;
  1317. vTaskDelay(pdMS_TO_TICKS(5000));
  1318. Fota_Ftp(Ptr + 32);
  1319. Fota_Process_Going = false;
  1320. }
  1321. else if (*(Ptr + 30) == 0x80) // 远程锁定命令
  1322. {
  1323. TcpCmdAnswer[3] = 0x01;
  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. if (*(Ptr + 31) == 0x01) // 0x01代表锁定
  1331. {
  1332. // battSeparateEnable = 1;
  1333. // battSeparateCtlState = 1;
  1334. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1335. }
  1336. else if (*(Ptr + 31) == 0x02) // 0x02代表解锁
  1337. {
  1338. // battSeparateEnable = 1;
  1339. // battSeparateCtlState = 0;
  1340. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1341. }
  1342. }
  1343. else
  1344. {
  1345. TcpCmdAnswer[3] = 0x0f;
  1346. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1347. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1348. TcpCmdAnswer[22] = 0x00;
  1349. TcpCmdAnswer[23] = 0x06;
  1350. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1351. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1352. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1353. }
  1354. break;
  1355. }
  1356. case TCP_UDSCMD_SYM:
  1357. {
  1358. TcpCmdAnswer[0] = TCP_START_SYM1;
  1359. TcpCmdAnswer[1] = TCP_START_SYM1;
  1360. TcpCmdAnswer[2] = TCP_UDSCMD_SYM;
  1361. tcpUdsFunc(Ptr, TcpCmdAnswer);
  1362. // tcpipConnectionSend(SocketId, TcpCmdAnswer, 30);
  1363. break;
  1364. }
  1365. default:
  1366. {
  1367. break;
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. void tcpUdsFunc(uint8 *Ptr, uint8 *AnsPtr)
  1374. {
  1375. uint8 contrlType = 0;
  1376. uint16 udsDataLen = 0;
  1377. uint16 DIDBuffer = 0;
  1378. uint8 udsType_SID = 0;
  1379. uint8 DataBuffer[10];
  1380. contrlType = *(Ptr + 30);
  1381. udsDataLen = *(Ptr + 31);
  1382. udsType_SID = *(Ptr + 32);
  1383. DIDBuffer = *(Ptr + 33) << 8 | *(Ptr + 34);
  1384. memcpy(DataBuffer, Ptr + 35, udsDataLen);
  1385. switch (contrlType)
  1386. {
  1387. case 0x00:
  1388. {
  1389. break;
  1390. }
  1391. case 0x01:
  1392. {
  1393. switch (udsType_SID)
  1394. {
  1395. case 0x2E:
  1396. {
  1397. switch (DIDBuffer)
  1398. {
  1399. case 0x01:
  1400. {
  1401. // battSeparateEnable = 1;
  1402. if (DataBuffer[0] == 0x01)
  1403. {
  1404. // battSeparateCtlState = 1;
  1405. }
  1406. else if (DataBuffer[0] == 0x00)
  1407. {
  1408. // battSeparateCtlState = 0;
  1409. }
  1410. *(AnsPtr + 3) = 0x0f;
  1411. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1412. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1413. *(AnsPtr + 22) = 0x00;
  1414. *(AnsPtr + 23) = 0x06;
  1415. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1416. *(AnsPtr + 30) = contrlType;
  1417. *(AnsPtr + 31) = udsDataLen;
  1418. *(AnsPtr + 32) = udsType_SID;
  1419. *(AnsPtr + 33) = DIDBuffer >> 8;
  1420. *(AnsPtr + 34) = DIDBuffer;
  1421. *(AnsPtr + 36) = DataBuffer[0];
  1422. *(AnsPtr + 37) = bcc_chk(AnsPtr, 37);
  1423. return;
  1424. break;
  1425. }
  1426. default:
  1427. break;
  1428. }
  1429. break;
  1430. }
  1431. case 0x22:
  1432. {
  1433. break;
  1434. }
  1435. default:
  1436. break;
  1437. }
  1438. break;
  1439. }
  1440. default:
  1441. break;
  1442. }
  1443. *(AnsPtr + 3) = 0x0f;
  1444. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1445. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1446. *(AnsPtr + 22) = 0x00;
  1447. *(AnsPtr + 23) = 0x06;
  1448. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1449. *(AnsPtr + 30) = contrlType;
  1450. *(AnsPtr + 31) = udsDataLen;
  1451. *(AnsPtr + 32) = 0x7F;
  1452. *(AnsPtr + 33) = DIDBuffer >> 8;
  1453. *(AnsPtr + 34) = DIDBuffer;
  1454. *(AnsPtr + 36) = 0x31;
  1455. *(AnsPtr + 37) = bcc_chk(AnsPtr, 37);
  1456. }
  1457. void Fota_Ftp(uint8 *dataPtrIn)
  1458. {
  1459. static uint8 ftp_process = 0;
  1460. char *ATCmdSend = NULL;
  1461. sint8 recvRet = -1;
  1462. char ATSendDataBuffer[100] = {0};
  1463. uint8 UartRecvBuffer[50];
  1464. uint16 ReadLen = 0;
  1465. uint8 *retptr = NULL;
  1466. // 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,";
  1467. /*URL信息解析*/
  1468. char *databuffer[20];
  1469. char *accountPtr;
  1470. char *passwordPtr;
  1471. char *ftpServerIpPtr;
  1472. char *ftpServerPort;
  1473. char *filePathPtr;
  1474. char *filenamePtr;
  1475. char keyFilename[20] = {0};
  1476. char *p = NULL;
  1477. p = strtok((char *)dataPtrIn, ",");
  1478. uint8 index = 0;
  1479. boolean ftp_EndFlg = false;
  1480. while (p && index < 20)
  1481. {
  1482. databuffer[index] = p;
  1483. p = strtok(NULL, ",");
  1484. index++;
  1485. }
  1486. accountPtr = databuffer[0];
  1487. passwordPtr = databuffer[1];
  1488. ftpServerIpPtr = databuffer[2];
  1489. ftpServerPort = databuffer[3];
  1490. filePathPtr = databuffer[7];
  1491. for (sint8 i = strlen(filePathPtr); i--; i < 0)
  1492. {
  1493. if (*(filePathPtr + i) == '/')
  1494. {
  1495. *(filePathPtr + i) = 0;
  1496. filenamePtr = filePathPtr + i + 1;
  1497. break;
  1498. }
  1499. }
  1500. memcpy(keyFilename, filenamePtr, mstrlen(filenamePtr) - 3);
  1501. memcpy(&keyFilename[mstrlen(filenamePtr) - 3], "zl", strlen("zl"));
  1502. while (!ftp_EndFlg)
  1503. {
  1504. vTaskDelay(pdMS_TO_TICKS(100));
  1505. switch (ftp_process)
  1506. {
  1507. case 0: // start
  1508. {
  1509. ATCmdSend = (char *)("AT+CFTPSSTART\r\n");
  1510. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1511. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSSTART: 0"), 2000);
  1512. if (recvRet == 0)
  1513. {
  1514. ftp_process++;
  1515. }
  1516. else
  1517. {
  1518. ftp_EndFlg = 1;
  1519. }
  1520. break;
  1521. }
  1522. case 1: // login
  1523. {
  1524. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1525. sprintf(ATSendDataBuffer, "AT+CFTPSLOGIN=\"%s\",%s,\"%s\",\"%s\",0\r\n", ftpServerIpPtr, ftpServerPort, accountPtr, passwordPtr);
  1526. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1527. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSLOGIN: 0"), 30000);
  1528. if (recvRet == 0)
  1529. {
  1530. ftp_process++;
  1531. }
  1532. else
  1533. {
  1534. ftp_EndFlg = 1;
  1535. }
  1536. break;
  1537. }
  1538. case 2: // transmit bin file from server to module
  1539. {
  1540. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1541. sprintf(ATSendDataBuffer, "AT+CFTPSGETFILE=\"%s/%s\"\r\n", filePathPtr, filenamePtr);
  1542. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1543. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSGETFILE: 0"), 10000);
  1544. if (recvRet == 0)
  1545. {
  1546. ftp_process++;
  1547. }
  1548. else
  1549. {
  1550. ftp_EndFlg = 1;
  1551. }
  1552. break;
  1553. }
  1554. case 3: // transmit zl file from server to module
  1555. {
  1556. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1557. sprintf(ATSendDataBuffer, "AT+CFTPSGETFILE=\"%s/%s\"\r\n", filePathPtr, keyFilename);
  1558. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1559. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSGETFILE: 0"), 10000);
  1560. if (recvRet == 0)
  1561. {
  1562. ftp_process++;
  1563. }
  1564. else
  1565. {
  1566. ftp_EndFlg = 1;
  1567. }
  1568. break;
  1569. }
  1570. case 4: // logout
  1571. {
  1572. ATCmdSend = (char *)("AT+CFTPSLOGOUT\r\n");
  1573. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1574. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSLOGOUT: 0"), 2000);
  1575. if (recvRet == 0)
  1576. {
  1577. ftp_process++;
  1578. }
  1579. else
  1580. {
  1581. ftp_process++;
  1582. }
  1583. break;
  1584. }
  1585. case 5: // stop
  1586. {
  1587. ATCmdSend = (char *)("AT+CFTPSSTOP\r\n");
  1588. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1589. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("CFTPSSTOP: 0"), 2000);
  1590. if (recvRet == 0)
  1591. {
  1592. ftp_process++;
  1593. }
  1594. else
  1595. {
  1596. ftp_process++;
  1597. }
  1598. break;
  1599. }
  1600. case 6: // get data from module
  1601. {
  1602. char findDataBuffer[25] = {0};
  1603. uint32 currentAddr = 0;
  1604. uint16 readLenAsk = 256;
  1605. uint16 FotaRecvDataLen_8 = 0;
  1606. uint32 fileLen = 65535;
  1607. uint32 FlashAddStart = 0;
  1608. uint32 appReceviedCRC;
  1609. uint16 getDataLenErrCount = 0;
  1610. uint8 UartData[512] = {0};
  1611. uint8 FlashData[512] = {0};
  1612. uint8 *fileDataPtr = NULL;
  1613. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1614. sprintf(ATSendDataBuffer, "AT+CFTRANTX=\"c:/%s\"\r\n", keyFilename);
  1615. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartRecvBuffer, &ReadLen, pdMS_TO_TICKS(5000));
  1616. if (ReadLen == (4 + 45))
  1617. {
  1618. retptr = (uint8 *)strstr((char *)UartRecvBuffer, (char *)("+CFTRANTX: DATA"));
  1619. if (retptr)
  1620. {
  1621. fileLen = (*(retptr + 21) << 24) | (*(retptr + 22) << 16) | (*(retptr + 23) << 8) | (*(retptr + 24) << 0);
  1622. appReceviedCRC = (*(retptr + 19) << 8) | (*(retptr + 20) << 0);
  1623. Hal_FlsGetAppVectorTableStartAddr(&FlashAddStart);
  1624. Hal_OTAFlashAppInfoInit();
  1625. Hal_FlsErase(0, FlashAddStart + fileLen + FLS_INTERNAL_WRITE_SIZE, 100);
  1626. Hal_SetAppInfo(fileLen, appReceviedCRC, CONTROLLER_SELF);
  1627. while (readLenAsk != 0)
  1628. {
  1629. memset(UartData, 0x00, 512);
  1630. memset(FlashData, 0x00, 512);
  1631. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1632. sprintf(ATSendDataBuffer, "AT+CFTRANTX=\"c:/%s\",%d,%d\r\n", filenamePtr, currentAddr, readLenAsk);
  1633. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartData, &ReadLen, pdMS_TO_TICKS(5000));
  1634. sprintf(findDataBuffer, "+CFTRANTX: DATA,%d", readLenAsk);
  1635. if (currentAddr % 0x100 == 0)
  1636. {
  1637. Dio_FlipChannel(DioConf_DioChannel_PTE9_GPIO_OUT_MCU_LED5);
  1638. }
  1639. if (ReadLen == (readLenAsk + strlen(findDataBuffer) + 26))
  1640. {
  1641. retptr = (uint8 *)strstr((char *)UartData, findDataBuffer);
  1642. if (retptr)
  1643. {
  1644. uint16 uartcrc = 0x00;
  1645. uint16 flashcrc = 0xff;
  1646. fileDataPtr = retptr + 2 + mstrlen(findDataBuffer);
  1647. FotaRecvDataLen_8 = ((readLenAsk + 7) / 8) * 8;
  1648. Hal_FlsWrite(FlashAddStart + currentAddr, fileDataPtr, FotaRecvDataLen_8, 100);
  1649. Hal_FlsRead(FlashAddStart + currentAddr, FlashData, FotaRecvDataLen_8, 100);
  1650. uartcrc = CRC16_Modbus(fileDataPtr, FotaRecvDataLen_8);
  1651. flashcrc = CRC16_Modbus(FlashData, FotaRecvDataLen_8);
  1652. if (flashcrc == uartcrc)
  1653. {
  1654. currentAddr = currentAddr + readLenAsk;
  1655. getDataLenErrCount = 0;
  1656. }
  1657. else
  1658. {
  1659. uint32 secflash = (FlashAddStart + currentAddr) & 0xFFFFF000;
  1660. if (secflash > 0)
  1661. {
  1662. currentAddr = secflash - FlashAddStart;
  1663. }
  1664. else
  1665. {
  1666. currentAddr = 0;
  1667. }
  1668. MemIf_JobResultType ret = Hal_FlsErase(secflash, 4096, 100);
  1669. getDataLenErrCount++;
  1670. }
  1671. readLenAsk = min(fileLen - currentAddr, readLenAsk);
  1672. }
  1673. else
  1674. {
  1675. getDataLenErrCount++;
  1676. }
  1677. }
  1678. else
  1679. {
  1680. getDataLenErrCount++;
  1681. }
  1682. if (getDataLenErrCount >= 100)
  1683. {
  1684. break;
  1685. }
  1686. }
  1687. Dio_WriteChannel(DioConf_DioChannel_PTE9_GPIO_OUT_MCU_LED5, STD_ON); // switch off the led
  1688. if (getDataLenErrCount < 50 && Hal_FlsCheckIsTransferSucceed() == TRUE)
  1689. {
  1690. ftp_process++;
  1691. }
  1692. else
  1693. {
  1694. ftp_process++;
  1695. }
  1696. }
  1697. }
  1698. else
  1699. {
  1700. ftp_process++;
  1701. }
  1702. break;
  1703. }
  1704. case 7: // delete the bin and zl files
  1705. {
  1706. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1707. sprintf(ATSendDataBuffer, "AT+FSDEL=*.*\r\n");
  1708. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartRecvBuffer, &ReadLen, pdMS_TO_TICKS(5000));
  1709. ftp_process++;
  1710. Fota_update_flag = TRUE;
  1711. break;
  1712. }
  1713. default:
  1714. ftp_EndFlg = 1;
  1715. break;
  1716. }
  1717. }
  1718. if (ftp_process < 6)
  1719. {
  1720. Fota_error_flag = 1;
  1721. }
  1722. }