AppTaskUart1.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. const ATCmdFunc Atcmdfunc[] = {
  18. {AT_CMD_TEST, "AT\r\n", at_callbackFunc},
  19. {AT_SIMREADY, "AT+CPIN?\r\n", at_callbackFunc},
  20. {AT_GETICCID, "AT+CICCID\r\n", at_callbackFunc},
  21. {AT_CGREG, "AT+CGREG?\r\n", at_callbackFunc},
  22. {AT_CSQ, "AT+CSQ\r\n", at_callbackFunc},
  23. {AT_NETOPEN, "AT+NETOPEN\r\n", at_callbackFunc},
  24. {AT_CGIP, "AT+CDNSGIP=", at_callbackFunc},
  25. //{AT_CONNECT, "AT+CIPOPEN=0,\"TCP\",\"120.26.68.165\",14319\r\n", at_callbackFunc},
  26. {AT_CONNECT, "AT+CIPOPEN=", at_callbackFunc},
  27. {AT_CONNECTCHK, "AT+CIPOPEN?\r\n", at_callbackFunc},
  28. {AT_SEND, "AT+CIPSEND=", at_callbackFunc},
  29. {AT_DISCON, "AT+CIPCLOSE=0\r\n", at_callbackFunc},
  30. {AT_NETCLOSE, "AT+NETCLOSE\r\n", at_callbackFunc}};
  31. static process_Tcp gProcess_Tcp_Task = PROCESS_TCP_IDLE;
  32. #define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a)
  33. void InitFunc(void);
  34. sint8 TcpConnectFunc(sint8 *ConnectId);
  35. sint8 TcpDataSendFunc(sint8 ConnectId);
  36. sint8 TcpRegisterChkFunc(void);
  37. void TcpDataEncode(uint32 *PtrSendAddr, uint16 *SendLen);
  38. void GetUtc8Time(UTC8TimeType *UTC8TimeTcp);
  39. static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, sint8 *retFunc);
  40. static sint8 tcpipConnectionSend(uint8 TcpConnectId,uint8 * SendDataPtr,uint16 SendDataLen);
  41. static void TcpDataInfoRecvHandle(uint8 *DataRecv,uint16 DataRecvLen);
  42. void Uart_4G_Task(void *pvParameters)
  43. {
  44. (void)pvParameters;
  45. // 4G开机
  46. Dio_WriteChannel(DioConf_DioChannel_PTA6_GPIO_OUT_MCU_4G_POW_EN, STD_ON);
  47. Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_ON);
  48. Dio_LevelType _4G_Status = 0; // 0-关机,1-开机
  49. _4G_Status = Dio_ReadChannel(DioConf_DioChannel_PTB1_GPIO_IN_MCU_4G_STATUS);
  50. // LED测试
  51. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  52. vTaskDelay(pdMS_TO_TICKS(5000));
  53. uint8 pReadLen = 0;
  54. InitFunc(); // 4G模块初始化,注:AT同步不通过,没有进行次数判定及跳转
  55. uint32 SendTimerCounter = 0;
  56. for (;;)
  57. {
  58. switch (gProcess_Tcp_Task)
  59. {
  60. case PROCESS_TCP_IDLE: //空闲状态
  61. {
  62. vTaskDelay(pdMS_TO_TICKS(10));
  63. if (SocketId < 0)
  64. {
  65. PROC_TCP_STATE_SWITCH(PROCESS_TCP_REGCHK);
  66. }
  67. else if ((TimerCounter -SendTimerCounter)>1000)
  68. {
  69. SendTimerCounter = TimerCounter;
  70. PROC_TCP_STATE_SWITCH(PROCESS_TCP_SEND);
  71. //Dio_FlipChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3);
  72. }
  73. else if (TimerCounter % 100 == 0)
  74. {
  75. PROC_TCP_STATE_SWITCH(PROCESS_TCP_RECV);
  76. }
  77. break;
  78. }
  79. case PROCESS_TCP_ATSYS:
  80. {
  81. sint8 ATRet = -1;
  82. AtcmdTransmit(AT_CMD_TEST, NULL, 0, &ATRet);
  83. if (ATRet == 0)
  84. {
  85. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  86. }
  87. else
  88. {
  89. PROC_TCP_STATE_SWITCH(PROCESS_TCP_ERROR);
  90. }
  91. break;
  92. }
  93. case PROCESS_TCP_REGCHK: //驻网检查,包括SIM,CPIN检查,ICCID获取
  94. {
  95. sint8 RegChkRet = 0;
  96. RegChkRet = TcpRegisterChkFunc();
  97. if (RegChkRet > 0) //检查通过,SIM卡已就绪,已进行驻网
  98. {
  99. PROC_TCP_STATE_SWITCH(PROCESS_TCP_CONNECT);
  100. }
  101. else
  102. {
  103. PROC_TCP_STATE_SWITCH(PROCESS_TCP_ERROR);
  104. }
  105. break;
  106. }
  107. case PROCESS_TCP_CONNECT: //网络连接,包括域名转换
  108. {
  109. if (SocketId < 0)
  110. {
  111. sint8 ConnectRet = 0;
  112. ConnectRet = TcpConnectFunc(&SocketId);
  113. if (ConnectRet > 0)
  114. {
  115. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  116. }
  117. else
  118. {
  119. PROC_TCP_STATE_SWITCH(PROCESS_TCP_ERROR);
  120. }
  121. }
  122. break;
  123. }
  124. case PROCESS_TCP_SEND: //网络数据发送
  125. {
  126. sint8 SendRet = -1;
  127. SendRet = TcpDataSendFunc(SocketId);
  128. if (SendRet == 0)
  129. {
  130. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  131. }
  132. else
  133. {
  134. SocketId = -1;
  135. PROC_TCP_STATE_SWITCH(PROCESS_TCP_ERROR);
  136. }
  137. break;
  138. }
  139. case PROCESS_TCP_RECV: //网络数据接收,100ms空闲状态下即可接收
  140. {
  141. memset(RX_Buffer[UART_LPUART1],0x00,sizeof(RX_Buffer[UART_LPUART1]));
  142. UART_Receive_Data(UART_LPUART1,RX_Buffer[UART_LPUART1],&pReadLen,100);
  143. if(pReadLen>0&&SocketId>=0)
  144. {
  145. TcpDataInfoRecvHandle(RX_Buffer[UART_LPUART1],pReadLen);
  146. }
  147. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  148. break;
  149. }
  150. case PROCESS_TCP_HEART: //心跳包发送
  151. {
  152. break;
  153. }
  154. case PROCESS_TCP_SLEEP: //网络休眠状态
  155. {
  156. break;
  157. }
  158. case PROCESS_TCP_ERROR: //错误状态
  159. {
  160. // Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_OFF);
  161. // vTaskDelay(pdMS_TO_TICKS(100));
  162. PROC_TCP_STATE_SWITCH(PROCESS_TCP_IDLE);
  163. // Dio_WriteChannel(DioConf_DioChannel_PTA7_GPIO_OUT_MCU_4G_PWRKEY, STD_ON);
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. sint8 TcpDataSendFunc(sint8 ConnectId)
  170. {
  171. sint8 outValue = -1;
  172. uint32 pSendDataAddr = 0;
  173. uint16 DataSendLen = 0;
  174. memcpy(TcpbattSN, defaultSn, 17);
  175. TcpDataEncode(&pSendDataAddr, &DataSendLen); //数据组包,malloc申请在里面,pSendData指向申请的地址
  176. if (DataSendLen == 0)
  177. {
  178. return 0; //暂时无数据可以发
  179. }
  180. outValue = tcpipConnectionSend(ConnectId,(uint8 *)pSendDataAddr,DataSendLen);//发送函数
  181. if (pSendDataAddr != 0)
  182. {
  183. free((uint8 *)(pSendDataAddr));
  184. }
  185. pSendDataAddr = 0;
  186. return outValue;
  187. }
  188. sint8 TcpConnectFunc(sint8 *ConnectId)
  189. {
  190. uint8 ConnectStep = 0;
  191. sint8 ChkState = 0;
  192. sint8 ATRet = -1;
  193. while (1)
  194. {
  195. switch (ConnectStep)
  196. {
  197. case 0: // AT指令同步
  198. {
  199. char *ATCmdSend = (char *)("ATE0\r\n");
  200. uint8 ATCmdSendLen = mstrlen(ATCmdSend);
  201. uint8 ReadLen = 0;
  202. memset(RX_Buffer[UART_LPUART1], 0x00, sizeof(RX_Buffer[UART_LPUART1]));
  203. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATCmdSend, ATCmdSendLen, RX_Buffer[UART_LPUART1], &ReadLen, 100);
  204. uint8 *retptr = NULL;
  205. if (ReadLen > 0)
  206. {
  207. retptr = (uint8 *)strstr((char *)RX_Buffer[UART_LPUART1], (char *)("OK"));
  208. if (retptr)
  209. {
  210. ConnectStep++;
  211. }
  212. else
  213. {
  214. ChkState = -ConnectStep;
  215. return ChkState;
  216. }
  217. }
  218. else
  219. {
  220. ChkState = -ConnectStep;
  221. return ChkState;
  222. }
  223. break;
  224. }
  225. case 1: // Netopen
  226. {
  227. AtcmdTransmit(AT_NETOPEN, NULL, 0, &ATRet);
  228. if (ATRet == 0)
  229. {
  230. ConnectStep++;
  231. }
  232. else
  233. {
  234. ChkState = -ConnectStep;
  235. return ChkState;
  236. }
  237. break;
  238. }
  239. case 2: //连接检查
  240. {
  241. AtcmdTransmit(AT_CONNECTCHK, NULL, 0, &ATRet); // ATret返回的值是连接id,如果未连接返回-1
  242. if (ATRet >= 0)
  243. {
  244. *ConnectId = ATRet;
  245. return 1;
  246. }
  247. else
  248. {
  249. ConnectStep++;
  250. *ConnectId = 0;
  251. }
  252. break;
  253. }
  254. case 3: //域名转换
  255. {
  256. char AtCmdSend[30] = {0};
  257. uint8 AtCmdSendLen = 0;
  258. AtCmdSendLen = mstrlen(WebSiteName);
  259. memcpy(AtCmdSend, WebSiteName, AtCmdSendLen);
  260. memcpy(AtCmdSend + AtCmdSendLen, (char *)CRLF, sizeof(CRLF));
  261. AtCmdSendLen = AtCmdSendLen + 2;
  262. AtcmdTransmit(AT_CGIP, AtCmdSend, AtCmdSendLen, &ATRet);
  263. if (ATRet == 0)
  264. {
  265. ConnectStep++;
  266. }
  267. else
  268. {
  269. ChkState = -ConnectStep;
  270. return ChkState;
  271. }
  272. break;
  273. }
  274. case 4: //创建连接
  275. {
  276. char AtCmdSend[20] = {0};
  277. uint8 AtCmdSendTotalLen = 0;
  278. sprintf(AtCmdSend, "%d,\"TCP\",%s,%d\r\n", *ConnectId, WebSiteIp, WebSitePort); //此处需要优化
  279. AtCmdSendTotalLen = mstrlen(AtCmdSend);
  280. AtcmdTransmit(AT_CONNECT, AtCmdSend, AtCmdSendTotalLen, &ATRet);
  281. if (ATRet == 0)
  282. {
  283. ConnectStep++;
  284. }
  285. else
  286. {
  287. ChkState = -ConnectStep;
  288. return ChkState;
  289. }
  290. break;
  291. }
  292. default:
  293. ChkState = ConnectStep;
  294. return ChkState;
  295. }
  296. }
  297. }
  298. sint8 TcpRegisterChkFunc(void)
  299. {
  300. uint8 RegChkStep = 0;
  301. sint8 ChkState = 0; //默认为0
  302. sint8 ATRet = -1;
  303. while (1)
  304. {
  305. switch (RegChkStep)
  306. {
  307. case 0: // AT指令同步
  308. {
  309. AtcmdTransmit(AT_CMD_TEST, NULL, 0, &ATRet);
  310. if (ATRet == 0)
  311. {
  312. RegChkStep++;
  313. }
  314. else
  315. {
  316. ChkState = -RegChkStep;
  317. return ChkState;
  318. }
  319. break;
  320. }
  321. case 1: // CPIN检查
  322. {
  323. AtcmdTransmit(AT_SIMREADY, NULL, 0, &ATRet);
  324. if (ATRet == 0)
  325. {
  326. RegChkStep++;
  327. }
  328. else
  329. {
  330. ChkState = -RegChkStep;
  331. return ChkState;
  332. }
  333. break;
  334. }
  335. case 2:
  336. {
  337. AtcmdTransmit(AT_CGREG, NULL, 0, &ATRet); //驻网检查,返回值1和5是驻网成功
  338. if (ATRet == 1 || ATRet == 5)
  339. {
  340. RegChkStep++;
  341. }
  342. else
  343. {
  344. ChkState = -RegChkStep;
  345. return ChkState;
  346. }
  347. break;
  348. }
  349. default:
  350. ChkState = RegChkStep;
  351. return ChkState;
  352. break;
  353. }
  354. }
  355. }
  356. void InitFunc(void)
  357. {
  358. // 4G模块初始化
  359. uint8 _4G_InitStep = 0;
  360. sint8 ATRet = -1;
  361. uint8 ReadLen = 0;
  362. char *ATCmdSend = NULL;
  363. uint8 ATCmdSendLen = 0;
  364. while (1)
  365. {
  366. switch (_4G_InitStep)
  367. {
  368. case 0: // AT指令同步
  369. {
  370. AtcmdTransmit(AT_CMD_TEST, NULL, 0, &ATRet);
  371. if (ATRet == 0)
  372. {
  373. _4G_InitStep++;
  374. }
  375. else
  376. {
  377. _4G_InitStep = 0;
  378. }
  379. break;
  380. }
  381. case 1: //关闭回显
  382. {
  383. ATCmdSend = (char *)("ATE0\r\n");
  384. ATCmdSendLen = mstrlen(ATCmdSend);
  385. memset(RX_Buffer[UART_LPUART1], 0x00, sizeof(RX_Buffer[UART_LPUART1]));
  386. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATCmdSend, ATCmdSendLen, RX_Buffer[UART_LPUART1], &ReadLen, 100);
  387. uint8 *retptr = NULL;
  388. if (ReadLen > 0)
  389. {
  390. retptr = (uint8 *)strstr((char *)RX_Buffer[UART_LPUART1], (char *)("OK"));
  391. if (retptr)
  392. {
  393. _4G_InitStep++;
  394. }
  395. else
  396. {
  397. _4G_InitStep = 0;
  398. }
  399. }
  400. else
  401. {
  402. _4G_InitStep = 0;
  403. }
  404. break;
  405. }
  406. case 2: // IMEI获取
  407. {
  408. ATCmdSend = (char *)("AT+SIMEI?\r\n");
  409. ATCmdSendLen = mstrlen(ATCmdSend);
  410. memset(RX_Buffer[UART_LPUART1], 0x00, sizeof(RX_Buffer[UART_LPUART1]));
  411. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATCmdSend, ATCmdSendLen, RX_Buffer[UART_LPUART1], &ReadLen, 100);
  412. uint8 *retptr = NULL;
  413. if (ReadLen > 0)
  414. {
  415. retptr = (uint8 *)strstr((char *)RX_Buffer[UART_LPUART1], (char *)("+SIMEI"));
  416. if (retptr)
  417. {
  418. memcpy(ImeiNum, retptr + 8, 15);
  419. _4G_InitStep++;
  420. }
  421. else
  422. {
  423. _4G_InitStep = 0;
  424. }
  425. }
  426. else
  427. {
  428. _4G_InitStep = 0;
  429. }
  430. break;
  431. }
  432. case 3: // 打开时间自动更新
  433. {
  434. ATCmdSend = (char *)("AT+CTZU=1\r\n");
  435. ATCmdSendLen = mstrlen(ATCmdSend);
  436. memset(RX_Buffer[UART_LPUART1], 0x00, sizeof(RX_Buffer[UART_LPUART1]));
  437. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATCmdSend, ATCmdSendLen, RX_Buffer[UART_LPUART1], &ReadLen, 100);
  438. uint8 *retptr = NULL;
  439. if (ReadLen > 0)
  440. {
  441. retptr = (uint8 *)strstr((char *)RX_Buffer[UART_LPUART1], (char *)("OK"));
  442. if (retptr)
  443. {
  444. _4G_InitStep++;
  445. }
  446. else
  447. {
  448. _4G_InitStep = 0;
  449. }
  450. }
  451. else
  452. {
  453. _4G_InitStep = 0;
  454. }
  455. break;
  456. }
  457. case 4: // ICCID获取
  458. {
  459. AtcmdTransmit(AT_GETICCID, NULL, 0, &ATRet);
  460. if (ATRet == 0)
  461. {
  462. _4G_InitStep++;
  463. }
  464. else
  465. {
  466. _4G_InitStep = 0;
  467. }
  468. break;
  469. }
  470. default:
  471. {
  472. return;
  473. break;
  474. }
  475. }
  476. }
  477. }
  478. static void AtcmdTransmit(sint8 CmdIdx, uint8 *SetValuePtr, uint16 SetValueLen, sint8 *retFunc)
  479. {
  480. uint16 ReadLen = 0;
  481. uint8 *PtrATCmdSend = NULL;
  482. uint8 ATCmdFixedLen = 0;
  483. uint16 ATCmdTotalLen = 0;
  484. ATCmdFixedLen = mstrlen(Atcmdfunc[CmdIdx].str);
  485. ATCmdTotalLen = ATCmdFixedLen + SetValueLen;
  486. PtrATCmdSend = malloc(ATCmdTotalLen + 1);
  487. memset(PtrATCmdSend, 0x00, ATCmdTotalLen + 1);
  488. memcpy(PtrATCmdSend, Atcmdfunc[CmdIdx].str, ATCmdFixedLen);
  489. if (SetValuePtr != NULL)
  490. {
  491. memcpy(PtrATCmdSend + ATCmdFixedLen, SetValuePtr, SetValueLen);
  492. }
  493. memset(RX_Buffer[UART_LPUART1], 0x00, sizeof(RX_Buffer[UART_LPUART1]));
  494. UART_Query_Data(UART_LPUART1, UART_LPUART1, PtrATCmdSend, ATCmdTotalLen, RX_Buffer[UART_LPUART1], &ReadLen, 100);
  495. *retFunc = Atcmdfunc[CmdIdx].cb(PtrATCmdSend, RX_Buffer[UART_LPUART1], CmdIdx, ReadLen);
  496. if (PtrATCmdSend != NULL)
  497. {
  498. memset(PtrATCmdSend, 0x00, ATCmdTotalLen + 1);
  499. free(PtrATCmdSend);
  500. }
  501. PtrATCmdSend = NULL;
  502. return;
  503. }
  504. sint8 at_callbackFunc(char *PSendStr, char *pReadStr, uint8 CmdIdx, uint16 pReadLen)
  505. {
  506. sint8 OutValue = -1;
  507. uint8 *retptr = NULL;
  508. char *OkAns = "OK";
  509. retptr = (uint8 *)strstr((char *)pReadStr, OkAns);
  510. switch (CmdIdx)
  511. {
  512. case AT_CMD_TEST:
  513. {
  514. if (retptr)
  515. {
  516. OutValue = 0;
  517. }
  518. break;
  519. }
  520. case AT_SIMREADY:
  521. {
  522. if (retptr)
  523. {
  524. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("READY"));
  525. if (retptr)
  526. {
  527. OutValue = 0;
  528. }
  529. }
  530. break;
  531. }
  532. case AT_GETICCID:
  533. {
  534. if (retptr)
  535. {
  536. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("ICCID:"));
  537. if (retptr)
  538. {
  539. memcpy(IccidNum, retptr + 7, 20);
  540. OutValue = 0;
  541. }
  542. }
  543. break;
  544. }
  545. case AT_CGREG:
  546. {
  547. if (retptr)
  548. {
  549. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("CGREG:"));
  550. if (retptr)
  551. {
  552. uint8 RegN = 0;
  553. uint8 RegState = 0;
  554. RegN = CharToHex(*(retptr + 7));
  555. RegState = CharToHex(*(retptr + 9));
  556. OutValue = (RegState + RegN);
  557. return OutValue;
  558. }
  559. }
  560. break;
  561. }
  562. case AT_NETOPEN:
  563. {
  564. if (retptr)
  565. {
  566. OutValue = 0;
  567. }
  568. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("opened")); //重复打开
  569. if (retptr)
  570. {
  571. OutValue = 0;
  572. }
  573. break;
  574. }
  575. case AT_CONNECTCHK:
  576. {
  577. if (retptr)
  578. {
  579. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("TCP"));
  580. if (retptr)
  581. {
  582. OutValue = CharToHex(*(retptr - 3));
  583. return OutValue;
  584. }
  585. }
  586. break;
  587. }
  588. case AT_CGIP:
  589. {
  590. if (retptr)
  591. {
  592. for (uint8 i = 0; i < 30; i++)
  593. {
  594. if (*(retptr - i) == ',')
  595. {
  596. memcpy(WebSiteIp, retptr - i + 1, 15);
  597. OutValue = 0;
  598. break;
  599. }
  600. }
  601. }
  602. break;
  603. }
  604. case AT_CONNECT:
  605. {
  606. if (retptr)
  607. {
  608. retptr = (uint8 *)strstr((char *)pReadStr, (char *)("CIPOPEN:"));
  609. if (retptr)
  610. {
  611. SocketId = CharToHex(*(retptr + 9));
  612. }
  613. OutValue = 0;
  614. }
  615. break;
  616. }
  617. case AT_SEND:
  618. {
  619. retptr = (uint8 *)strstr((char *)pReadStr, (char *)(">"));
  620. if (retptr)
  621. {
  622. OutValue = 0;
  623. }
  624. break;
  625. }
  626. default:
  627. break;
  628. }
  629. return OutValue;
  630. }
  631. void TcpDataEncode(uint32 *PtrSendAddr, uint16 *SendLen)
  632. {
  633. uint8 DataIdx = 0;
  634. static uint32 TcpSendTimeCounter = 0;
  635. TcpSendTimeCounter++;
  636. if (TcpSendTimeCounter == 1)
  637. {
  638. DataIdx = VerMsg; //版本信息发送
  639. }
  640. else if (TcpSendTimeCounter % 10 == 0)
  641. {
  642. DataIdx = BattMsg; //电池信息发送
  643. }
  644. else if ((TcpSendTimeCounter+1) % 10 == 0)
  645. {
  646. DataIdx = 0x83; //储能开关信息发送
  647. }
  648. else if (TcpSendTimeCounter % 240 == 0)
  649. {
  650. DataIdx = GpsMsg; //定位信息发送
  651. }
  652. else
  653. {
  654. *SendLen = 0;
  655. DataIdx = 0; //不发送,返回
  656. return;
  657. }
  658. UTC8TimeType UTC8TimeTcp;
  659. GetUtc8Time(&UTC8TimeTcp); //时间获取
  660. uint8 *SendBuffer = NULL;
  661. switch (DataIdx)
  662. {
  663. case 0x82:
  664. {
  665. GPSInfo GpsRecvData;
  666. GPSMsgtoTcpType GpsToTcpInfo;
  667. *SendLen = sizeof(GpsToTcpInfo);
  668. SendBuffer = malloc(*SendLen);
  669. uint16 tac = 0;
  670. uint32 cellId = 0;
  671. uint8 DataLen = (uint16)sizeof(GpsToTcpInfo.gpsInfo);
  672. GpsToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  673. GpsToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  674. GpsToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  675. GpsToTcpInfo.ansSymbol = TCP_ANS_SYM;
  676. memcpy(GpsToTcpInfo.SN, TcpbattSN, BATT_SN_LEN);
  677. GpsToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  678. GpsToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  679. GpsToTcpInfo.dataLength[1] = DataLen & 0xFF;
  680. GpsToTcpInfo.gpsInfo.sendTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  681. GpsToTcpInfo.gpsInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  682. GpsToTcpInfo.gpsInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  683. GpsToTcpInfo.gpsInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  684. GpsToTcpInfo.gpsInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  685. GpsToTcpInfo.gpsInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  686. GpsToTcpInfo.gpsInfo.msgMark = DataIdx;
  687. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  688. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  689. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  690. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  691. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  692. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  693. if (xQueueReceive(GpsDataQueueHandle, &GpsRecvData, 0) == pdPASS)
  694. {
  695. memcpy((uint8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, (uint8 *)&GpsRecvData, sizeof(GPSInfo));
  696. }
  697. else
  698. {
  699. memset((uint8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, 0x00, sizeof(GPSInfo));
  700. }
  701. GpsToTcpInfo.gpsInfo.Tac[0] = tac >> 8;
  702. GpsToTcpInfo.gpsInfo.Tac[1] = tac & 0xFF;
  703. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 24;
  704. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 16;
  705. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 8;
  706. GpsToTcpInfo.gpsInfo.CellID[0] = cellId;
  707. uint16 xyzDatacahce[3] = {0};
  708. memcpy(xyzDatacahce, xyzData, 3);
  709. for (uint8 i = 0; i < 3; i++)
  710. {
  711. if (xyzDatacahce[i] > 0x8000) // 数据为负
  712. {
  713. xyzDatacahce[i] = (uint16)((uint16)(~(xyzDatacahce[i])) + 1);
  714. xyzDatacahce[i] = 20000U - xyzDatacahce[i];
  715. }
  716. else
  717. {
  718. xyzDatacahce[i] = xyzDatacahce[i] + 20000U;
  719. }
  720. }
  721. GpsToTcpInfo.gpsInfo.xData[0] = xyzDatacahce[0] >> 8;
  722. GpsToTcpInfo.gpsInfo.xData[1] = xyzDatacahce[0];
  723. GpsToTcpInfo.gpsInfo.yData[0] = xyzDatacahce[1] >> 8;
  724. GpsToTcpInfo.gpsInfo.yData[1] = xyzDatacahce[1];
  725. GpsToTcpInfo.gpsInfo.zData[0] = xyzDatacahce[2] >> 8;
  726. GpsToTcpInfo.gpsInfo.zData[1] = xyzDatacahce[2];
  727. GpsToTcpInfo.CRC = bcc_chk((uint8 *)&GpsToTcpInfo, sizeof(GPSMsgtoTcpType) - 1);
  728. memcpy(SendBuffer, &GpsToTcpInfo, sizeof(GpsToTcpInfo));
  729. *PtrSendAddr = (uint32)SendBuffer;
  730. break;
  731. }
  732. case 0x86:
  733. {
  734. VersionMsgtoTcpType VerMsgToTcpInfo;
  735. *SendLen = sizeof(VersionMsgtoTcpType);
  736. SendBuffer = malloc(*SendLen);
  737. uint16 DataLen = 0;
  738. DataLen = (uint16)sizeof(VerMsgToTcpInfo.VerInfo);
  739. VerMsgToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  740. VerMsgToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  741. VerMsgToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  742. VerMsgToTcpInfo.ansSymbol = TCP_ANS_SYM;
  743. memcpy(VerMsgToTcpInfo.SN, TcpbattSN, BATT_SN_LEN);
  744. VerMsgToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  745. VerMsgToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  746. VerMsgToTcpInfo.dataLength[1] = DataLen & 0xFF;
  747. VerMsgToTcpInfo.VerInfo.sendTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  748. VerMsgToTcpInfo.VerInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  749. VerMsgToTcpInfo.VerInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  750. VerMsgToTcpInfo.VerInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  751. VerMsgToTcpInfo.VerInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  752. VerMsgToTcpInfo.VerInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  753. VerMsgToTcpInfo.VerInfo.msgMark = DataIdx;
  754. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  755. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  756. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  757. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  758. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  759. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  760. memcpy(VerMsgToTcpInfo.VerInfo.ICCID, IccidNum, 20);
  761. memcpy(VerMsgToTcpInfo.VerInfo.IMEI, ImeiNum, 15);
  762. VerMsgToTcpInfo.VerInfo.BMSHwVersion[0] = bmsHwVersion>>8;
  763. VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = bmsHwVersion;
  764. VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = bmsSwVersion>>24;
  765. VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = bmsSwVersion>>16;
  766. VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = bmsSwVersion>>8;
  767. VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = bmsSwVersion;
  768. VerMsgToTcpInfo.VerInfo.HwVersion[0] = (HWVERSION >> 8) & 0xFF;
  769. VerMsgToTcpInfo.VerInfo.HwVersion[1] = (HWVERSION)&0xFF;
  770. VerMsgToTcpInfo.VerInfo.BLVersion[0] = (BLSWVERSION >> 24) & 0xFF;
  771. VerMsgToTcpInfo.VerInfo.BLVersion[1] = (BLSWVERSION >> 16) & 0xFF;
  772. VerMsgToTcpInfo.VerInfo.BLVersion[2] = (BLSWVERSION >> 8) & 0xFF;
  773. VerMsgToTcpInfo.VerInfo.BLVersion[3] = (BLSWVERSION)&0xFF;
  774. VerMsgToTcpInfo.VerInfo.DRVVersion[0] = (DRVSWVERSION >> 24) & 0xFF;
  775. VerMsgToTcpInfo.VerInfo.DRVVersion[1] = (DRVSWVERSION >> 16) & 0xFF;
  776. VerMsgToTcpInfo.VerInfo.DRVVersion[2] = (DRVSWVERSION >> 8) & 0xFF;
  777. VerMsgToTcpInfo.VerInfo.DRVVersion[3] = (DRVSWVERSION)&0xFF;
  778. VerMsgToTcpInfo.VerInfo.APPVersion[0] = (APPSWVERSION >> 24) & 0xFF;
  779. VerMsgToTcpInfo.VerInfo.APPVersion[1] = (APPSWVERSION >> 16) & 0xFF;
  780. VerMsgToTcpInfo.VerInfo.APPVersion[2] = (APPSWVERSION >> 8) & 0xFF;
  781. VerMsgToTcpInfo.VerInfo.APPVersion[3] = (APPSWVERSION)&0xFF;
  782. VerMsgToTcpInfo.VerInfo.BmsType = BMS_MANUFACTURE;
  783. VerMsgToTcpInfo.VerInfo.BmsInfo = BMS_INFO;
  784. VerMsgToTcpInfo.VerInfo.DataModuleType = DATA_MODULE_TYPE;
  785. VerMsgToTcpInfo.CRC = bcc_chk((uint8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo) - 1);
  786. memcpy(SendBuffer, &VerMsgToTcpInfo, sizeof(VersionMsgtoTcpType));
  787. *PtrSendAddr = (uint32)SendBuffer;
  788. break;
  789. }
  790. case 0x80:
  791. {
  792. uint16 ErrorTemp;
  793. ErrorTemp = GetErrorNum(ErrorArray,sizeof(ErrorArray)/2);
  794. uint8 ProtocolHeaderLen = 25; //电池信息协议头部加校验码长度,此长度不更改
  795. uint8 ProtocolFixedLen = 60; //电池信息协议固定总长度 如协议新增,需要更改此长度
  796. uint16 ProtocolFluctedLen = AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + BmsOtherTemp + OtherTemp; //电池信息协议变动长度
  797. uint16 DataLen;
  798. *SendLen = ProtocolFixedLen + ProtocolFluctedLen;
  799. SendBuffer = malloc(*SendLen);
  800. if (SendBuffer == NULL)
  801. {
  802. return;
  803. }
  804. *(SendBuffer + 0) = TCP_START_SYM1; //起始码-1
  805. *(SendBuffer + 1) = TCP_START_SYM2; //起始码-2
  806. *(SendBuffer + 2) = TCP_CMD_SYM; //命令标识-3
  807. *(SendBuffer + 3) = TCP_ANS_SYM; //应答标识-4
  808. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN); // SN码 5-21
  809. *(SendBuffer + 21) = TCP_ENCPT_DISABLE; //加密方式-22
  810. DataLen = ProtocolFixedLen + ProtocolFluctedLen - ProtocolHeaderLen; //电池信息单元协议固定长度
  811. *(SendBuffer + 22) = (DataLen >> 8) & 0xFF; //数据长度H-23
  812. *(SendBuffer + 23) = DataLen & 0xFF; //数据长度L-24
  813. *(SendBuffer + 24) = (UTC8TimeTcp.year) & 0xFF; // year-25
  814. *(SendBuffer + 25) = UTC8TimeTcp.month & 0xFF; // month-26
  815. *(SendBuffer + 26) = UTC8TimeTcp.day & 0xFF; // day-27
  816. *(SendBuffer + 27) = UTC8TimeTcp.hour & 0xFF; // hour-28
  817. *(SendBuffer + 28) = UTC8TimeTcp.minute & 0xFF; // mins-29
  818. *(SendBuffer + 29) = UTC8TimeTcp.second & 0xFF; // sec-30
  819. *(SendBuffer + 30) = DataIdx; //电池信息发送-31
  820. *(SendBuffer + 31) = (UTC8TimeTcp.year) & 0xFF; // year-32
  821. *(SendBuffer + 32) = UTC8TimeTcp.month & 0xFF; // month-33
  822. *(SendBuffer + 33) = UTC8TimeTcp.day & 0xFF; // day-34
  823. *(SendBuffer + 34) = UTC8TimeTcp.hour & 0xFF; // hour-35
  824. *(SendBuffer + 35) = UTC8TimeTcp.minute & 0xFF; // mins-36
  825. *(SendBuffer + 36) = UTC8TimeTcp.second & 0xFF; // sec-37
  826. *(SendBuffer + 37) = CSQValue; //信号强度-38
  827. *(SendBuffer + 38) = 0; //故障等级-39
  828. *(SendBuffer + 39) = ErrorTemp >> 8; //故障代码H-40
  829. *(SendBuffer + 40) = ErrorTemp & 0xFF; //故障代码L-41
  830. *(SendBuffer + 41) = battI >> 8; //电流-42
  831. *(SendBuffer + 42) = battI & 0xFF; //电流-43
  832. *(SendBuffer + 43) = battPackVol >> 8; //电压-44
  833. *(SendBuffer + 44) = battPackVol & 0xFF; //电压-45
  834. *(SendBuffer + 45) = battPackVol >> 8; //电压-46
  835. *(SendBuffer + 46) = battPackVol & 0xFF; //电压-47
  836. *(SendBuffer + 47) = battMOSSwitchState; // mos状态-48
  837. *(SendBuffer + 48) = (uint8)(battSOC); // soc-49
  838. *(SendBuffer + 49) = (uint8)(battSOH); // soh-50
  839. *(SendBuffer + 50) = (battBalanceoInfo >> 24) & 0xFF;
  840. *(SendBuffer + 51) = (battBalanceoInfo >> 16) & 0xFF;
  841. *(SendBuffer + 52) = (battBalanceoInfo >> 8) & 0xFF;
  842. *(SendBuffer + 53) = battBalanceoInfo & 0xFF; //均衡状态-51-54
  843. *(SendBuffer + 54) = AppDataInfo.BattCellCount; //电压个数-55
  844. for (uint8 i = 0; i < AppDataInfo.BattCellCount; i++) //单体电压
  845. {
  846. *(SendBuffer + 54 + i * 2 + 1) = (battCellU[i] >> 8) & 0xFF;
  847. *(SendBuffer + 54 + i * 2 + 2) = battCellU[i] & 0xFF;
  848. }
  849. *(SendBuffer + 55 + AppDataInfo.BattCellCount * 2) = AppDataInfo.BattTempCount; //模组温度个数
  850. memcpy((SendBuffer + 55 + AppDataInfo.BattCellCount * 2 + 1), &battCellTemp, AppDataInfo.BattTempCount); //模组温度
  851. *(SendBuffer + 56 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = battWorkState; //电池工作状态
  852. *(SendBuffer + 57 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = battHeatEnableState; //电池加热使能状态
  853. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = BmsOtherTemp + OtherTemp; //其他温度个数
  854. // *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 1) = MOSTemp; // mos温度
  855. // *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 2) = packTemp; //环境温度
  856. // *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 3) = fastChargeTemp; //快充温度
  857. // *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 4) = normalChargeTemp; //慢充温度
  858. // *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 5) = heatTemp1; //加热温度1
  859. // *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 6) = heatTemp2; //加热温度2
  860. *(SendBuffer + 59 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + BmsOtherTemp + OtherTemp) = bcc_chk(SendBuffer, ProtocolFixedLen + ProtocolFluctedLen - 1); //校验码
  861. *PtrSendAddr = (uint32)SendBuffer;
  862. break;
  863. }
  864. case 0x83://储能场景的开关信息
  865. {
  866. StorageInfoToTcp StorageInfo;
  867. *SendLen = sizeof(StorageInfo);
  868. SendBuffer = malloc(*SendLen);
  869. uint16 DataLen = 0;
  870. DataLen = (uint16)sizeof(StorageInfo.StorageMsg);
  871. StorageInfo.startSymbol[0] = TCP_START_SYM1;
  872. StorageInfo.startSymbol[1] = TCP_START_SYM2;
  873. StorageInfo.cmdSymbol = TCP_CMD_SYM;
  874. StorageInfo.ansSymbol = TCP_ANS_SYM;
  875. memcpy(StorageInfo.SN, TcpbattSN, BATT_SN_LEN);
  876. StorageInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  877. StorageInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  878. StorageInfo.dataLength[1] = DataLen & 0xFF;
  879. StorageInfo.StorageMsg.sendTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  880. StorageInfo.StorageMsg.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  881. StorageInfo.StorageMsg.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  882. StorageInfo.StorageMsg.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  883. StorageInfo.StorageMsg.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  884. StorageInfo.StorageMsg.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  885. StorageInfo.StorageMsg.msgMark = DataIdx;
  886. StorageInfo.StorageMsg.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year) & 0xFF; // year
  887. StorageInfo.StorageMsg.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  888. StorageInfo.StorageMsg.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  889. StorageInfo.StorageMsg.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  890. StorageInfo.StorageMsg.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  891. StorageInfo.StorageMsg.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  892. StorageInfo.StorageMsg.RelayState = battRelayState;
  893. StorageInfo.StorageMsg.FanState = battFanState;
  894. StorageInfo.StorageMsg.HeatState = battHeatState;
  895. StorageInfo.StorageMsg.ConvertState = battConverState;
  896. StorageInfo.CRC = bcc_chk((uint8 *)&StorageInfo, sizeof(StorageInfo) - 1);
  897. memcpy(SendBuffer, &StorageInfo, sizeof(StorageInfo));
  898. *PtrSendAddr = (uint32)SendBuffer;
  899. break;
  900. }
  901. default:
  902. break;
  903. }
  904. }
  905. void GetUtc8Time(UTC8TimeType *UTC8TimeTcp)
  906. {
  907. char *AtCmdAsk = (char *)("AT+CCLK?\r\n");
  908. uint8 AtCmdLen = mstrlen(AtCmdAsk);
  909. uint8 ReadLen = 0;
  910. uint8 *retptr = NULL;
  911. memset(RX_Buffer[UART_LPUART1], 0x00, sizeof(RX_Buffer[UART_LPUART1]));
  912. UART_Query_Data(UART_LPUART1, UART_LPUART1, AtCmdAsk, AtCmdLen, RX_Buffer[UART_LPUART1], &ReadLen, 100);
  913. if (ReadLen > 0)
  914. {
  915. if ((uint8 *)strstr((char *)RX_Buffer[UART_LPUART1], (char *)("OK")))
  916. {
  917. retptr = (uint8 *)strstr((char *)RX_Buffer[UART_LPUART1], (char *)("+CCLK:"));
  918. UTC8TimeTcp->year = CharToHex(*(retptr + 8)) * 10 + CharToHex(*(retptr + 9));
  919. UTC8TimeTcp->month = CharToHex(*(retptr + 11)) * 10 + CharToHex(*(retptr + 12));
  920. UTC8TimeTcp->day = CharToHex(*(retptr + 14)) * 10 + CharToHex(*(retptr + 15));
  921. UTC8TimeTcp->hour = CharToHex(*(retptr + 17)) * 10 + CharToHex(*(retptr + 18));
  922. UTC8TimeTcp->minute = CharToHex(*(retptr + 20)) * 10 + CharToHex(*(retptr + 21));
  923. UTC8TimeTcp->second = CharToHex(*(retptr + 23)) * 10 + CharToHex(*(retptr + 24));
  924. }
  925. }
  926. }
  927. static void TcpDataInfoRecvHandle(uint8 *DataRecv,uint16 DataRecvLen)
  928. {
  929. uint8 Tcp_Cmd;
  930. uint8 *Ptr=NULL,*retptr=NULL;
  931. uint8 TcpCmdAnswer[31];
  932. uint16 TcpDataLen = 0;
  933. uint16 NumCalTemp = 1;
  934. retptr = (uint8 *)strstr((char *)DataRecv, (char *)("\r\n##"));
  935. if(retptr==NULL)return;
  936. for(uint8 i=0;i<5;i++)
  937. {
  938. if(*(retptr - i - 1)=='D')
  939. {
  940. break;
  941. }
  942. TcpDataLen = TcpDataLen + CharToHex(*(retptr - i - 1))*NumCalTemp;
  943. NumCalTemp = NumCalTemp*10;
  944. }
  945. if (TcpDataLen>0)
  946. {
  947. Ptr = retptr + 2;
  948. if ((*(Ptr + 0) == TCP_START_SYM1) && (*(Ptr + 1) == TCP_START_SYM2)) //服务器起始信息
  949. {
  950. Tcp_Cmd = *(Ptr + 2); //命令标志
  951. // if (*(Ptr + 3) == 0xFE)
  952. // {
  953. // TCPWorkState = 0x01;//需要暂停发送,进行命令应答的标志
  954. // }
  955. switch (Tcp_Cmd)
  956. {
  957. case TCP_QUERY_SYM:
  958. break;
  959. case TCP_SETCMD_SYM:
  960. break;
  961. case TCP_CONCMD_SYM:
  962. {
  963. TcpCmdAnswer[0] = TCP_START_SYM1;
  964. TcpCmdAnswer[1] = TCP_START_SYM1;
  965. TcpCmdAnswer[2] = TCP_CONCMD_SYM;
  966. if (*(Ptr + 30) == 0x80) //远程锁定命令
  967. {
  968. TcpCmdAnswer[3] = 0x01;
  969. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  970. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  971. TcpCmdAnswer[22] = 0x00;
  972. TcpCmdAnswer[23] = 0x06;
  973. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  974. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  975. if (*(Ptr + 31) == 0x01) // 0x01代表锁定
  976. {
  977. battSeparateEnable = 1;
  978. battSeparateEnableState = 1;
  979. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31 );
  980. }
  981. else if (*(Ptr + 31) == 0x02) // 0x02代表解锁
  982. {
  983. battSeparateEnable = 1;
  984. battSeparateEnableState = 0;
  985. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  986. }
  987. }
  988. }
  989. default:
  990. {
  991. break;
  992. }
  993. }
  994. }
  995. }
  996. }
  997. static sint8 tcpipConnectionSend(uint8 TcpConnectId,uint8 * SendDataPtr,uint16 SendDataLen)
  998. {
  999. sint8 outValue = -1;
  1000. sint8 ATRet = -1;
  1001. uint8 ReadLen = 0;
  1002. char AtCmdSend[10] = {0};
  1003. uint8 AtCmdSendTotalLen = 0;
  1004. sprintf(AtCmdSend, "%d,%d\r\n", TcpConnectId, SendDataLen);
  1005. AtCmdSendTotalLen = mstrlen(AtCmdSend);
  1006. AtcmdTransmit(AT_SEND, AtCmdSend, AtCmdSendTotalLen, &ATRet);
  1007. if (ATRet == 0)
  1008. {
  1009. memset(RX_Buffer[UART_LPUART1], 0x00, sizeof(RX_Buffer[UART_LPUART1]));
  1010. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)SendDataPtr, SendDataLen, RX_Buffer[UART_LPUART1], &ReadLen, 100);
  1011. if ((uint8 *)strstr((char *)RX_Buffer[UART_LPUART1], (char *)("OK")))
  1012. {
  1013. outValue = 0;
  1014. }
  1015. }
  1016. return outValue;
  1017. }