AppTaskUart1.c 65 KB

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