AppTaskUart1.c 70 KB

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