AppTaskUart1.c 51 KB

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