AppTaskUart1.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  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. returnFreq[1][0], returnFreq[1][1], returnFreq[1][2], returnFreq[1][3], returnFreq[1][4],
  1025. returnP[1][0], returnP[1][1], returnP[1][2], returnP[1][3], returnP[1][4],
  1026. returnFreq[2][0], returnFreq[2][1], returnFreq[2][2], returnFreq[2][3], returnFreq[2][4],
  1027. returnP[2][0], returnP[2][1], returnP[2][2], returnP[2][3], returnP[2][4],tmp,DeviceErrNum,
  1028. socd_pct_ahSoc,socd_pct_battSoc,socd_pct_bcuSoc,socd_pct_ekfSoc,
  1029. BBox_TotalCharEngy,BBox_TotalDischarEngy,BBox_TotalBackCharEngy,BBox_TotalCharCapy,BBox_TotalDischarCapy,BBox_TotalBackCharCapy);
  1030. BufferLen = strlen((const char *)rbuf);
  1031. *SendLen = BufferLen + sizeof(DebugMsgInfo);
  1032. SendBuffer = pvPortMalloc(*SendLen);
  1033. memcpy(SendBuffer + sizeof(DebugMsgInfo) - 1, rbuf, BufferLen);
  1034. DataLen = sizeof(DebugMsgInfo.DebugInfo) + BufferLen;
  1035. DebugMsgInfo.startSymbol[0] = TCP_START_SYM1;
  1036. DebugMsgInfo.startSymbol[1] = TCP_START_SYM2;
  1037. DebugMsgInfo.cmdSymbol = TCP_CMD_SYM;
  1038. DebugMsgInfo.ansSymbol = TCP_ANS_SYM;
  1039. memcpy(DebugMsgInfo.SN, TcpbattSN, BATT_SN_LEN);
  1040. DebugMsgInfo.encryptMethod = TCP_ENCPT_DISABLE; // not encrypt
  1041. DebugMsgInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  1042. DebugMsgInfo.dataLength[1] = DataLen & 0xFF;
  1043. DebugMsgInfo.DebugInfo.sendTimeUTC[0] = UTC8TimeTcp.year & 0xFF; // year
  1044. DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; // month
  1045. DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; // day
  1046. DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; // hour
  1047. DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; // mins
  1048. DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; // sec
  1049. DebugMsgInfo.DebugInfo.msgMark = DebugMsg;
  1050. DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen >> 8;
  1051. DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen;
  1052. memcpy(SendBuffer, (UINT8 *)&DebugMsgInfo, sizeof(DebugMsgInfo) - 1);
  1053. DebugMsgInfo._CRC = bcc_chk(SendBuffer, BufferLen + sizeof(DebugMsgInfo) - 1);
  1054. memcpy(SendBuffer + BufferLen + sizeof(DebugMsgInfo) - 1, &DebugMsgInfo._CRC, 1);
  1055. *PtrSendAddr = (uint32)SendBuffer;
  1056. break;
  1057. }
  1058. case 0x90:
  1059. {
  1060. *SendLen = 0x52;
  1061. SendBuffer = pvPortMalloc(*SendLen);
  1062. *(SendBuffer + 0) = TCP_START_SYM1;
  1063. *(SendBuffer + 1) = TCP_START_SYM2;
  1064. *(SendBuffer + 2) = TCP_CMD_SYM;
  1065. *(SendBuffer + 3) = TCP_ANS_SYM;
  1066. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1067. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1068. uint16 DataLen = 0x39; // 57
  1069. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1070. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1071. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1072. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1073. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1074. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1075. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1076. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1077. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8 BATTMSG
  1078. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1079. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1080. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1081. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1082. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1083. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1084. memcpy(SendBuffer + 0x25, VIN, 17);
  1085. *(SendBuffer + 0x36) = (vehicleStatus & 0xFF); // uint8 整车状态
  1086. if (BMS_Mode == 2)
  1087. {
  1088. bmsHVOn = 1;
  1089. }
  1090. else
  1091. {
  1092. bmsHVOn = 2;
  1093. }
  1094. *(SendBuffer + 0x37) = (bmsHVOn & 0xFF); // uint8 bms上高压指令
  1095. *(SendBuffer + 0x38) = (currentGearPosition & 0xFF); // uint8 当前档位
  1096. *(SendBuffer + 0x39) = (parkingBreakStatus & 0xFF); // uint8 手刹信号
  1097. *(SendBuffer + 0x3A) = (breakingStatus & 0xFF); // uint8 制动开关
  1098. *(SendBuffer + 0x3B) = ((ODO >> 24) & 0xFF); // uint32 总里程
  1099. *(SendBuffer + 0x3C) = ((ODO >> 16) & 0xFF);
  1100. *(SendBuffer + 0x3D) = ((ODO >> 8) & 0xFF);
  1101. *(SendBuffer + 0x3E) = (ODO & 0xFF);
  1102. *(SendBuffer + 0x3F) = (dcdcWorkStatus & 0xFF); // uint8 DCDC状态
  1103. *(SendBuffer + 0x40) = (numOfChrgableSubsys & 0xFF); // uint8 可充电子系统数
  1104. *(SendBuffer + 0x41) = (chrgableSubsysCode & 0xFF); // uint8 可充电储能子系统号
  1105. *(SendBuffer + 0x42) = (BMS_MaxCellTempCSC & 0xFF); // uint8 最高温度子系统号 精度_1,偏移量_0,单位_
  1106. *(SendBuffer + 0x43) = (BMS_MaxCellTempNum & 0xFF); // uint8 最高温度探针单体代号 精度_1,偏移量_0,单位_
  1107. *(SendBuffer + 0x44) = (BMS_MinCellTempCSC & 0xFF); // uint8 最低温度子系统号 精度_1,偏移量_0,单位_
  1108. *(SendBuffer + 0x45) = (BMS_MinCellTempNum & 0xFF); // uint8 最低温度探针子系统代号 精度_1,偏移量_0,单位_
  1109. *(SendBuffer + 0x46) = (BMS_MaxCellVoltCSC & 0xFF); // uint8 最高电压电池子系统号 精度_1,偏移量_0,单位_
  1110. *(SendBuffer + 0x47) = ((BMS_MaxCellVoltNum >> 8) & 0xFF); // uint16 最高电压电池单体代号 精度_1,偏移量_0,单位_
  1111. *(SendBuffer + 0x48) = (BMS_MaxCellVoltNum & 0xFF);
  1112. *(SendBuffer + 0x49) = (BMS_MinCellVoltCSC & 0xFF); // uint8 最低电压电池子系统号 精度_1,偏移量_0,单位_
  1113. *(SendBuffer + 0x4A) = ((BMS_MinCellVoltNum >> 8) & 0xFF); // uint16 最低电压电池单体代号 精度_1,偏移量_0,单位_
  1114. *(SendBuffer + 0x4B) = (BMS_MinCellVoltNum & 0xFF);
  1115. *(SendBuffer + 0x4C) = (ebcStatus & 0xFF); // uint8 换电控制器状态
  1116. *(SendBuffer + 0x4D) = (uint8)((ebcAskHVOn & 0x01) | ((ebcAskHVOff & 0x01) << 1) | ((retainLockSignal & 0x01) << 3) | ((dischargeLockSignal & 0x01) << 5) | ((chargeLockSignal & 0x01) << 6));
  1117. *(SendBuffer + 0x4E) = (chargeFlag & 0xFF); // uint8 充电标志位
  1118. *(SendBuffer + 0x4F) = ((vcuDCVol >> 8) & 0xFF); // uint16 电机直流母线电压
  1119. *(SendBuffer + 0x50) = (vcuDCVol & 0xFF);
  1120. *(SendBuffer + 0x51) = bcc_chk(SendBuffer, 0x51);
  1121. *PtrSendAddr = (uint32)SendBuffer;
  1122. break;
  1123. }
  1124. case 0x91:
  1125. {
  1126. if(BMS_CellRecvFlg==false)//CAN上电压数据未接收完成
  1127. {
  1128. *SendLen = 0; // 不发送,长度为0
  1129. return;
  1130. }
  1131. GetCSQValue(&CSQValue);
  1132. *SendLen = 0x54 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM);
  1133. SendBuffer = pvPortMalloc(*SendLen);
  1134. *(SendBuffer + 0) = TCP_START_SYM1;
  1135. *(SendBuffer + 1) = TCP_START_SYM2;
  1136. *(SendBuffer + 2) = TCP_CMD_SYM;
  1137. *(SendBuffer + 3) = TCP_ANS_SYM;
  1138. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1139. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1140. uint16 DataLen = 0x3B + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2 + min(BMS_TempTotal, BMS_TEMP_MAX_NUM);
  1141. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1142. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1143. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1144. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1145. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1146. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1147. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1148. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1149. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8 BATTMSG
  1150. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1151. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1152. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1153. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1154. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1155. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1156. *(SendBuffer + 0x25) = (CSQValue & 0xFF); // uint8 csq
  1157. if (BMS_Mode == 2)
  1158. {
  1159. bmsHVOn = 1;
  1160. }
  1161. else
  1162. {
  1163. bmsHVOn = 2;
  1164. }
  1165. *(SendBuffer + 0x26) = (bmsHVOn & 0xFF); // uint8 BMS当前状态,0:预留;1:高压上电;2:高压下电;3:无效;,Re-1,Off-0,
  1166. *(SendBuffer + 0x27) = (BMS_CharGunSt & 0xFF); // uint8 直流充电枪连接状态,0:未连接1 : 连接, Re - 1, Off - 0,
  1167. *(SendBuffer + 0x28) = (BMS_FtLvl & 0xFF); // uint8 当前最高故障等级,0-正常 1-1级 轻微故障 2-2级 较严重故障 3-3级 最严重故障,精度_1,偏移量_0,单位_
  1168. *(SendBuffer + 0x29) = (BMS_FtCode & 0xFF); // uint8 故障码,见BMS_ErrCode,精度_1,偏移量_0,单位_
  1169. *(SendBuffer + 0x2A) = (BMS_ReqHVOff & 0xFF); // uint8 BMS下高压请求,0-无效 1-请求下高压 2-不请求下高压 3-无效,精度_1,偏移量_0,单位_
  1170. *(SendBuffer + 0x2B) = (BMS_CharSt & 0xFF); // uint8 充电状态,0-未充电 1-充电中 2-充电已完成 3-充电错误故障,精度_1,偏移量_0,单位_
  1171. *(SendBuffer + 0x2C) = (BMS_PackSOC & 0xFF); // uint8 电池包SOC 精度_0.4,偏移量_0,单位_
  1172. *(SendBuffer + 0x2D) = (BMS_PackSOH / 25 * 10) & 0xFF; // SOH 精度1%
  1173. *(SendBuffer + 0x2E) = (BMS_BattVolt >> 8) & 0xFF; // 电池包总电压
  1174. *(SendBuffer + 0x2F) = (BMS_BattVolt & 0xFF);
  1175. *(SendBuffer + 0x30) = ((BMS_PackCurr >> 8) & 0xFF); // uint16 电池包总电流,充电为负值,放电为正值 精度_0.1,偏移量_-1000,单位_A
  1176. *(SendBuffer + 0x31) = (BMS_PackCurr & 0xFF);
  1177. *(SendBuffer + 0x32) = ((BMS_SysInsRes >> 8) & 0xFF); // uint16 系统绝缘电阻 精度_1,偏移量_0,单位_KΩ
  1178. *(SendBuffer + 0x33) = (BMS_SysInsRes & 0xFF);
  1179. /*28 Byte 继电器状态*/
  1180. *(SendBuffer + 0x34) = (BMS_StPosRly & 0xFF); // uint8 主正继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1181. *(SendBuffer + 0x35) = (BMS_StPreCharRly & 0xFF); // uint8 预充继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1182. *(SendBuffer + 0x36) = (BMS_StNegRly & 0xFF); // uint8 主负继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1183. *(SendBuffer + 0x37) = 0; // 三合一
  1184. *(SendBuffer + 0x38) = 0; // PTC
  1185. *(SendBuffer + 0x39) = 0; // 空调
  1186. *(SendBuffer + 0x3A) = (BMS_StPosCharRly1 & 0xFF); // uint8 直流充正继电器1状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1187. *(SendBuffer + 0x3B) = (BMS_StNegCharRly1 & 0xFF); // uint8 直流充负继电器1状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1188. *(SendBuffer + 0x3C) = (BMS_StPosCharRly2 & 0xFF); // uint8 直流充正继电器2状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1189. *(SendBuffer + 0x3D) = (BMS_StNegCharRly2 & 0xFF); // uint8 直流充负继电器2状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1190. *(SendBuffer + 0x3E) = (BMS_StPosHeatRly & 0xFF); // uint8 加热正继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1191. *(SendBuffer + 0x3F) = (BMS_StNegHeatRly & 0xFF); // uint8 加热负继电器状态,0-预留 1-断开 2-吸合 ,精度_1,偏移量_0,单位_
  1192. *(SendBuffer + 0x40) = 0;
  1193. /*28-Byte继电器状态结束*/
  1194. *(SendBuffer + 0x50) = ((BMS_CellTotal >> 8) & 0xFF); // uint16 PACK中单体电芯的总数目
  1195. *(SendBuffer + 0x51) = (BMS_CellTotal & 0xFF);
  1196. for (uint16 index = 0; index < min(BMS_CellTotal, BMS_CELL_MAX_NUM); index++)
  1197. {
  1198. *(SendBuffer + 0x52 + index * 2) = ((BMS_CellVolt[index] >> 8) & 0xFF);
  1199. *(SendBuffer + 0x52 + index * 2 + 1) = ((BMS_CellVolt[index]) & 0xFF);
  1200. }
  1201. *(SendBuffer + 0x52 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2) = (BMS_TempTotal & 0xFF); // uint8 PACK中电芯温度点(探针)的总数目
  1202. memcpy(SendBuffer + 0x53 + min(BMS_CellTotal, BMS_CELL_MAX_NUM) * 2, BMS_CellTemp, min(BMS_TempTotal, BMS_TEMP_MAX_NUM));
  1203. *(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));
  1204. *PtrSendAddr = (uint32)SendBuffer;
  1205. break;
  1206. }
  1207. case 0x92:
  1208. {
  1209. *SendLen = 0x4E;
  1210. SendBuffer = pvPortMalloc(*SendLen);
  1211. *(SendBuffer + 0) = TCP_START_SYM1;
  1212. *(SendBuffer + 1) = TCP_START_SYM2;
  1213. *(SendBuffer + 2) = TCP_CMD_SYM;
  1214. *(SendBuffer + 3) = TCP_ANS_SYM;
  1215. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1216. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1217. uint16 DataLen = 0x35;
  1218. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1219. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1220. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1221. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1222. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1223. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1224. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1225. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1226. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8
  1227. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1228. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1229. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1230. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1231. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1232. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1233. *(SendBuffer + 0x25) = ((BBox_TotalCharCapy >> 24) & 0xFF); // uint32 累积充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1234. *(SendBuffer + 0x26) = ((BBox_TotalCharCapy >> 16) & 0xFF);
  1235. *(SendBuffer + 0x27) = ((BBox_TotalCharCapy >> 8) & 0xFF);
  1236. *(SendBuffer + 0x28) = (BBox_TotalCharCapy & 0xFF);
  1237. *(SendBuffer + 0x29) = ((BBox_TotalDischarCapy >> 24) & 0xFF); // uint32 累积放出Ah 数 精度_0.1,偏移量_0,单位_Ah
  1238. *(SendBuffer + 0x2A) = ((BBox_TotalDischarCapy >> 16) & 0xFF);
  1239. *(SendBuffer + 0x2B) = ((BBox_TotalDischarCapy >> 8) & 0xFF);
  1240. *(SendBuffer + 0x2C) = (BBox_TotalDischarCapy & 0xFF);
  1241. *(SendBuffer + 0x2D) = ((BMS_TotalCharEngy >> 24) & 0xFF); // uint32 累计充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1242. *(SendBuffer + 0x2E) = ((BMS_TotalCharEngy >> 16) & 0xFF);
  1243. *(SendBuffer + 0x2F) = ((BMS_TotalCharEngy >> 8) & 0xFF);
  1244. *(SendBuffer + 0x30) = (BMS_TotalCharEngy & 0xFF);
  1245. *(SendBuffer + 0x31) = ((BMS_TotalDischarEngy >> 24) & 0xFF); // uint32 累计放出kWh 数 精度_0.1,偏移量_0,单位_KWh
  1246. *(SendBuffer + 0x32) = ((BMS_TotalDischarEngy >> 16) & 0xFF);
  1247. *(SendBuffer + 0x33) = ((BMS_TotalDischarEngy >> 8) & 0xFF);
  1248. *(SendBuffer + 0x34) = (BMS_TotalDischarEngy & 0xFF);
  1249. *(SendBuffer + 0x35) = ((BBox_TotalBackCharCapy >> 24) & 0xFF); // uint32 累计动能回馈充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1250. *(SendBuffer + 0x36) = ((BBox_TotalBackCharCapy >> 16) & 0xFF);
  1251. *(SendBuffer + 0x37) = ((BBox_TotalBackCharCapy >> 8) & 0xFF);
  1252. *(SendBuffer + 0x38) = (BBox_TotalBackCharCapy & 0xFF);
  1253. *(SendBuffer + 0x39) = ((BMS_TotalBackCharEngy >> 24) & 0xFF); // uint32 累计动能回馈充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1254. *(SendBuffer + 0x3A) = ((BMS_TotalBackCharEngy >> 16) & 0xFF);
  1255. *(SendBuffer + 0x3B) = ((BMS_TotalBackCharEngy >> 8) & 0xFF);
  1256. *(SendBuffer + 0x3C) = (BMS_TotalBackCharEngy & 0xFF);
  1257. *(SendBuffer + 0x3D) = ((BBox_TotalStaCharCapy >> 24) & 0xFF); // uint32 累计换电站充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1258. *(SendBuffer + 0x3E) = ((BBox_TotalStaCharCapy >> 16) & 0xFF);
  1259. *(SendBuffer + 0x3F) = ((BBox_TotalStaCharCapy >> 8) & 0xFF);
  1260. *(SendBuffer + 0x40) = (BBox_TotalStaCharCapy & 0xFF);
  1261. *(SendBuffer + 0x41) = ((BMS_TotalStaCharEngy >> 24) & 0xFF); // uint32 累计换电站充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1262. *(SendBuffer + 0x42) = ((BMS_TotalStaCharEngy >> 16) & 0xFF);
  1263. *(SendBuffer + 0x43) = ((BMS_TotalStaCharEngy >> 8) & 0xFF);
  1264. *(SendBuffer + 0x44) = (BMS_TotalStaCharEngy & 0xFF);
  1265. *(SendBuffer + 0x45) = ((BBox_TotalGunCharCapy >> 24) & 0xFF); // uint32 累计插枪充电充入Ah 数 精度_0.1,偏移量_0,单位_Ah
  1266. *(SendBuffer + 0x46) = ((BBox_TotalGunCharCapy >> 16) & 0xFF);
  1267. *(SendBuffer + 0x47) = ((BBox_TotalGunCharCapy >> 8) & 0xFF);
  1268. *(SendBuffer + 0x48) = (BBox_TotalGunCharCapy & 0xFF);
  1269. *(SendBuffer + 0x49) = ((BMS_TotalGunCharEngy >> 24) & 0xFF); // uint32 累计插枪充电充入kWh 数 精度_0.1,偏移量_0,单位_KWh
  1270. *(SendBuffer + 0x4A) = ((BMS_TotalGunCharEngy >> 16) & 0xFF);
  1271. *(SendBuffer + 0x4B) = ((BMS_TotalGunCharEngy >> 8) & 0xFF);
  1272. *(SendBuffer + 0x4C) = (BMS_TotalGunCharEngy & 0xFF);
  1273. *(SendBuffer + 0x4D) = bcc_chk(SendBuffer, 0x4D);
  1274. *PtrSendAddr = (uint32)SendBuffer;
  1275. break;
  1276. }
  1277. case 0x95:
  1278. {
  1279. *SendLen = 0x30;
  1280. SendBuffer = pvPortMalloc(*SendLen);
  1281. *(SendBuffer + 0) = TCP_START_SYM1;
  1282. *(SendBuffer + 1) = TCP_START_SYM2;
  1283. *(SendBuffer + 2) = TCP_CMD_SYM;
  1284. *(SendBuffer + 3) = TCP_ANS_SYM;
  1285. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1286. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1287. uint16 DataLen = 0x17;
  1288. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1289. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1290. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1291. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1292. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1293. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1294. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1295. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1296. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8
  1297. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1298. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1299. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1300. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1301. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1302. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1303. // test data
  1304. // BMS_BattVolt = 5678;
  1305. // BMS_PackCurr = 35+10000;
  1306. // BMS_MaxCellVolt = 4235;
  1307. // BMS_MinCellVolt = 4123;
  1308. // BMS_MaxCellTemp = 25+50;
  1309. // BMS_MinCellTemp = 20+50;
  1310. *(SendBuffer + 0x25) = (BMS_BattVolt >> 8) & 0xFF; // 电池包总电压
  1311. *(SendBuffer + 0x26) = (BMS_BattVolt & 0xFF);
  1312. *(SendBuffer + 0x27) = ((BMS_PackCurr >> 8) & 0xFF); // uint16 电池包总电流,充电为负值,放电为正值 精度_0.1,偏移量_-1000,单位_A
  1313. *(SendBuffer + 0x28) = (BMS_PackCurr & 0xFF);
  1314. *(SendBuffer + 0x29) = ((BMS_MaxCellVolt >> 8) & 0xFF); // uint16 电池最高单体电压,1mV
  1315. *(SendBuffer + 0x2A) = (BMS_MaxCellVolt & 0xFF);
  1316. *(SendBuffer + 0x2B) = ((BMS_MinCellVolt >> 8) & 0xFF); // uint16 电池最高单体电压,1mV
  1317. *(SendBuffer + 0x2C) = (BMS_MinCellVolt & 0xFF);
  1318. *(SendBuffer + 0x2D) = ((BMS_MaxCellTemp-10) & 0xFF); // uint16 电池最高单体电压,1mV
  1319. *(SendBuffer + 0x2E) = ((BMS_MinCellTemp-10) & 0xFF);
  1320. *(SendBuffer + 0x2F) = bcc_chk(SendBuffer, 0x2F);
  1321. *PtrSendAddr = (uint32)SendBuffer;
  1322. break;
  1323. }
  1324. case 0x96:
  1325. {
  1326. *SendLen = 0x4A;
  1327. SendBuffer = pvPortMalloc(*SendLen);
  1328. *(SendBuffer + 0) = TCP_START_SYM1;
  1329. *(SendBuffer + 1) = TCP_START_SYM2;
  1330. *(SendBuffer + 2) = TCP_CMD_SYM;
  1331. *(SendBuffer + 3) = TCP_ANS_SYM;
  1332. memcpy(SendBuffer + 4, TcpbattSN, BATT_SN_LEN);
  1333. *(SendBuffer + 0x15) = (TCP_ENCPT_DISABLE & 0xFF); // uint8 TCP_ENCPT_DISABLE
  1334. uint16 DataLen = 0x31;
  1335. *(SendBuffer + 0x16) = ((DataLen >> 8) & 0xFF); // uint16 DataLen
  1336. *(SendBuffer + 0x17) = (DataLen & 0xFF);
  1337. *(SendBuffer + 0x18) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1338. *(SendBuffer + 0x19) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1339. *(SendBuffer + 0x1A) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1340. *(SendBuffer + 0x1B) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1341. *(SendBuffer + 0x1C) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1342. *(SendBuffer + 0x1D) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1343. *(SendBuffer + 0x1E) = (DataIdx & 0xFF); // uint8
  1344. *(SendBuffer + 0x1F) = ((UTC8TimeTcp.year) & 0xFF); // uint8 year
  1345. *(SendBuffer + 0x20) = (UTC8TimeTcp.month & 0xFF); // uint8 month
  1346. *(SendBuffer + 0x21) = (UTC8TimeTcp.day & 0xFF); // uint8 day
  1347. *(SendBuffer + 0x22) = (UTC8TimeTcp.hour & 0xFF); // uint8 hour
  1348. *(SendBuffer + 0x23) = (UTC8TimeTcp.minute & 0xFF); // uint8 minute
  1349. *(SendBuffer + 0x24) = (UTC8TimeTcp.second & 0xFF); // uint8 second
  1350. // test data
  1351. // BMS_PackSOC = 20/0.4;
  1352. // BMS_PackSOH = 95/0.4;
  1353. // BBox_PackSOC = 91/0.4;
  1354. // BBox_PackSOH = 96;
  1355. // BBox_TotalDischarCapy = 12345;
  1356. // BBox_TotalCharCapy = 23456;
  1357. // BBox_TotalBackCharCapy = 34567;
  1358. // BBox_TotalDischarEngy = 45678;
  1359. // BBox_TotalCharEngy = 56789;
  1360. // BBox_TotalBackCharEngy= 67890;
  1361. // BBox_Dq_Dv_MAX = 123456;
  1362. // BBox_Dq_Dv_MIN = 111112;
  1363. *(SendBuffer + 0x25) = (BMS_PackSOC) & 0xFF; // BMS计算SOC
  1364. *(SendBuffer + 0x26) = (BMS_PackSOH / 25 * 10) & 0xFF; // SOH 精度1%
  1365. *(SendBuffer + 0x27) = (BBox_PackSOC & 0xFF); // 终端计算SOC
  1366. *(SendBuffer + 0x28) = (BBox_PackSOH & 0xFF);
  1367. *(SendBuffer + 0x29) = ((BBox_TotalDischarCapy >> 24) & 0xFF); // uint32 BBox计算累计放电安时数 Ah 精度 0.1Ah
  1368. *(SendBuffer + 0x2A) = ((BBox_TotalDischarCapy >> 16) & 0xFF);
  1369. *(SendBuffer + 0x2B) = ((BBox_TotalDischarCapy >> 8) & 0xFF);
  1370. *(SendBuffer + 0x2C) = ((BBox_TotalDischarCapy) & 0xFF);
  1371. *(SendBuffer + 0x2D) = ((BBox_TotalCharCapy >> 24) & 0xFF); // uint32 BBox计算累计充电安时数 Ah 精度 0.1Ah
  1372. *(SendBuffer + 0x2E) = ((BBox_TotalCharCapy >> 16) & 0xFF);
  1373. *(SendBuffer + 0x2F) = ((BBox_TotalCharCapy >> 8) & 0xFF);
  1374. *(SendBuffer + 0x30) = ((BBox_TotalCharCapy) & 0xFF);
  1375. *(SendBuffer + 0x31) = ((BBox_TotalBackCharCapy >> 24) & 0xFF); // uint32 BBox计算累计动能回收安时数 Ah 精度 0.1Ah
  1376. *(SendBuffer + 0x32) = ((BBox_TotalBackCharCapy >> 16) & 0xFF);
  1377. *(SendBuffer + 0x33) = ((BBox_TotalBackCharCapy >> 8) & 0xFF);
  1378. *(SendBuffer + 0x34) = ((BBox_TotalBackCharCapy) & 0xFF);
  1379. *(SendBuffer + 0x35) = ((BBox_TotalDischarEngy >> 24) & 0xFF); // uint32 BBox计算累计放电能量 Kwh
  1380. *(SendBuffer + 0x36) = ((BBox_TotalDischarEngy >> 16) & 0xFF);
  1381. *(SendBuffer + 0x37) = ((BBox_TotalDischarEngy >> 8) & 0xFF);
  1382. *(SendBuffer + 0x38) = ((BBox_TotalDischarEngy) & 0xFF);
  1383. *(SendBuffer + 0x39) = ((BBox_TotalCharEngy >> 24) & 0xFF); // uint32 BBox计算累计充电能量 Kwh
  1384. *(SendBuffer + 0x3A) = ((BBox_TotalCharEngy >> 16) & 0xFF);
  1385. *(SendBuffer + 0x3B) = ((BBox_TotalCharEngy >> 8) & 0xFF);
  1386. *(SendBuffer + 0x3C) = ((BBox_TotalCharEngy) & 0xFF);
  1387. *(SendBuffer + 0x3D) = ((BBox_TotalBackCharEngy >> 24) & 0xFF); // uint32 BBox计算累计动能回收能量 Kwh
  1388. *(SendBuffer + 0x3E) = ((BBox_TotalBackCharEngy >> 16) & 0xFF);
  1389. *(SendBuffer + 0x3F) = ((BBox_TotalBackCharEngy >> 8) & 0xFF);
  1390. *(SendBuffer + 0x40) = ((BBox_TotalBackCharEngy) & 0xFF);
  1391. *(SendBuffer + 0x41) = ((BBox_Dq_Dv_MAX >> 24) & 0xFF); // uint32 BBox计算Dq/Dv_MAX
  1392. *(SendBuffer + 0x42) = ((BBox_Dq_Dv_MAX >> 16) & 0xFF);
  1393. *(SendBuffer + 0x43) = ((BBox_Dq_Dv_MAX >> 8) & 0xFF);
  1394. *(SendBuffer + 0x44) = ((BBox_Dq_Dv_MAX) & 0xFF);
  1395. *(SendBuffer + 0x45) = ((BBox_Dq_Dv_MIN >> 24) & 0xFF); // uint32 BBox计算Dq/Dv_MAX
  1396. *(SendBuffer + 0x46) = ((BBox_Dq_Dv_MIN >> 16) & 0xFF);
  1397. *(SendBuffer + 0x47) = ((BBox_Dq_Dv_MIN >> 8) & 0xFF);
  1398. *(SendBuffer + 0x48) = ((BBox_Dq_Dv_MIN) & 0xFF);
  1399. *(SendBuffer + 0x49) = bcc_chk(SendBuffer, 0x49);
  1400. *PtrSendAddr = (uint32)SendBuffer;
  1401. break;
  1402. }
  1403. default:
  1404. break;
  1405. }
  1406. }
  1407. void TimeUpdate(UTC8TimeType *UTC8TimeTcpPtr)
  1408. {
  1409. int year = 0, month = 0, day = 0, hour = 0,minute = 0,second = 0;
  1410. int lastday = 0;
  1411. year = UTC8TimeTcpPtr->year + 2000;
  1412. month = UTC8TimeTcpPtr->month;
  1413. day = UTC8TimeTcpPtr->day;
  1414. hour = UTC8TimeTcpPtr->hour;
  1415. minute = UTC8TimeTcpPtr->minute;
  1416. second = UTC8TimeTcpPtr->second;
  1417. second+=1;
  1418. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  1419. {
  1420. lastday = 31;
  1421. }
  1422. else if (month == 4 || month == 6 || month == 9 || month == 11)
  1423. {
  1424. lastday = 30;
  1425. }
  1426. else
  1427. {
  1428. if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
  1429. lastday = 29;
  1430. else
  1431. lastday = 28;
  1432. }
  1433. if(second >=60)
  1434. {
  1435. second -=60;
  1436. minute +=1;
  1437. if(minute >=60)
  1438. {
  1439. minute -=60;
  1440. hour +=1;
  1441. }
  1442. }
  1443. if (hour >= 24)
  1444. {
  1445. hour -= 24;
  1446. day += 1;
  1447. if (day > lastday)
  1448. {
  1449. day -= lastday;
  1450. month += 1;
  1451. if (month > 12)
  1452. {
  1453. month -= 12;
  1454. year += 1;
  1455. }
  1456. }
  1457. }
  1458. UTC8TimeTcpPtr->year = year - 2000;
  1459. UTC8TimeTcpPtr->month = month;
  1460. UTC8TimeTcpPtr->day = day;
  1461. UTC8TimeTcpPtr->hour = hour;
  1462. UTC8TimeTcpPtr->minute = minute;
  1463. UTC8TimeTcpPtr->second = second;
  1464. }
  1465. void GetUtc8Time(UTC8TimeType *UTC8TimeTcpPtr)
  1466. {
  1467. char *AtCmdAsk = (char *)("AT+CCLK?\r\n");
  1468. uint8 AtCmdLen = mstrlen(AtCmdAsk);
  1469. uint16 ReadLen = 0;
  1470. uint8 *retptr = NULL;
  1471. uint8 UartRecvPtr[128];
  1472. uint8 ErrCnt = 0;
  1473. while(ErrCnt<3)
  1474. {
  1475. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)AtCmdAsk, AtCmdLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(100));
  1476. if (ReadLen ==39 )
  1477. {
  1478. if ((uint8 *)strstr((char *)UartRecvPtr, (char *)("OK")))
  1479. {
  1480. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("+CCLK:"));
  1481. if (retptr)
  1482. {
  1483. UTC8TimeTcpPtr->year = CharToHex(*(retptr + 8)) * 10 + CharToHex(*(retptr + 9));
  1484. UTC8TimeTcpPtr->month = CharToHex(*(retptr + 11)) * 10 + CharToHex(*(retptr + 12));
  1485. UTC8TimeTcpPtr->day = CharToHex(*(retptr + 14)) * 10 + CharToHex(*(retptr + 15));
  1486. UTC8TimeTcpPtr->hour = CharToHex(*(retptr + 17)) * 10 + CharToHex(*(retptr + 18));
  1487. UTC8TimeTcpPtr->minute = CharToHex(*(retptr + 20)) * 10 + CharToHex(*(retptr + 21));
  1488. UTC8TimeTcpPtr->second = CharToHex(*(retptr + 23)) * 10 + CharToHex(*(retptr + 24));
  1489. break;
  1490. }
  1491. }
  1492. }
  1493. else
  1494. {
  1495. ErrCnt++;
  1496. }
  1497. }
  1498. }
  1499. static void GetCSQValue(uint8 *out)
  1500. {
  1501. char *AtCmdAsk = (char *)("AT+CSQ\r\n");
  1502. uint8 AtCmdLen = mstrlen(AtCmdAsk);
  1503. uint16 ReadLen = 0;
  1504. uint8 *retptr = NULL;
  1505. uint8 UartRecvPtr[30];
  1506. static uint8 CSQValue = 0;
  1507. UART_Query_Data(UART_LPUART1, UART_LPUART1, (uint8 *)AtCmdAsk, AtCmdLen, UartRecvPtr, &ReadLen, pdMS_TO_TICKS(100));
  1508. if (ReadLen > 0)
  1509. {
  1510. if ((uint8 *)strstr((char *)UartRecvPtr, (char *)("OK")))
  1511. {
  1512. retptr = (uint8 *)strstr((char *)UartRecvPtr, (char *)("+CSQ:"));
  1513. char CsqStr[5];
  1514. for (uint8 i = 0; i < 5; i++)
  1515. {
  1516. if (*(retptr + i + 6) == ',')
  1517. {
  1518. break;
  1519. }
  1520. memcpy((CsqStr + i), (retptr + i + 6), 1);
  1521. }
  1522. CSQValue = atoi(CsqStr);
  1523. }
  1524. }
  1525. *out = CSQValue;
  1526. return;
  1527. }
  1528. static void TcpDataInfoRecvHandle(uint8 *DataRecv, uint16 DataRecvLen)
  1529. {
  1530. uint8 Tcp_Cmd;
  1531. uint8 *Ptr = NULL, *retptr = NULL;
  1532. uint8 TcpCmdAnswer[50] = {0};
  1533. uint16 TcpDataLen = 0;
  1534. uint16 NumCalTemp = 1;
  1535. retptr = (uint8 *)strstr((char *)DataRecv, (char *)("\r\n##"));
  1536. if (retptr == NULL || DataRecvLen < 5)
  1537. return;
  1538. for (uint8 i = 0; i < 5; i++)
  1539. {
  1540. if (*(retptr - i - 1) == 'D')
  1541. {
  1542. break;
  1543. }
  1544. TcpDataLen = TcpDataLen + CharToHex(*(retptr - i - 1)) * NumCalTemp;
  1545. NumCalTemp = NumCalTemp * 10;
  1546. }
  1547. if (TcpDataLen > 0)
  1548. {
  1549. Ptr = retptr + 2;
  1550. if ((*(Ptr + 0) == TCP_START_SYM1) && (*(Ptr + 1) == TCP_START_SYM2)) // 服务器起始信息
  1551. {
  1552. Tcp_Cmd = *(Ptr + 2); // 命令标志
  1553. if (*(Ptr + 3) == 0xFE)
  1554. {
  1555. TcpWorkState = 0x01; // 需要暂停发送,进行命令应答的标志
  1556. }
  1557. else
  1558. {
  1559. return;
  1560. }
  1561. switch (Tcp_Cmd)
  1562. {
  1563. case TCP_QUERY_SYM:
  1564. break;
  1565. case TCP_SETCMD_SYM:
  1566. break;
  1567. case TCP_CONCMD_SYM:
  1568. {
  1569. TcpCmdAnswer[0] = TCP_START_SYM1;
  1570. TcpCmdAnswer[1] = TCP_START_SYM1;
  1571. TcpCmdAnswer[2] = TCP_CONCMD_SYM;
  1572. if (*(Ptr + 30) == 0x83) // 远程升级指令
  1573. {
  1574. TcpCmdAnswer[3] = 0x01;
  1575. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1576. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1577. TcpCmdAnswer[22] = 0x00;
  1578. TcpCmdAnswer[23] = 0x06;
  1579. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1580. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1581. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1582. Fota_Process_Going = true;
  1583. vTaskDelay(pdMS_TO_TICKS(5000));
  1584. Fota_Ftp(Ptr + 32);
  1585. Fota_Process_Going = false;
  1586. }
  1587. else if (*(Ptr + 30) == 0x80) // 远程锁定命令
  1588. {
  1589. TcpCmdAnswer[3] = 0x01;
  1590. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1591. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1592. TcpCmdAnswer[22] = 0x00;
  1593. TcpCmdAnswer[23] = 0x06;
  1594. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1595. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1596. if (*(Ptr + 31) == 0x01) // 0x01代表锁定
  1597. {
  1598. // battSeparateEnable = 1;
  1599. // battSeparateCtlState = 1;
  1600. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1601. }
  1602. else if (*(Ptr + 31) == 0x02) // 0x02代表解锁
  1603. {
  1604. // battSeparateEnable = 1;
  1605. // battSeparateCtlState = 0;
  1606. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1607. }
  1608. }
  1609. else
  1610. {
  1611. TcpCmdAnswer[3] = 0x0f;
  1612. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  1613. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  1614. TcpCmdAnswer[22] = 0x00;
  1615. TcpCmdAnswer[23] = 0x06;
  1616. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  1617. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  1618. tcpipConnectionSend(SocketId, TcpCmdAnswer, 31);
  1619. }
  1620. break;
  1621. }
  1622. case TCP_UDSCMD_SYM:
  1623. {
  1624. TcpCmdAnswer[0] = TCP_START_SYM1;
  1625. TcpCmdAnswer[1] = TCP_START_SYM1;
  1626. TcpCmdAnswer[2] = TCP_UDSCMD_SYM;
  1627. uint16 sendLen = tcpUdsFunc(Ptr, TcpCmdAnswer);
  1628. tcpipConnectionSend(SocketId, TcpCmdAnswer, sendLen);
  1629. break;
  1630. }
  1631. default:
  1632. {
  1633. break;
  1634. }
  1635. }
  1636. }
  1637. }
  1638. }
  1639. static uint8 tcpUdsAnsMsgEncode(bool posOrNeg,uint8 udsType_SID, uint16 udsType_DID, uint8 negCode,uint8 *AnsPtr)
  1640. {
  1641. uint8 length = 0;
  1642. if(posOrNeg == false) //neg ans
  1643. {
  1644. length = 5;
  1645. *(AnsPtr + 0) = 0x7F;
  1646. *(AnsPtr + 1) = udsType_SID;
  1647. *(AnsPtr + 2) = (udsType_DID>>8)&0xFF;
  1648. *(AnsPtr + 3) = udsType_DID&0xFF;
  1649. *(AnsPtr + 4) = negCode;
  1650. }
  1651. else
  1652. {
  1653. length = 3;
  1654. *(AnsPtr + 0) = (udsType_SID + 0x40)&0xFF;
  1655. *(AnsPtr + 1) = (udsType_DID>>8)&0xFF;
  1656. *(AnsPtr + 2) = udsType_DID&0xFF;
  1657. }
  1658. return length;
  1659. }
  1660. uint16 tcpUdsFunc(uint8 *Ptr, uint8 *AnsPtr)
  1661. {
  1662. uint16 AnsLength = 0;
  1663. uint8 contrlType = 0;
  1664. uint16 udsDataLen = 0;
  1665. uint16 DIDBuffer = 0;
  1666. uint8 udsType_SID = 0;
  1667. uint8 udsAnsBuffer[10];
  1668. uint8 retLength = 0;
  1669. uint8 DataBuffer[10];
  1670. contrlType = *(Ptr + 30);
  1671. udsDataLen = *(Ptr + 31) - 3;
  1672. udsType_SID = *(Ptr + 32);
  1673. DIDBuffer = *(Ptr + 33) << 8 | *(Ptr + 34);
  1674. memcpy(DataBuffer, Ptr + 35, udsDataLen);
  1675. switch (contrlType)
  1676. {
  1677. case 0x00:
  1678. {
  1679. switch (udsType_SID)
  1680. {
  1681. case 0x2E:
  1682. {
  1683. switch (DIDBuffer)
  1684. {
  1685. case 0x0201: //soc correction did
  1686. {
  1687. if(udsDataLen == 2)
  1688. {
  1689. float socCorrectionValue = (float)((float)((DataBuffer[0]<<8 | DataBuffer[1])&0xFFFF)*0.1-100);
  1690. if(socCorrectionValue != 0 && socCorrectionValue<100 && socCorrectionValue>-100) //有效修正
  1691. {
  1692. BBox_SocCorrectionValue = socCorrectionValue;
  1693. socCorrectionValue = 0;
  1694. retLength = tcpUdsAnsMsgEncode(true,udsType_SID,DIDBuffer,0x00,udsAnsBuffer);
  1695. }
  1696. else
  1697. {
  1698. socCorrectionValue = 0;
  1699. retLength = tcpUdsAnsMsgEncode(false,udsType_SID,DIDBuffer,0x13,udsAnsBuffer);
  1700. }
  1701. }
  1702. else
  1703. {
  1704. retLength = tcpUdsAnsMsgEncode(false,udsType_SID,DIDBuffer,0x13,udsAnsBuffer);
  1705. }
  1706. *(AnsPtr + 3) = 0x01;
  1707. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1708. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1709. *(AnsPtr + 22) = 0x00;
  1710. *(AnsPtr + 23) = 0x06 + retLength + 1;
  1711. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1712. *(AnsPtr + 30) = retLength;
  1713. memcpy((AnsPtr + 31),udsAnsBuffer,retLength);
  1714. *(AnsPtr + 31 + retLength) = bcc_chk(AnsPtr, 31 + retLength);
  1715. AnsLength = 32 + retLength;
  1716. return AnsLength;
  1717. break;
  1718. }
  1719. default:
  1720. retLength = tcpUdsAnsMsgEncode(false,udsType_SID,DIDBuffer,0x31,udsAnsBuffer);
  1721. *(AnsPtr + 3) = 0x01;
  1722. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1723. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1724. *(AnsPtr + 22) = 0x00;
  1725. *(AnsPtr + 23) = 0x06 + retLength + 1;
  1726. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1727. *(AnsPtr + 30) = retLength;
  1728. memcpy((AnsPtr + 31),udsAnsBuffer,retLength);
  1729. *(AnsPtr + 31 + retLength) = bcc_chk(AnsPtr, 31 + retLength);
  1730. AnsLength = 32 + retLength;
  1731. return AnsLength;
  1732. break;
  1733. }
  1734. }
  1735. default:
  1736. break;
  1737. }
  1738. break;
  1739. }
  1740. case 0x01:
  1741. {
  1742. switch (udsType_SID)
  1743. {
  1744. case 0x2E:
  1745. {
  1746. switch (DIDBuffer)
  1747. {
  1748. case 0x01:
  1749. {
  1750. // battSeparateEnable = 1;
  1751. if (DataBuffer[0] == 0x01)
  1752. {
  1753. // battSeparateCtlState = 1;
  1754. }
  1755. else if (DataBuffer[0] == 0x00)
  1756. {
  1757. // battSeparateCtlState = 0;
  1758. }
  1759. *(AnsPtr + 3) = 0x0f;
  1760. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1761. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1762. *(AnsPtr + 22) = 0x00;
  1763. *(AnsPtr + 23) = 0x06;
  1764. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1765. *(AnsPtr + 30) = contrlType;
  1766. *(AnsPtr + 31) = udsDataLen;
  1767. *(AnsPtr + 32) = udsType_SID;
  1768. *(AnsPtr + 33) = DIDBuffer >> 8;
  1769. *(AnsPtr + 34) = DIDBuffer;
  1770. *(AnsPtr + 36) = DataBuffer[0];
  1771. *(AnsPtr + 37) = bcc_chk(AnsPtr, 37);
  1772. return AnsLength;
  1773. break;
  1774. }
  1775. default:
  1776. return AnsLength;
  1777. break;
  1778. }
  1779. break;
  1780. }
  1781. case 0x22:
  1782. {
  1783. break;
  1784. }
  1785. default:
  1786. break;
  1787. }
  1788. break;
  1789. }
  1790. default:
  1791. break;
  1792. }
  1793. *(AnsPtr + 3) = 0x0f;
  1794. memcpy((AnsPtr + 4), (Ptr + 4), BATT_SN_LEN);
  1795. *(AnsPtr + 21) = TCP_ENCPT_DISABLE;
  1796. *(AnsPtr + 22) = 0x00;
  1797. *(AnsPtr + 23) = 0x06;
  1798. memcpy((AnsPtr + 24), (Ptr + 24), 6);
  1799. *(AnsPtr + 30) = contrlType;
  1800. *(AnsPtr + 31) = udsDataLen;
  1801. *(AnsPtr + 32) = 0x7F;
  1802. *(AnsPtr + 33) = DIDBuffer >> 8;
  1803. *(AnsPtr + 34) = DIDBuffer;
  1804. *(AnsPtr + 36) = 0x31;
  1805. *(AnsPtr + 37) = bcc_chk(AnsPtr, 37);
  1806. return AnsLength;
  1807. }
  1808. void Fota_Ftp(uint8 *dataPtrIn)
  1809. {
  1810. static uint8 ftp_process = 0;
  1811. char *ATCmdSend = NULL;
  1812. sint8 recvRet = -1;
  1813. char ATSendDataBuffer[100] = {0};
  1814. uint8 UartRecvBuffer[50];
  1815. uint16 ReadLen = 0;
  1816. uint8 *retptr = NULL;
  1817. // 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,";
  1818. /*URL信息解析*/
  1819. char *databuffer[20];
  1820. char *accountPtr;
  1821. char *passwordPtr;
  1822. char *ftpServerIpPtr;
  1823. char *ftpServerPort;
  1824. char *filePathPtr;
  1825. char *filenamePtr;
  1826. char keyFilename[20] = {0};
  1827. char *p = NULL;
  1828. p = strtok((char *)dataPtrIn, ",");
  1829. uint8 index = 0;
  1830. boolean ftp_EndFlg = false;
  1831. while (p && index < 20)
  1832. {
  1833. databuffer[index] = p;
  1834. p = strtok(NULL, ",");
  1835. index++;
  1836. }
  1837. accountPtr = databuffer[0];
  1838. passwordPtr = databuffer[1];
  1839. ftpServerIpPtr = databuffer[2];
  1840. ftpServerPort = databuffer[3];
  1841. filePathPtr = databuffer[7];
  1842. for (sint8 i = strlen(filePathPtr); i--; i < 0)
  1843. {
  1844. if (*(filePathPtr + i) == '/')
  1845. {
  1846. *(filePathPtr + i) = 0;
  1847. filenamePtr = filePathPtr + i + 1;
  1848. break;
  1849. }
  1850. }
  1851. memcpy(keyFilename, filenamePtr, mstrlen(filenamePtr) - 3);
  1852. memcpy(&keyFilename[mstrlen(filenamePtr) - 3], "zl", strlen("zl"));
  1853. while (!ftp_EndFlg)
  1854. {
  1855. vTaskDelay(pdMS_TO_TICKS(100));
  1856. switch (ftp_process)
  1857. {
  1858. case 0: // start
  1859. {
  1860. ATCmdSend = (char *)("AT+CFTPSSTART\r\n");
  1861. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1862. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSSTART: 0"), 2000);
  1863. if (recvRet == 0)
  1864. {
  1865. ftp_process++;
  1866. }
  1867. else
  1868. {
  1869. DeviceErrNum = 10;
  1870. ftp_EndFlg = 1;
  1871. }
  1872. break;
  1873. }
  1874. case 1: // login
  1875. {
  1876. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1877. sprintf(ATSendDataBuffer, "AT+CFTPSLOGIN=\"%s\",%s,\"%s\",\"%s\",0\r\n", ftpServerIpPtr, ftpServerPort, accountPtr, passwordPtr);
  1878. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1879. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSLOGIN: 0"), 30000);
  1880. if (recvRet == 0)
  1881. {
  1882. ftp_process++;
  1883. }
  1884. else
  1885. {
  1886. DeviceErrNum = 11;
  1887. ftp_EndFlg = 1;
  1888. }
  1889. break;
  1890. }
  1891. case 2: // transmit bin file from server to module
  1892. {
  1893. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1894. sprintf(ATSendDataBuffer, "AT+CFTPSGETFILE=\"%s/%s\"\r\n", filePathPtr, filenamePtr);
  1895. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1896. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSGETFILE: 0"), 10000);
  1897. if (recvRet == 0)
  1898. {
  1899. ftp_process++;
  1900. }
  1901. else
  1902. {
  1903. DeviceErrNum = 12;
  1904. ftp_EndFlg = 1;
  1905. }
  1906. break;
  1907. }
  1908. case 3: // transmit zl file from server to module
  1909. {
  1910. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1911. sprintf(ATSendDataBuffer, "AT+CFTPSGETFILE=\"%s/%s\"\r\n", filePathPtr, keyFilename);
  1912. UART_Send_Data(UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), pdMS_TO_TICKS(100));
  1913. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSGETFILE: 0"), 10000);
  1914. if (recvRet == 0)
  1915. {
  1916. ftp_process++;
  1917. }
  1918. else
  1919. {
  1920. DeviceErrNum = 13;
  1921. ftp_EndFlg = 1;
  1922. }
  1923. break;
  1924. }
  1925. case 4: // logout
  1926. {
  1927. ATCmdSend = (char *)("AT+CFTPSLOGOUT\r\n");
  1928. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1929. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("+CFTPSLOGOUT: 0"), 2000);
  1930. if (recvRet == 0)
  1931. {
  1932. ftp_process++;
  1933. }
  1934. else
  1935. {
  1936. ftp_process++;
  1937. }
  1938. break;
  1939. }
  1940. case 5: // stop
  1941. {
  1942. ATCmdSend = (char *)("AT+CFTPSSTOP\r\n");
  1943. UART_Send_Data(UART_LPUART1, ATCmdSend, mstrlen(ATCmdSend), pdMS_TO_TICKS(100));
  1944. recvRet = AtcmdDelayRecvFunc(UART_LPUART1, (char *)("CFTPSSTOP: 0"), 2000);
  1945. if (recvRet == 0)
  1946. {
  1947. ftp_process++;
  1948. }
  1949. else
  1950. {
  1951. ftp_process++;
  1952. }
  1953. break;
  1954. }
  1955. case 6: // get data from module
  1956. {
  1957. char findDataBuffer[25] = {0};
  1958. uint32 currentAddr = 0;
  1959. uint16 readLenAsk = 256;
  1960. uint16 FotaRecvDataLen_8 = 0;
  1961. uint32 fileLen = 65535;
  1962. uint32 FlashAddStart = 0;
  1963. uint32 appReceviedCRC;
  1964. uint16 getDataLenErrCount = 0;
  1965. uint8 UartData[512] = {0};
  1966. uint8 FlashData[512] = {0};
  1967. uint8 *fileDataPtr = NULL;
  1968. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1969. sprintf(ATSendDataBuffer, "AT+CFTRANTX=\"c:/%s\"\r\n", keyFilename);
  1970. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartRecvBuffer, &ReadLen, pdMS_TO_TICKS(5000));
  1971. if (ReadLen == (4 + 45))
  1972. {
  1973. retptr = (uint8 *)strstr((char *)UartRecvBuffer, (char *)("+CFTRANTX: DATA"));
  1974. if (retptr)
  1975. {
  1976. fileLen = (*(retptr + 21) << 24) | (*(retptr + 22) << 16) | (*(retptr + 23) << 8) | (*(retptr + 24) << 0);
  1977. appReceviedCRC = (*(retptr + 19) << 8) | (*(retptr + 20) << 0);
  1978. Hal_FlsGetAppVectorTableStartAddr(&FlashAddStart);
  1979. Hal_OTAFlashAppInfoInit();
  1980. Hal_FlsErase(0, FlashAddStart + fileLen + FLS_INTERNAL_WRITE_SIZE, 100);
  1981. Hal_SetAppInfo(fileLen, appReceviedCRC, CONTROLLER_SELF);
  1982. while (readLenAsk != 0)
  1983. {
  1984. memset(UartData, 0x00, 512);
  1985. memset(FlashData, 0x00, 512);
  1986. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  1987. sprintf(ATSendDataBuffer, "AT+CFTRANTX=\"c:/%s\",%d,%d\r\n", filenamePtr, currentAddr, readLenAsk);
  1988. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartData, &ReadLen, pdMS_TO_TICKS(5000));
  1989. sprintf(findDataBuffer, "+CFTRANTX: DATA,%d", readLenAsk);
  1990. if (currentAddr % 0x100 == 0)
  1991. {
  1992. Dio_FlipChannel(LED_INDEX3);
  1993. }
  1994. if (ReadLen == (readLenAsk + strlen(findDataBuffer) + 26))
  1995. {
  1996. retptr = (uint8 *)strstr((char *)UartData, findDataBuffer);
  1997. if (retptr)
  1998. {
  1999. uint16 uartcrc = 0x00;
  2000. uint16 flashcrc = 0xff;
  2001. fileDataPtr = retptr + 2 + mstrlen(findDataBuffer);
  2002. FotaRecvDataLen_8 = ((readLenAsk + 7) / 8) * 8;
  2003. Hal_FlsWrite(FlashAddStart + currentAddr, fileDataPtr, FotaRecvDataLen_8, 100);
  2004. Hal_FlsRead(FlashAddStart + currentAddr, FlashData, FotaRecvDataLen_8, 100);
  2005. uartcrc = CRC16_Modbus(fileDataPtr, FotaRecvDataLen_8);
  2006. flashcrc = CRC16_Modbus(FlashData, FotaRecvDataLen_8);
  2007. if (flashcrc == uartcrc)
  2008. {
  2009. currentAddr = currentAddr + readLenAsk;
  2010. getDataLenErrCount = 0;
  2011. }
  2012. else
  2013. {
  2014. uint32 secflash = (FlashAddStart + currentAddr) & 0xFFFFF000;
  2015. if (secflash > 0)
  2016. {
  2017. currentAddr = secflash - FlashAddStart;
  2018. }
  2019. else
  2020. {
  2021. currentAddr = 0;
  2022. }
  2023. MemIf_JobResultType ret = Hal_FlsErase(secflash, 4096, 100);
  2024. getDataLenErrCount++;
  2025. }
  2026. readLenAsk = min(fileLen - currentAddr, readLenAsk);
  2027. }
  2028. else
  2029. {
  2030. getDataLenErrCount++;
  2031. }
  2032. }
  2033. else
  2034. {
  2035. getDataLenErrCount++;
  2036. }
  2037. if (getDataLenErrCount >= 100)
  2038. {
  2039. break;
  2040. }
  2041. }
  2042. Dio_WriteChannel(LED_INDEX3, STD_ON); // switch off the led
  2043. if (getDataLenErrCount < 50 && Hal_FlsCheckIsTransferSucceed() == TRUE)
  2044. {
  2045. ftp_process++;
  2046. Fota_update_flag = 1;
  2047. }
  2048. else
  2049. {
  2050. ftp_process++;
  2051. DeviceErrNum = 15;
  2052. }
  2053. }
  2054. }
  2055. else
  2056. {
  2057. ftp_process++;
  2058. DeviceErrNum = 14;
  2059. }
  2060. break;
  2061. }
  2062. case 7: // delete the bin and zl files
  2063. {
  2064. memset(ATSendDataBuffer, 0x00, sizeof(ATSendDataBuffer));
  2065. sprintf(ATSendDataBuffer, "AT+FSDEL=*.*\r\n");
  2066. UART_Query_Data(UART_LPUART1, UART_LPUART1, ATSendDataBuffer, mstrlen(ATSendDataBuffer), UartRecvBuffer, &ReadLen, pdMS_TO_TICKS(5000));
  2067. ftp_process++;
  2068. break;
  2069. }
  2070. default:
  2071. ftp_EndFlg = 1;
  2072. break;
  2073. }
  2074. }
  2075. if (Fota_update_flag==0)
  2076. {
  2077. Fota_error_flag = 1;
  2078. }
  2079. }