EC800(2761).c 49 KB

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