AppTaskUart1.c 50 KB

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