AppTaskUart1.c 57 KB

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