AppTaskUart1.c 44 KB

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