AppTaskUart1.c 56 KB

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