AppTaskUart1.c 51 KB

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