EC800(1712).c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. //----------------------------说明
  2. // 程序使用hal库,数据传输使用的串口,串口设置为发送+dma,接收+dma+空闲中断的模式
  3. #include "EC800.h"
  4. //-----------------------------变量定义
  5. //---------
  6. ec800Date s_ec800Date = {
  7. .ip = "39.98.37.180",
  8. .port = 32449,
  9. .clientid = "NULL",
  10. .username = "hechun",
  11. .password = "admin123",
  12. // .username = "SZC30010-AE01",
  13. // .password = "qazSZC30010-AE01",
  14. };
  15. messageDate s_messageDate = {0};
  16. // 初始化故障结构体数组
  17. Fault faults[8] = {
  18. {"0", "Out of fence fault"},
  19. {"1", "485 communication abnormality"},
  20. {"2", "MQTT communication failure"},
  21. {"3", "Gyroscope data anomaly"},
  22. {"4", "Car battery malfunction"},
  23. {"5", "4G module initialization failed"},
  24. {"6", "GPS positioning information lost."},
  25. {"7", "Upgrade failure fault"},
  26. };
  27. typedef struct{
  28. uint8_t maxSpeed; // 服务器下发的允许最大车速
  29. uint16_t year; // 上传时间 年
  30. uint8_t month; // 上传时间 月
  31. uint8_t day; // 上传时间 日
  32. uint8_t hour; // 上传时间 时
  33. uint8_t minute; // 上传时间 分
  34. uint8_t sec; // 上传时间 秒
  35. }recDate;
  36. recDate s_recDate = {0};
  37. nmea_msg s_nmea_msg = {0};
  38. nmea_utc_time s_nmea_utc_time = {0};
  39. // AT指令响应超时时间定义
  40. #define REC_TIMEOUT (10000) // 1ms
  41. /**
  42. * @brief 计算字符串的长度
  43. * @param str: 所需计算字符串的指针
  44. * @note
  45. * @retval 无
  46. */
  47. int EC800_calculateStringLength(const char* str) {
  48. int length = 0;
  49. while (str[length] != '\0') {
  50. length++;
  51. }
  52. return length;
  53. }
  54. /**
  55. * @brief 发送指令函数
  56. * @param command: 指令
  57. * @param enterNum: 进入函数次数,只有第一次的时候清除buff
  58. * @note
  59. * @retval 无
  60. */
  61. void EC800M_SendCommand(const char* command)
  62. {
  63. uint32_t stringLen = 0;
  64. stringLen = EC800_calculateStringLength(command);
  65. // 使用DMA
  66. // //等待发送状态OK
  67. // while(HAL_DMA_GetState(&hdma_usart3_tx) == HAL_DMA_STATE_BUSY) osDelay(1);
  68. // //发送数据
  69. // HAL_UART_Transmit_DMA(&huart3, (uint8_t*)command, stringLen);
  70. // 不使用发送dma
  71. HAL_UART_Transmit(&huart4, (uint8_t*)command, stringLen, 1000);
  72. }
  73. /**
  74. * @brief 接收指令回复函数
  75. * @param haystack: 接收的字符串数据
  76. * @param needle: 正确回复的数据
  77. * @note
  78. * @retval 无
  79. */
  80. char* EC800M_RecRespond(char *haystack, const char *needle ){
  81. char* p = NULL;
  82. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  83. // __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
  84. while(g_usart4_rx_sta == 0) osDelay(1);
  85. // __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE); // 关闭空闲中断
  86. g_usart4_rx_sta= 0;
  87. p = strstr(haystack, needle);
  88. memset(haystack, 0, USART4_REC_LEN); // 清除数据buff
  89. HAL_UART_DMAStop(&huart4);
  90. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  91. return p;
  92. }
  93. // 自定义的搜索函数
  94. void* search_sequence(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len) {
  95. const unsigned char* h = (const unsigned char*)haystack;
  96. const unsigned char* n = (const unsigned char*)needle;
  97. // 如果needle为空或haystack长度小于needle长度,则直接返回NULL
  98. if (needle_len == 0 || haystack_len < needle_len) {
  99. return NULL;
  100. }
  101. for (size_t i = 0; i <= haystack_len - needle_len; ++i) {
  102. size_t j;
  103. for (j = 0; j < needle_len; ++j) {
  104. if (h[i + j] != n[j]) {
  105. break; // 如果当前字符不匹配,则跳出内层循环
  106. }
  107. }
  108. if (j == needle_len) {
  109. return (void*)(h + i); // 找到匹配的序列,返回其在haystack中的位置
  110. }
  111. }
  112. // 如果遍历了整个haystack都没有找到匹配的序列,则返回NULL
  113. return NULL;
  114. }
  115. // 接收并比较响应字符串
  116. uint8_t Accept_and_Compare_Str(const char* needle){
  117. uint8_t temp = 0;
  118. static uint16_t timeOutCnt = 0; // 超时计数
  119. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  120. // __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
  121. while(!temp) {
  122. osDelay(1);
  123. if(search_sequence(g_usart4_rx_buf, USART4_REC_LEN, needle, strlen(needle))){
  124. temp = 1;
  125. }
  126. if(search_sequence(g_usart4_rx_buf, USART4_REC_LEN, "ERROR", strlen("ERROR"))){
  127. temp = 2;
  128. }
  129. if(timeOutCnt < REC_TIMEOUT){
  130. timeOutCnt++;
  131. }else{
  132. timeOutCnt = 0;
  133. temp = 3;
  134. }
  135. }
  136. g_usart4_rx_sta = 0;
  137. HAL_UART_DMAStop(&huart4);
  138. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  139. // __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE); // 关闭空闲中断
  140. return temp;
  141. }
  142. /**
  143. * @brief 接收使能函数
  144. * @note
  145. * @retval 无
  146. */
  147. void EC800_recEnable(void){
  148. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  149. __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
  150. while(g_usart4_rx_sta == 0) osDelay(1);
  151. }
  152. /**
  153. * @brief 提取检查信号响应字符串字段中的信号量并进行信号判断
  154. * @param data: 响应的信号量指针
  155. * @note
  156. * @retval 无
  157. */
  158. static uint8_t EC800_extractSignal(char *data){
  159. char *token;
  160. int field_1;
  161. // 使用strtok函数提取字段
  162. token = strtok(data, ":,");
  163. while (token != NULL) {
  164. field_1 = atoi(token); // 转换为整数类型
  165. token = strtok(NULL, ":,");
  166. memset(data, 0, USART4_REC_LEN); // 清除数据buff
  167. g_usart4_rx_sta = 0; // 清除接收状态
  168. if (field_1 < 31) {
  169. return 1;
  170. } else {
  171. return 0;
  172. }
  173. }
  174. return 0;
  175. }
  176. /**
  177. * @brief 获取IMSI号
  178. * @param 无
  179. * @note
  180. * @retval 1: imsi没有获取到 0;imsi获取到
  181. */
  182. uint8_t EC800_getIMSI(void){
  183. char* found = NULL;
  184. char number[16]; // 15 digits + 1 for null terminator
  185. int numberIndex = 0;
  186. int numberFound = 0;
  187. EC800M_SendCommand(AT_CIMI);
  188. EC800_recEnable();
  189. found = strstr(g_usart4_rx_buf, AT_RESP_OK);
  190. if (found != NULL) {
  191. printf("IMSI is get\r\n");
  192. }else{
  193. printf("IMSI is not get\r\n");
  194. return 1;
  195. }
  196. for (int i = 0; i < strlen(g_usart4_rx_buf); i++) {
  197. if (isdigit(g_usart4_rx_buf[i])) {
  198. number[numberIndex] = g_usart4_rx_buf[i];
  199. numberIndex++;
  200. if (numberIndex >= 15) {
  201. numberFound = 1;
  202. break;
  203. }
  204. }
  205. }
  206. number[numberIndex] = '\0';
  207. if (numberFound) {
  208. memcpy(s_messageDate.imsi, number, strlen(number));
  209. printf("IMSI is: %s\n", number);
  210. } else {
  211. printf("Error: Unable to extract the number\n");
  212. }
  213. return 0;
  214. }
  215. /**
  216. * @brief EC800M确认网络并链接MQTT服务器
  217. * @param NONE
  218. * @note
  219. * @retval 无
  220. */
  221. uint8_t linkStep = 0;
  222. void EC800M_link(void){
  223. uint8_t temp = 0;
  224. char command[100] = {0};
  225. char errrCnt = 0; // 错误计数
  226. switch(linkStep){
  227. case 0: // 基础配置
  228. EC800M_SendCommand(AT_CMD_TEST);
  229. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  230. printf("Module status normal\r\n");
  231. }else{
  232. errrCnt++;
  233. printf("Module status abnormal\r\n");
  234. }
  235. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  236. if(errrCnt == 0){
  237. linkStep = 1;
  238. }else{
  239. errrCnt = 0;
  240. }
  241. break;
  242. case 1: // 获取ISMI号
  243. errrCnt = EC800_getIMSI();
  244. if(errrCnt == 0){
  245. linkStep = 2;
  246. }else{
  247. errrCnt = 0;
  248. }
  249. break;
  250. case 2:
  251. EC800M_SendCommand(AT_CMD_ATE0);
  252. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  253. printf("Cancel module echo\r\n");
  254. }else{
  255. errrCnt++;
  256. printf("Failed to cancel module echo\r\n");
  257. }
  258. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  259. if(errrCnt == 0){
  260. linkStep = 3;
  261. }else{
  262. errrCnt = 0;
  263. }
  264. break;
  265. case 3:
  266. EC800M_SendCommand(AT_CMD_CPIN);
  267. if (Accept_and_Compare_Str(AT_RESP_CPIN_READY) == 1) {
  268. printf("SIM card normal\r\n");
  269. }else{
  270. errrCnt++;
  271. printf("SIM card abnormal\r\n");
  272. }
  273. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  274. if(errrCnt == 0){
  275. linkStep = 4;
  276. }else{
  277. errrCnt = 0;
  278. }
  279. break;
  280. case 4:
  281. EC800M_SendCommand(AT_CMD_CSQ);
  282. EC800_recEnable();
  283. temp = EC800_extractSignal(g_usart4_rx_buf);
  284. if (temp) {
  285. printf("signal normal\r\n");
  286. }else{
  287. errrCnt++;
  288. printf("signal abnormal\r\n");
  289. }
  290. if(errrCnt == 0){
  291. linkStep = 5;
  292. }else{
  293. errrCnt = 0;
  294. }
  295. break;
  296. case 5:
  297. EC800M_SendCommand(AT_CMD_CREG);
  298. // EC800_recEnable();
  299. // // 使用字符串处理函数判断是否为正常状态
  300. // if (strstr(g_usart4_rx_buf, "1") != NULL || strstr(g_usart4_rx_buf, "5") != NULL) {
  301. // printf("The module successfully registered on the GSM network\r\n");
  302. // } else {
  303. // errrCnt++;
  304. // printf("The module failed to register on the GSM network\r\n");
  305. // }
  306. if((Accept_and_Compare_Str("+CREG: 0,1") == 1) || (Accept_and_Compare_Str("+CREG: 0,5") == 1)){
  307. printf("The module successfully registered on the GSM network\r\n");
  308. }else{
  309. errrCnt++;
  310. printf("The module failed to register on the GSM network\r\n");
  311. }
  312. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  313. g_usart4_rx_sta = 0; // 清除接收状态
  314. if(errrCnt == 0){
  315. linkStep = 6;
  316. }else{
  317. errrCnt = 0;
  318. }
  319. break;
  320. case 6:
  321. EC800M_SendCommand(AT_CMD_QIDEACT);
  322. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  323. printf("Successfully disabled mobile scene\r\n");
  324. }else{
  325. errrCnt++;
  326. printf("Failed to disable mobile scene\r\n");
  327. }
  328. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  329. if(errrCnt == 0){
  330. linkStep = 7;
  331. }else{
  332. errrCnt = 0;
  333. }
  334. break;
  335. case 7:
  336. EC800M_SendCommand(AT_CMD_QIACT);
  337. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  338. printf("Successfully enabled mobile scene\r\n");
  339. }else{
  340. errrCnt++;
  341. printf("Failed to enable mobile scene\r\n");
  342. }
  343. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  344. if(errrCnt == 0){
  345. linkStep = 8;
  346. }else{
  347. errrCnt = 0;
  348. }
  349. break;
  350. case 8: // 连接mqtt服务器
  351. sprintf(command, "AT+QMTCFG=\"qmtping\",0,%d\r\n", 30);
  352. EC800M_SendCommand(command);
  353. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  354. printf("MQTT heartbeat set successfully\r\n");
  355. }else{
  356. errrCnt++;
  357. printf("Failed to set MQTT heartbeat\r\n");
  358. }
  359. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  360. if(errrCnt == 0){
  361. linkStep = 9;
  362. }else{
  363. errrCnt = 0;
  364. }
  365. break;
  366. case 9:
  367. EC800M_SendCommand(AT_CMD_QMTCFG_SET_DATA_MODE);
  368. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  369. printf("Data receiving mode set successfully.\r\n");
  370. }else{
  371. errrCnt++;
  372. printf("Failed to set data receiving mode\r\n");
  373. }
  374. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  375. if(errrCnt == 0){
  376. linkStep = 10;
  377. }else{
  378. errrCnt = 0;
  379. }
  380. break;
  381. case 10:
  382. sprintf(command,"AT+QMTOPEN=0,\"%s\",%d\r\n", s_ec800Date.ip, s_ec800Date.port);
  383. EC800M_SendCommand(command);
  384. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  385. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTOPEN);
  386. //
  387. // if (found != NULL) {
  388. // printf("Successfully opened a network for the module\r\n");
  389. // }else{
  390. // errrCnt++;
  391. // printf("Failed to open a network for the module\r\n");
  392. // }
  393. temp = Accept_and_Compare_Str(AT_RESP_QMTOPEN);
  394. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  395. if (temp == 1) {
  396. printf("Successfully opened a network for the module\r\n");
  397. }else{
  398. errrCnt++;
  399. printf("Failed to open a network for the module\r\n");
  400. }
  401. if(errrCnt == 0){
  402. linkStep = 11;
  403. }else{
  404. errrCnt = 0;
  405. }
  406. break;
  407. case 11:
  408. sprintf(command,"AT+QMTCONN=0,%s,%s,%s\r\n",s_ec800Date.clientid, s_ec800Date.username, s_ec800Date.password);
  409. EC800M_SendCommand(command);
  410. if (Accept_and_Compare_Str(AT_RESP_QMTCONN) == 1) {
  411. printf("Successfully connected to the MQTT server\r\n");
  412. }else{
  413. errrCnt++;
  414. printf("Failed to connect to the MQTT server\r\n");
  415. }
  416. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  417. if(errrCnt == 0){
  418. linkStep = 12;
  419. }else{
  420. errrCnt = 0;
  421. }
  422. break;
  423. default: break;
  424. }
  425. }
  426. /**
  427. * @brief EC800M初始化GNSS
  428. * @param NONE
  429. * @note
  430. * @retval 无
  431. */
  432. uint8_t gnssStep = 0;
  433. void EC800_gnss_init(void){
  434. char errrCnt = 0; // 错误计数
  435. switch(gnssStep){
  436. case 6:
  437. EC800M_SendCommand(AT_QGPS_0);
  438. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  439. printf("Turn off the GNSS module\r\n");
  440. }else{
  441. errrCnt++;
  442. printf("Failed to turn off the GNSS module\r\n");
  443. }
  444. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  445. if(errrCnt == 0){
  446. gnssStep = 7;
  447. }else{
  448. gnssStep = 7;
  449. }
  450. break;
  451. case 7:
  452. EC800M_SendCommand(AT_QGPS_1);
  453. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  454. printf("Turn on the GNSS module\r\n");
  455. }else{
  456. errrCnt++;
  457. printf("Failed to turn on the GNSS module\r\n");
  458. }
  459. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  460. if(errrCnt == 0){
  461. gnssStep = 8;
  462. }else{
  463. errrCnt = 0;
  464. }
  465. break;
  466. case 0:
  467. EC800M_SendCommand(AT_QGPSCFG_out_port);
  468. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  469. printf("Outputting NMEA Sentences via Serial Debugging\r\n");
  470. }else{
  471. errrCnt++;
  472. printf("The NMEA sentence output is set incorrectly\r\n");
  473. }
  474. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  475. if(errrCnt == 0){
  476. gnssStep = 1;
  477. }else{
  478. errrCnt = 0;
  479. }
  480. break;
  481. case 1:
  482. EC800M_SendCommand(AT_QGPSCFG_nmeasrc);
  483. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  484. printf("Enabling NMEA sentence retrieval via AT+QGPSGNMEA\r\n");
  485. }else{
  486. errrCnt++;
  487. printf("Failed to enable retrieving NMEA sentences via AT+QGPSGNMEA\r\n");
  488. }
  489. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  490. if(errrCnt == 0){
  491. gnssStep = 2;
  492. }else{
  493. errrCnt = 0;
  494. }
  495. break;
  496. case 2:
  497. EC800M_SendCommand(AT_QGPSCFG_gpsnmeatype);
  498. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  499. printf("Configuring the output type of NMEA sentences to all formats\r\n");
  500. }else{
  501. errrCnt++;
  502. printf("Failed to configure the output type of NMEA sentences to all formats\r\n");
  503. }
  504. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  505. if(errrCnt == 0){
  506. gnssStep = 3;
  507. }else{
  508. errrCnt = 0;
  509. }
  510. break;
  511. case 3:
  512. EC800M_SendCommand(AT_QGPSCFG_gnssconfig);
  513. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  514. printf("Configuring the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  515. }else{
  516. errrCnt++;
  517. printf("Failed to configure the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  518. }
  519. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  520. if(errrCnt == 0){
  521. gnssStep = 4;
  522. }else{
  523. errrCnt = 0;
  524. }
  525. break;
  526. case 4:
  527. EC800M_SendCommand(AT_QGPSCFG_autogps);
  528. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  529. printf("Disable GNSS auto-start\r\n");
  530. }else{
  531. errrCnt++;
  532. printf("Failed to disable GNSS auto-start\r\n");
  533. }
  534. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  535. if(errrCnt == 0){
  536. gnssStep = 5;
  537. }else{
  538. errrCnt = 0;
  539. }
  540. break;
  541. case 5:
  542. EC800M_SendCommand(AT_QGPSCFG_apflash);
  543. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  544. printf("Disable AP-Flash quick start function\r\n");
  545. }else{
  546. errrCnt++;
  547. printf("Failed to disable AP-Flash quick start function\r\n");
  548. }
  549. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  550. if(errrCnt == 0){
  551. gnssStep = 6;
  552. }else{
  553. errrCnt = 0;
  554. }
  555. break;
  556. default:
  557. break;
  558. }
  559. }
  560. //UTC时间转换为任意时区时间,如果是转换为北京时间,timezone传8即可
  561. void utc_to_local_time(nmea_utc_time* utc_time, int8_t timezone, nmea_utc_time* local_time)
  562. {
  563. int year,month,day,hour;
  564. int lastday = 0; //last day of this month 本月天数
  565. int lastlastday = 0; //last day of last month 上个月天数
  566. year = utc_time->year; //utc time
  567. month = utc_time->month;
  568. day = utc_time->date;
  569. hour = utc_time->hour + timezone;
  570. //1月大,2月小,3月大,4月小,5月大,6月小,7月大,8月大,9月小,10月大,11月小,12月大
  571. if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
  572. {
  573. lastday = 31;//本月天数
  574. lastlastday = 30;//这里应该补上上个月的天数
  575. if(month == 3)
  576. {
  577. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year
  578. lastlastday = 29;
  579. else
  580. lastlastday = 28;
  581. }
  582. if(month == 8 || month == 1)//这里应该是8月和1月,因为8月和1月的上一个月(7月和12月)的天数是31天的
  583. lastlastday = 31;
  584. }
  585. else if(month == 4 || month == 6 || month == 9 || month == 11)
  586. {
  587. lastday = 30;
  588. lastlastday = 31;
  589. }
  590. else
  591. {
  592. lastlastday = 31;
  593. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
  594. lastday = 29;
  595. else
  596. lastday = 28;
  597. }
  598. if(hour >= 24)// if >24, day+1
  599. {
  600. hour -= 24;
  601. day += 1;
  602. if(day > lastday)// next month, day-lastday of this month
  603. {
  604. day -= lastday;
  605. month += 1;
  606. if(month > 12)// next year, month-12
  607. {
  608. month -= 12;
  609. year += 1;
  610. }
  611. }
  612. }
  613. if(hour < 0)// if <0, day-1
  614. {
  615. hour += 24;
  616. day -= 1;
  617. if(day < 1)// month-1, day=last day of last month
  618. {
  619. day = lastlastday;
  620. month -= 1;
  621. if(month < 1)// last year, month=12
  622. {
  623. month = 12;
  624. year -= 1;
  625. }
  626. }
  627. }
  628. // transfer value to local_time
  629. local_time->year = year;
  630. local_time->month = month;
  631. local_time->date = day;
  632. local_time->hour = hour;
  633. local_time->min = utc_time->min;
  634. local_time->sec = utc_time->sec;
  635. }
  636. /**
  637. * @brief 常用时间格式转时间戳
  638. * @param NONE
  639. * @note 要用utc时间进行转换,如果输入为北京时间的话,需要 hour-8;
  640. * @retval 无
  641. */
  642. uint32_t EC800_mktime (unsigned int year, unsigned int mon,
  643. unsigned int day, unsigned int hour,
  644. unsigned int min, unsigned int sec)
  645. {
  646. if (0 >= (int) (mon -= 2)){ /**//* 1..12 -> 11,12,1..10 */
  647. mon += 12; /**//* Puts Feb last since it has leap day */
  648. year -= 1;
  649. }
  650. return (((
  651. (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  652. year*365 - 719499
  653. )*24 + hour /**//* now have hours */
  654. )*60 + min /**//* now have minutes */
  655. )*60 + sec; /**//* finally seconds */
  656. }
  657. /**
  658. * @brief 读取GNSS的数据
  659. * @param NONE
  660. * @note
  661. * @retval 0:接收失败 1:接收成功
  662. */
  663. uint8_t EC800_readGnssDate(void){
  664. EC800M_SendCommand(AT_QGPSGNMEA_RMC);
  665. // EC800_recEnable();
  666. // found = strstr(g_usart4_rx_buf, AT_RESP_OK);
  667. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  668. printf("Location information received successfully\r\n");
  669. }else{
  670. printf("Location information reception failed\r\n");
  671. return 0;
  672. }
  673. printf("Location information:%s\r\n", g_usart4_rx_buf);
  674. NMEA_GNRMC_Analysis(&s_nmea_msg, (uint8_t*)g_usart4_rx_buf);
  675. utc_to_local_time(&(s_nmea_msg.utc), 8, &s_nmea_utc_time);
  676. printf("timer-%d/%d/%d %d:%d:%d\r\n",s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,s_nmea_utc_time.hour,s_nmea_utc_time.min,s_nmea_utc_time.sec);
  677. s_messageDate.Timestamp = EC800_mktime(s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,s_nmea_utc_time.hour - 8,s_nmea_utc_time.min,s_nmea_utc_time.sec);
  678. g_usart4_rx_sta= 0;
  679. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  680. printf("latitude:%d%d--longitude:%d%d\r\n", s_nmea_msg.latitude, s_nmea_msg.nshemi, s_nmea_msg.longitude, s_nmea_msg.ewhemi);
  681. /* 尝试获取互斥量,等待无限长时间 */
  682. if(osMutexAcquire(s_messageDate_locationHandle, osWaitForever) == osOK)
  683. {
  684. /* 安全地访问s_rs485RecDate结构体 */
  685. s_messageDate.latitude = s_nmea_msg.latitude;
  686. s_messageDate.nshemi = s_nmea_msg.nshemi;
  687. s_messageDate.longitude = s_nmea_msg.longitude;
  688. s_messageDate.ewhemi = s_nmea_msg.ewhemi;
  689. /* 访问完成,释放互斥量 */
  690. osMutexRelease(s_messageDate_locationHandle);
  691. }
  692. return 1;
  693. }
  694. /**
  695. * @brief EC800M订阅主题
  696. * @param topic1: 订阅的主题名称1字符串形式
  697. * @param topic2: 订阅的主题名称2字符串形式
  698. * @note
  699. * @retval 无
  700. */
  701. uint8_t EC800_subscribeToTopic(const char* topic1){
  702. char command[100] = {0};
  703. uint8_t recTemp = 0;
  704. sprintf(command, "AT+QMTSUB=0,1,\"%s\",0\r\n", topic1);
  705. EC800M_SendCommand(command);
  706. recTemp = Accept_and_Compare_Str(AT_RESP_QMTSUB);
  707. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  708. if (recTemp == 1) {
  709. printf("Subscription to the topic successful\r\n");
  710. return 1;
  711. }else if (recTemp == 2){
  712. printf("Subscription to the topic failed\r\n");
  713. return 0;
  714. }
  715. return 0;
  716. }
  717. /**
  718. * @brief EC800M发布消息
  719. * @param topic: 消息发布的主题 字符串形式
  720. * @param message: 发布的消息 字符串形式
  721. * @param len: 发布消息的字节长度
  722. * @note
  723. * @retval 无
  724. */
  725. uint8_t EC800_publishMessage(const char* topic, const char* message, uint16_t len){
  726. char command[100] = {0};
  727. char* responseCmd = ">";
  728. static uint8_t publishStep = 0;
  729. uint8_t recBack = 0;
  730. switch(publishStep){
  731. case 0:
  732. sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
  733. EC800M_SendCommand(command);
  734. // found = EC800M_RecRespond(g_usart4_rx_buf, responseCmd);
  735. if (Accept_and_Compare_Str(responseCmd) == 1) {
  736. printf("The topic has been linked\r\n");
  737. publishStep = 1;
  738. }else{
  739. printf("The topic link failed\r\n");
  740. publishStep = 0;
  741. }
  742. // break;
  743. case 1:
  744. EC800M_SendCommand(message);
  745. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  746. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTPUBEX);
  747. // if (found != NULL) {
  748. // printf("The message has been successfully posted\r\n");
  749. // publishStep = 0;
  750. // }else{
  751. // printf("Message publication failed\r\n");
  752. // publishStep = 0;
  753. // }
  754. recBack = Accept_and_Compare_Str(AT_RESP_QMTPUBEX);
  755. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  756. if (recBack == 1) {
  757. publishStep = 0;
  758. printf("The message has been successfully posted\r\n");
  759. }else{
  760. printf("Message publication failed\r\n");
  761. publishStep = 0;
  762. }
  763. break;
  764. default:
  765. break;
  766. }
  767. return recBack;
  768. }
  769. // 将uint8_t数组中的ASCII码数据转换为字符串的函数
  770. void uint8ArrayToAsciiString(const uint8_t* array, size_t length, char* str) {
  771. for (size_t i = 0; i < length; ++i) {
  772. str[i] = (char)array[i]; // 直接将每个字节转换为字符
  773. }
  774. str[length] = '\0'; // 添加字符串结束符
  775. }
  776. /**
  777. * @brief 登录 topic
  778. * @param NONE
  779. * @note topic 101
  780. * @retval 无
  781. */
  782. uint8_t Login_Topic(void){
  783. cJSON *root = NULL;
  784. char *jsonString = NULL;
  785. uint8_t temp = 0;
  786. char command[50] = {0};
  787. // 定义对象 { }
  788. root = cJSON_CreateObject();
  789. // 插入元素,对应 键值对
  790. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  791. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  792. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  793. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  794. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  795. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  796. memset(command, 0, 50);
  797. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  798. cJSON_AddItemToObject(root, "hardwareId", cJSON_CreateString(command));
  799. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  800. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  801. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  802. // 将 JSON 对象转换为字符串
  803. jsonString = cJSON_Print(root);
  804. cJSON_Delete(root);
  805. temp = EC800_publishMessage("toServer/mqtt/101", jsonString, strlen(jsonString));
  806. free(jsonString);
  807. return temp;
  808. }
  809. /**
  810. * @brief 下位机请求围栏数据
  811. * @param NONE
  812. * @note topic 604
  813. * @retval 无
  814. */
  815. uint8_t requesting_Fence_Data(void){
  816. cJSON *root = NULL;
  817. char *jsonString = NULL;
  818. uint8_t temp = 0;
  819. char command[50] = {0};
  820. // 定义对象 { }
  821. root = cJSON_CreateObject();
  822. // 插入元素,对应 键值对
  823. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  824. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  825. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  826. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  827. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  828. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  829. memset(command, 0, 50);
  830. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  831. cJSON_AddItemToObject(root, "vin", cJSON_CreateString(command));
  832. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  833. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  834. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  835. // 将 JSON 对象转换为字符串
  836. jsonString = cJSON_Print(root);
  837. cJSON_Delete(root);
  838. temp = EC800_publishMessage("toServer/mqtt/604", jsonString, strlen(jsonString));
  839. free(jsonString);
  840. return temp;
  841. }
  842. /**
  843. * @brief 创建单个报警项的JSON对象
  844. * @param NONE
  845. * @note topic toServer/mqtt/501
  846. * @retval 无
  847. */
  848. cJSON* createAlarmItem(const char* id, uint32_t alarmTime, const char* alarmDesc, int alarmFlag) {
  849. cJSON* alarmItem = cJSON_CreateObject();
  850. cJSON_AddStringToObject(alarmItem, "id", id);
  851. cJSON_AddNumberToObject(alarmItem, "alarmTime", alarmTime);
  852. cJSON_AddStringToObject(alarmItem, "alarmDesc", alarmDesc);
  853. cJSON_AddNumberToObject(alarmItem, "alarmFlag", alarmFlag);
  854. return alarmItem;
  855. }
  856. /**
  857. * @brief 根据Malfunction的值插入故障元素
  858. * @param NONE
  859. * @note topic toServer/mqtt/501
  860. * @retval 无
  861. */
  862. void insertFaultItems(cJSON* alarmList, uint32_t Malfunction, uint32_t fault_timeStamp) {
  863. for (int i = 0; i < 16; i++) {
  864. if (Malfunction & (1 << i)) {
  865. // 确保i在数组的有效范围内
  866. if (i < 8) {
  867. cJSON_AddItemToArray(alarmList, createAlarmItem(faults[i].errid, fault_timeStamp, faults[i].errorInfo, 1));
  868. }
  869. }
  870. }
  871. }
  872. /**
  873. * @brief 故障数据上传
  874. * @param NONE
  875. * @note topic toServer/mqtt/501
  876. * @retval 无
  877. */
  878. uint8_t EC800_uploadFaultReport(uint32_t fault_time_stamp, uint32_t fault_Code){
  879. cJSON *root = NULL;
  880. char *jsonString = NULL;
  881. uint8_t temp = 0;
  882. char command[50] = {0};
  883. // 定义对象 { }
  884. root = cJSON_CreateObject();
  885. // 插入元素,对应 键值对
  886. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  887. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  888. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  889. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  890. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  891. // 插入故障元素
  892. cJSON* alarmList = cJSON_CreateArray();
  893. insertFaultItems(alarmList, fault_Code, fault_time_stamp);
  894. cJSON_AddItemToObject(root, "alarmList", alarmList);
  895. // 将 JSON 对象转换为字符串
  896. jsonString = cJSON_Print(root);
  897. cJSON_Delete(root);
  898. temp = EC800_publishMessage("toServer/mqtt/501", jsonString, strlen(jsonString));
  899. free(jsonString);
  900. return temp;
  901. }
  902. /**
  903. * @brief EC800M与服务器进行时间对时
  904. * @note
  905. * @retval 无
  906. */
  907. uint8_t EC800_time_calibration(void){
  908. cJSON *root = NULL;
  909. char *jsonString = NULL;
  910. uint8_t temp = 0;
  911. char command[50] = {0};
  912. // 定义对象 { }
  913. root = cJSON_CreateObject();
  914. // 插入元素,对应 键值对
  915. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  916. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  917. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  918. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  919. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  920. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  921. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  922. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  923. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  924. // 将 JSON 对象转换为字符串
  925. jsonString = cJSON_Print(root);
  926. cJSON_Delete(root);
  927. temp = EC800_publishMessage("toServer/mqtt/105", jsonString, strlen(jsonString));
  928. free(jsonString);
  929. return temp;
  930. }
  931. /**
  932. * @brief 将服务器响应的字符串中的年月日进行分离
  933. * @note
  934. * @retval 无
  935. */
  936. void parseServerTime(char* str){
  937. char year[5];
  938. char month[3];
  939. char day[3];
  940. char hour[3];
  941. char minute[3];
  942. char second[3];
  943. strncpy(year, str, 4);
  944. year[4] = '\0';
  945. strncpy(month, str + 4, 2);
  946. month[2] = '\0';
  947. strncpy(day, str + 6, 2);
  948. day[2] = '\0';
  949. strncpy(hour, str + 8, 2);
  950. hour[2] = '\0';
  951. strncpy(minute, str + 10, 2);
  952. minute[2] = '\0';
  953. strncpy(second, str + 12, 2);
  954. second[2] = '\0';
  955. s_recDate.year = atoi(year);
  956. s_recDate.month = atoi(month);
  957. s_recDate.day = atoi(day);
  958. s_recDate.hour = atoi(hour);
  959. s_recDate.minute = atoi(minute);
  960. s_recDate.sec = atoi(second);
  961. s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour - 8,s_recDate.minute,s_recDate.sec);
  962. printf("Year (int): %d\n", s_recDate.year);
  963. printf("Month (int): %d\n", s_recDate.month);
  964. printf("Day (int): %d\n", s_recDate.day);
  965. printf("Hour (int): %d\n", s_recDate.hour);
  966. printf("Minute (int): %d\n", s_recDate.minute);
  967. printf("Second (int): %d\n", s_recDate.sec);
  968. }
  969. /**
  970. * @brief 解析对时数据帧的响应
  971. * @note
  972. * @retval 无
  973. */
  974. void EC800_parseRespondTime(cJSON *root, cJSON *item){
  975. uint8_t temp = 0;
  976. // 获取"resultCode"字段的值
  977. item = cJSON_GetObjectItem(root, "resultCode");
  978. if (item == NULL) {
  979. printf("Field \"resultCode\" not found\n");
  980. }else{
  981. // 打印"resultCode"字段的值
  982. printf("resultCode: %d\n", item->valueint);
  983. if((item->valueint) == 1){
  984. printf("The upload of synchronized data was successful.\n");
  985. temp = 1; // 上传成功
  986. }else{
  987. printf("The upload of synchronized data has failed.\n");
  988. }
  989. }
  990. if(temp == 1){
  991. item = cJSON_GetObjectItem(root, "serverTime");
  992. if (item == NULL) {
  993. printf("Field \"serverTime\" not found\n");
  994. }else{
  995. // 打印"serverTime"字段的值
  996. printf("serverTime: %s\n", item->valuestring);
  997. parseServerTime(item->valuestring);
  998. }
  999. }
  1000. }
  1001. /**
  1002. * @brief EC800M上传实时数据
  1003. * @note
  1004. * @retval 无
  1005. */
  1006. uint8_t EC800_uploadRealDate(void){
  1007. cJSON *root = NULL;
  1008. char *jsonString = NULL;
  1009. cJSON *rt000 = NULL;
  1010. cJSON *rt000Object1 = NULL;
  1011. char command[50] = {0};
  1012. uint32_t temp = 0; // 临时存储
  1013. // 定义对象 { }
  1014. root = cJSON_CreateObject();
  1015. //
  1016. // 插入元素,对应 键值对
  1017. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  1018. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  1019. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  1020. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  1021. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  1022. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  1023. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  1024. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  1025. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  1026. // 定义 { } 对象
  1027. rt000Object1 = cJSON_CreateObject();
  1028. cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
  1029. cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber((float)s_messageDate.demandVol / 10));
  1030. cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber((float)s_messageDate.demandCur / 10));
  1031. memset(command, 0, 50);
  1032. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  1033. cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString(command));
  1034. double point1Lat = s_messageDate.latitude / 100000;
  1035. double point1Lng = s_messageDate.longitude / 100000;
  1036. memset(command, 0, 50);
  1037. snprintf(command, sizeof(command), "%.5f,%.5f", point1Lat, point1Lng);
  1038. cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(command));
  1039. cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
  1040. cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
  1041. cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(s_messageDate.dailyDrivTime >> 8));
  1042. cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage / 10));
  1043. temp = (s_messageDate.accTotalDrivTime_day * 24) + (s_messageDate.accTotalDrivTime_h_min >> 8);
  1044. cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(temp));
  1045. cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
  1046. temp = 0;
  1047. temp = (s_messageDate.runTime >> 8) * 60 + (s_messageDate.runTime & 0x00ff);
  1048. cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(temp));
  1049. // 定义 [ ] 数组
  1050. rt000 = cJSON_CreateArray();
  1051. // 往数组中添加元素
  1052. cJSON_AddItemToArray(rt000, rt000Object1);
  1053. // 将子项插入根项中
  1054. cJSON_AddItemToObject(root, "rt000", rt000);
  1055. //
  1056. // 将 JSON 对象转换为字符串
  1057. jsonString = cJSON_Print(root);
  1058. cJSON_Delete(root);
  1059. temp = 0;
  1060. temp = EC800_publishMessage("toServer/mqtt/201", jsonString, strlen(jsonString));
  1061. free(jsonString);
  1062. return temp;
  1063. }
  1064. /**
  1065. * @brief 解析实时数据响应
  1066. * @note
  1067. * @retval 无
  1068. */
  1069. void EC800_respondRealDate(cJSON *root, cJSON *item){
  1070. // 获取"name"字段的值
  1071. item = cJSON_GetObjectItem(root, "resultCode");
  1072. if (item == NULL) {
  1073. printf("Field \"resultCode\" not found\n");
  1074. }else{
  1075. // 打印"name"字段的值
  1076. printf("resultCode: %d\n", item->valueint);
  1077. if((item->valueint) == 1){
  1078. printf("Uploaded real-time data successfully\n");
  1079. }else{
  1080. printf("The upload of real-time data has failed\n");
  1081. }
  1082. }
  1083. }
  1084. /**
  1085. * @brief 解析升級数据
  1086. * @note
  1087. * @retval 无
  1088. */
  1089. void EC800_respondUpdateDate(cJSON *root, cJSON *item, ftpInfo *p_ftpInfo){
  1090. /* 获取address对象 */
  1091. cJSON *hardwareUpdate = cJSON_GetObjectItem(root, "paramList");
  1092. if (hardwareUpdate == NULL) {
  1093. printf("Failed to parse paramList data\n");
  1094. return ;
  1095. }
  1096. // 获取"name"字段的值
  1097. item = cJSON_GetObjectItem(hardwareUpdate, "function");
  1098. if (item == NULL) {
  1099. printf("Field \"update\" not found\n");
  1100. }else{
  1101. // 打印"name"字段的值
  1102. printf("function: %d\n", item->valueint);
  1103. // 1:表示固件更新 当后台下发升级指令,并且行车状态为静止态允许更新固件
  1104. if(((item->valueint) == 1) && (s_comData.driveStatus == 0x02)){
  1105. s_ec800Date.hardwareUpdate = 1; // 更新固件
  1106. printf("update begin\n");
  1107. }else{
  1108. printf("no update\n");
  1109. }
  1110. }
  1111. // 获取"ftpAddr"字段的值
  1112. item = cJSON_GetObjectItem(hardwareUpdate, "ftpAddr");
  1113. if (item == NULL) {
  1114. printf("Field \"ftpAddr\" not found\n");
  1115. }else{
  1116. printf("ftpAddr: %s\n", item->valuestring);
  1117. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1118. strncpy(p_ftpInfo->ftpAddr, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1119. /* 确保字符串以空字符结尾 */
  1120. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->ftpAddr) - 1] = '\0';
  1121. }
  1122. // 获取"ftpPort"字段的值
  1123. item = cJSON_GetObjectItem(hardwareUpdate, "ftpPort");
  1124. if (item == NULL) {
  1125. printf("Field \"ftpPort\" not found\n");
  1126. }else{
  1127. // 打印"name"字段的值
  1128. printf("ftpPort: %d\n", item->valueint);
  1129. p_ftpInfo->ftpPort = item->valueint;
  1130. }
  1131. // 获取"account"字段的值
  1132. item = cJSON_GetObjectItem(hardwareUpdate, "account");
  1133. if (item == NULL) {
  1134. printf("Field \"account\" not found\n");
  1135. }else{
  1136. printf("account: %s\n", item->valuestring);
  1137. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1138. strncpy(p_ftpInfo->account, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1139. /* 确保字符串以空字符结尾 */
  1140. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->account) - 1] = '\0';
  1141. }
  1142. // 获取"passWord"字段的值
  1143. item = cJSON_GetObjectItem(hardwareUpdate, "passWord");
  1144. if (item == NULL) {
  1145. printf("Field \"passWord\" not found\n");
  1146. }else{
  1147. printf("account: %s\n", item->valuestring);
  1148. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1149. strncpy(p_ftpInfo->passWord, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1150. /* 确保字符串以空字符结尾 */
  1151. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->passWord) - 1] = '\0';
  1152. }
  1153. // 获取"passWord"字段的值
  1154. item = cJSON_GetObjectItem(hardwareUpdate, "textDirectory");
  1155. if (item == NULL) {
  1156. printf("Field \"textDirectory\" not found\n");
  1157. }else{
  1158. printf("textDirectory: %s\n", item->valuestring);
  1159. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1160. strncpy(p_ftpInfo->textDirectory, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1161. /* 确保字符串以空字符结尾 */
  1162. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textDirectory) - 1] = '\0';
  1163. }
  1164. // 获取"passWord"字段的值
  1165. item = cJSON_GetObjectItem(hardwareUpdate, "textName");
  1166. if (item == NULL) {
  1167. printf("Field \"textName\" not found\n");
  1168. }else{
  1169. printf("textName: %s\n", item->valuestring);
  1170. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1171. strncpy(p_ftpInfo->textName, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1172. /* 确保字符串以空字符结尾 */
  1173. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textName) - 1] = '\0';
  1174. }
  1175. }
  1176. /**
  1177. * @brief 解析围栏经纬度数据
  1178. * @note
  1179. * @param root: cJSON 根对象
  1180. * @param Latitude: 纬度数组
  1181. * @param Longitude: 经度数组
  1182. * @retval 成功返回1,失败返回0
  1183. */
  1184. int parse_Latitude_Longitude_Data(cJSON *root, double Latitude[], double Longitude[]) {
  1185. // 确保输入不为空
  1186. if (root == NULL) {
  1187. printf("Root object is NULL\n");
  1188. return 0;
  1189. }
  1190. // 获取 polygonLat 数组
  1191. cJSON *polygonLat = cJSON_GetObjectItem(root, "polygonLat");
  1192. if (polygonLat == NULL || polygonLat->type != cJSON_Array) {
  1193. printf("polygonLat is not an array or is missing\n");
  1194. return 0;
  1195. }
  1196. // 获取 polygonLng 数组
  1197. cJSON *polygonLng = cJSON_GetObjectItem(root, "polygonLon");
  1198. if (polygonLng == NULL || polygonLat->type != cJSON_Array) {
  1199. printf("polygonLng is not an array or is missing\n");
  1200. return 0;
  1201. }
  1202. // 获取数组大小并检查是否匹配和是否超出预定义的最大大小
  1203. int polygonLatSize = cJSON_GetArraySize(polygonLat);
  1204. int polygonLngSize = cJSON_GetArraySize(polygonLng);
  1205. if (polygonLatSize != polygonLngSize || polygonLatSize > REC_COORDINATE_DEPTH) {
  1206. printf("Array sizes mismatch or exceed maximum allowed size\n");
  1207. return 0;
  1208. }
  1209. // 解析 polygonLat 数组
  1210. for (int i = 0; i < polygonLatSize; ++i) {
  1211. cJSON *latItem = cJSON_GetArrayItem(polygonLat, i);
  1212. if (latItem == NULL || latItem->type != cJSON_Number) {
  1213. printf("Lat item is not a number or is missing\n");
  1214. return 0;
  1215. }
  1216. Latitude[i] = latItem->valuedouble;
  1217. }
  1218. // 解析 polygonLng 数组
  1219. for (int i = 0; i < polygonLngSize; ++i) {
  1220. cJSON *lngItem = cJSON_GetArrayItem(polygonLng, i);
  1221. if (lngItem == NULL || lngItem->type != cJSON_Number) {
  1222. printf("Lng item is not a number or is missing\n");
  1223. return 0;
  1224. }
  1225. Longitude[i] = lngItem->valuedouble;
  1226. }
  1227. // // 解析 speedLimit 字段
  1228. // cJSON* speedLimit = cJSON_GetObjectItem(root, "speedLimit");
  1229. // if (speedLimit == NULL) {
  1230. // printf("Field \"speedLimit\" not found\n");
  1231. // return 0;
  1232. // }else{
  1233. // s_param_boot.speed_limit = speedLimit->valueint;
  1234. // }
  1235. //
  1236. // // 解析 overspeedTimeout 字段
  1237. // cJSON* overspeedTimeout = cJSON_GetObjectItem(root, "overspeedTimeout");
  1238. // if (speedLimit == NULL) {
  1239. // printf("Field \"overspeedTimeout\" not found\n");
  1240. // return 0;
  1241. // }else{
  1242. // s_param_boot.overspeed_Timeout = speedLimit->valueint;
  1243. // }
  1244. //
  1245. // // 解析 fenceBreachTimeout 字段
  1246. // cJSON* fenceBreachTimeout = cJSON_GetObjectItem(root, "fenceBreachTimeout");
  1247. // if (speedLimit == NULL) {
  1248. // printf("Field \"fenceBreachTimeout\" not found\n");
  1249. // return 0;
  1250. // }else{
  1251. // s_param_boot.fenceBreach_Timeout = speedLimit->valueint;
  1252. // }
  1253. //
  1254. return 1; // 成功
  1255. }
  1256. /**
  1257. * @brief 解析登录响应
  1258. * @note
  1259. * @return
  1260. */
  1261. void parse_Login_Response(cJSON *root, cJSON *item){
  1262. // 获取"name"字段的值
  1263. item = cJSON_GetObjectItem(root, "resultCode");
  1264. if (item == NULL) {
  1265. printf("Field \"resultCode\" not found\n");
  1266. }else{
  1267. // 打印"name"字段的值
  1268. printf("resultCode: %d\n", item->valueint);
  1269. if((item->valueint) == 1){
  1270. s_messageDate.loginResult = 1; // 登录成功
  1271. printf("Login response data successfully\n");
  1272. }else{
  1273. printf("Login response has failed\n");
  1274. }
  1275. }
  1276. }
  1277. /**
  1278. * @brief 解析远程控制指令
  1279. * @note
  1280. * @return
  1281. */
  1282. void processRemoteControl(cJSON *root, cJSON *item){
  1283. global_par *p_global_par = &s_global_par;
  1284. // 解析paramList
  1285. cJSON* paramList = cJSON_GetObjectItem(root, "paramList");
  1286. if (cJSON_GetArraySize(paramList) == 0) {
  1287. printf("paramList is empty\n");
  1288. return;
  1289. }
  1290. cJSON* firstParam = cJSON_GetArrayItem(paramList, 0);
  1291. if (!firstParam) {
  1292. printf("paramList is empty\n");
  1293. return;
  1294. }
  1295. // 解析paramList中的第一个元素
  1296. cJSON* id = cJSON_GetObjectItem(firstParam, "id");
  1297. if (item == NULL){
  1298. printf("id is not a string\n");
  1299. return;
  1300. }else{
  1301. printf("id: %d\n", id->valueint);
  1302. }
  1303. item = cJSON_GetObjectItem(firstParam, "value");
  1304. if (item == NULL){
  1305. printf("value is not a string\n");
  1306. return;
  1307. }else{
  1308. printf("value: %d\n", item->valueint);
  1309. }
  1310. // 解析指令
  1311. switch(id->valueint){
  1312. case 0: // 超级权限
  1313. p_global_par->superUser = (item->valueint == 1);
  1314. break;
  1315. case 1: // 更新围栏数据
  1316. p_global_par->fence_update = (item->valueint == 1);
  1317. break;
  1318. case 2: // 车辆限速
  1319. s_param_boot.speed_limit = item->valueint;
  1320. break;
  1321. case 3: // 锁车
  1322. s_param_boot.car_lock_flag = (item->valueint == 1);
  1323. break;
  1324. default: break;
  1325. }
  1326. }
  1327. /**
  1328. * @brief 通信响应解析
  1329. * @param p_ec800Date: ec800Date结构体的指针数据
  1330. * @note
  1331. * @return 当前的控制命令
  1332. */
  1333. short EC800_respondParse(ec800Date *p_ec800Date){
  1334. cJSON *root = NULL;
  1335. cJSON *item = NULL;
  1336. uint8_t rec_back = 0;
  1337. uint16_t cmd = 0;
  1338. // 找到JSON数据的起始位置
  1339. const char* start = strchr(g_usart4_rx_buf, '{');
  1340. if (start == NULL) {
  1341. printf("JSON data not found\n");
  1342. return 0;
  1343. }
  1344. // 解析JSON数据
  1345. root = cJSON_Parse(start);
  1346. if (root == NULL) {
  1347. printf("Failed to parse JSON data\n");
  1348. cJSON_Delete(root);
  1349. return 0;
  1350. }
  1351. // 获取"controlCode"字段的值
  1352. item = cJSON_GetObjectItem(root, "controlCode");
  1353. if (item == NULL) {
  1354. printf("Field \"devId\" not found\n");
  1355. }else{
  1356. // 打印"controlCode"字段的值
  1357. printf("controlCode: %d\n", item->valueint);
  1358. cmd = item->valueint;
  1359. }
  1360. // 解析对应命令的对应数据
  1361. switch(cmd){
  1362. case 102:
  1363. parse_Login_Response(root, item);
  1364. break;
  1365. case 106:
  1366. EC800_parseRespondTime(root, item);
  1367. break;
  1368. case 202:
  1369. EC800_respondRealDate(root, item);
  1370. break;
  1371. case 602:
  1372. EC800_respondUpdateDate(root, item, &s_ftpInfo);
  1373. break;
  1374. case 605:
  1375. rec_back = parse_Latitude_Longitude_Data(root, polygonLat, polygonLng);
  1376. if(rec_back == 1){ // 接收成功
  1377. p_ec800Date->fenceRecSuccess = 1;
  1378. }
  1379. break;
  1380. case 502: // 告警响应
  1381. break;
  1382. case 601: // 远程控制
  1383. processRemoteControl(root, item);
  1384. break;
  1385. default :
  1386. break;
  1387. }
  1388. cJSON_Delete(root);
  1389. return (cmd);
  1390. }
  1391. uint8_t stateStep = 0;
  1392. uint16_t timesCnt = 0; // 实时发送计数器
  1393. uint8_t respondSt = 0; // 响应状态指示 1: 为等待响应 2:成功响应
  1394. /**
  1395. * @brief EC800M状态转换与使用
  1396. * @note
  1397. * @retval 无
  1398. */
  1399. void EC800_stateTransition_use(void){
  1400. uint16_t right = 1; // 返回是否为正确
  1401. ec800Date *p_ec800Date = &s_ec800Date;
  1402. char command[100] = {0};
  1403. static uint8_t interCnt = 0; // 当前区间进入次数
  1404. global_par *p_global_par = &s_global_par;
  1405. switch(stateStep){
  1406. case 0: // 初始化模块
  1407. interCnt++;
  1408. EC800M_link();
  1409. // 初始化连接MQTT失败
  1410. if(interCnt > 15){
  1411. interCnt = 16;
  1412. p_global_par->InitFaultFlag_4G = 1;
  1413. }
  1414. // 初始化成功
  1415. if(linkStep == 12){
  1416. interCnt = 0; // 清除进入计数
  1417. p_global_par->InitFaultFlag_4G = 0;
  1418. stateStep = 1;
  1419. p_ec800Date->ec800InitFlag = 1; // 初始化完成
  1420. }else{
  1421. p_ec800Date->ec800InitFlag = 0; // 初始化未完成
  1422. }
  1423. break;
  1424. case 1: // 初始化GNSS
  1425. interCnt++;
  1426. EC800_gnss_init();
  1427. // 初始化GNSS失败
  1428. if(interCnt > 11){
  1429. interCnt = 16;
  1430. p_global_par->InitFaultFlag_4G = 1;
  1431. }
  1432. if(gnssStep == 8){
  1433. stateStep = 2;
  1434. interCnt = 0; // 清除进入计数
  1435. p_global_par->InitFaultFlag_4G = 0;
  1436. }
  1437. break;
  1438. case 2: // 订阅主题
  1439. sprintf(command, "toclient/%d/%x%x%x", DEV_TYPE, s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  1440. right = EC800_subscribeToTopic(command);
  1441. if(right != 1){ // 发布消息失败,可能断开链接
  1442. if(interCnt < 3){
  1443. interCnt++;
  1444. }else{
  1445. interCnt = 16;
  1446. p_global_par->InitFaultFlag_4G = 1;
  1447. }
  1448. }else{
  1449. interCnt = 0; // 清除进入计数
  1450. p_global_par->InitFaultFlag_4G = 0;
  1451. stateStep = 3;
  1452. }
  1453. break;
  1454. case 3: // 登录服务器主题
  1455. right = Login_Topic();
  1456. if(right == 1){
  1457. respondSt = 1; // 等待响应
  1458. stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
  1459. }
  1460. break;
  1461. case 4: // 下位机请求围栏数据
  1462. if(s_comData.vinRecSuccess == 1){ // 成功接收到车辆vin
  1463. right = requesting_Fence_Data();
  1464. if(right == 1){
  1465. respondSt = 1; // 等待响应
  1466. stateStep = 8; // 等待围栏数据响应
  1467. }
  1468. }else{ // 初始化时与车通信故障 跳转上传实时信息,上报故障,跳过围栏获取
  1469. if(BIT_CHECK(s_comData.Malfunction, com485)){
  1470. stateStep = 5;
  1471. }
  1472. }
  1473. // 后台要求重新请求围栏数据,请求完成,清除标志
  1474. if(p_global_par->fence_update == 1){
  1475. p_global_par->fence_update = 0;
  1476. }
  1477. break;
  1478. case 5:
  1479. // 获取定位信息
  1480. right = EC800_readGnssDate();
  1481. // 时间数据是否获取到
  1482. s_global_par.time_stamp_flag = (right == 1);
  1483. // 没有获取到定位信息 重复获取三次
  1484. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  1485. if(p_global_par->positionErrorCnt < 3){
  1486. p_global_par->positionErrorCnt++;
  1487. }else{
  1488. p_global_par->positionErrorCnt = 3;
  1489. stateStep = 6;
  1490. }
  1491. }else{
  1492. p_global_par->positionErrorCnt = 0;
  1493. stateStep = 6;
  1494. }
  1495. break;
  1496. case 6: // 与服务器对时
  1497. right = EC800_time_calibration();
  1498. if(right == 1){
  1499. respondSt = 1; // 等待响应
  1500. stateStep = 8; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
  1501. }
  1502. break;
  1503. case 7: // 发布消息
  1504. right = EC800_uploadRealDate();
  1505. if(right == 0){
  1506. // stateStep = 0;
  1507. // linkStep = 0;
  1508. // stateStep = 6;
  1509. }else if(right == 1){
  1510. stateStep = 8;
  1511. }
  1512. break;
  1513. case 8: // 接收消息
  1514. // 转弯行驶中
  1515. if(turnFlag == 1){
  1516. if(timesCnt < TURN_INTERVAL_TIME_MS){
  1517. timesCnt++;
  1518. }else{
  1519. timesCnt= 0;
  1520. stateStep = 5;
  1521. HAL_UART_DMAStop(&huart4);
  1522. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1523. }
  1524. }else{ // 正常行驶中
  1525. if(timesCnt < PUBLISH_TIME_MS){
  1526. timesCnt++;
  1527. }else{
  1528. timesCnt= 0;
  1529. stateStep = 5;
  1530. HAL_UART_DMAStop(&huart4);
  1531. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1532. }
  1533. }
  1534. // 后台要求重新请求围栏数据
  1535. if(p_global_par->fence_update == 1){
  1536. stateStep = 4;
  1537. }
  1538. // 通信超时判断
  1539. if(respondSt == 1){ // 等待响应状态
  1540. if(p_global_par->mqttTimeoutCnt < PUBLISH_TIME_MS){
  1541. p_global_par->mqttTimeoutCnt ++;
  1542. }else{
  1543. p_global_par->mqttTimeoutCnt = PUBLISH_TIME_MS;
  1544. }
  1545. }else{
  1546. p_global_par->mqttTimeoutCnt = 0;
  1547. }
  1548. // 当MQTT通信异常且gnss回复错误的情况下,4g模块可能断线,重新连接4G模块
  1549. // stateStep = 0;
  1550. // linkStep = 0;
  1551. // gnssStep = 0;
  1552. break;
  1553. default:
  1554. break;
  1555. }
  1556. }
  1557. /**
  1558. * @brief MQTT服务器数据接收处理
  1559. * @note
  1560. * @retval 无
  1561. */
  1562. void handleReceive(void) {
  1563. uint16_t right = 1; // 返回是否为正确
  1564. if(g_usart4_rx_sta!=0){
  1565. HAL_UART_Transmit(&huart1, (uint8_t*)g_usart4_rx_buf, strlen(g_usart4_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  1566. right = EC800_respondParse(&s_ec800Date);
  1567. if((right == CTR_CODE_LOGIN) && (s_messageDate.loginResult == 1)){ // 登录响应
  1568. stateStep = 4;
  1569. right = 0;
  1570. timesCnt = 0;
  1571. respondSt = 2; // 成功响应
  1572. }else if(right == CTR_CODE_LOGIN){ // 登录响应 未登录成功
  1573. stateStep = 3;
  1574. right = 0;
  1575. timesCnt = 0;
  1576. respondSt = 2; // 成功响应
  1577. }else if(right == CTR_CODE_FENCES){ // 围栏数据响应
  1578. stateStep = 5;
  1579. right = 0;
  1580. timesCnt = 0;
  1581. respondSt = 2; // 成功响应
  1582. }
  1583. else if(right == CTR_CODE_JUDETIME){ // 对时响应
  1584. stateStep = 7;
  1585. right = 0;
  1586. // timesCnt = 0;
  1587. respondSt = 2; // 成功响应
  1588. }else if((right == CTR_CODE_REMOTE) && (s_ec800Date.hardwareUpdate == 1)){ // 固件更新
  1589. stateStep = 0xff; // 停止占用4G模块
  1590. }
  1591. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff,接收新的数据
  1592. g_usart4_rx_sta = 0; // 清除接收状态
  1593. HAL_UART_DMAStop(&huart4);
  1594. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1595. }
  1596. }