AppTaskUart1.c 51 KB

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