AppTaskUart1.c 57 KB

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