EC800(2318).c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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. // 进入临界区 挂起所有任务
  66. vTaskSuspendAll();
  67. stringLen = EC800_calculateStringLength(command);
  68. // 使用DMA
  69. // //等待发送状态OK
  70. // while(HAL_DMA_GetState(&hdma_usart3_tx) == HAL_DMA_STATE_BUSY) osDelay(1);
  71. // //发送数据
  72. // HAL_UART_Transmit_DMA(&huart3, (uint8_t*)command, stringLen);
  73. // 不使用发送dma
  74. // HAL_UART_Transmit(&huart4, (uint8_t*)command, stringLen, 1000);
  75. // 使用中断
  76. HAL_UART_Transmit_IT(&huart4, (uint8_t*)command, stringLen);
  77. // 退出临界区 恢复任务调度
  78. xTaskResumeAll();
  79. }
  80. /**
  81. * @brief 接收指令回复函数
  82. * @param haystack: 接收的字符串数据
  83. * @param needle: 正确回复的数据
  84. * @note
  85. * @retval 无
  86. */
  87. char* EC800M_RecRespond(char *haystack, const char *needle ){
  88. char* p = NULL;
  89. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  90. // __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
  91. while(g_usart4_rx_sta == 0) osDelay(1);
  92. // __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE); // 关闭空闲中断
  93. g_usart4_rx_sta= 0;
  94. p = strstr(haystack, needle);
  95. memset(haystack, 0, USART4_REC_LEN); // 清除数据buff
  96. HAL_UART_DMAStop(&huart4);
  97. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  98. return p;
  99. }
  100. // 自定义的搜索函数
  101. void* search_sequence(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len) {
  102. const unsigned char* h = (const unsigned char*)haystack;
  103. const unsigned char* n = (const unsigned char*)needle;
  104. // 如果needle为空或haystack长度小于needle长度,则直接返回NULL
  105. if (needle_len == 0 || haystack_len < needle_len) {
  106. return NULL;
  107. }
  108. for (size_t i = 0; i <= haystack_len - needle_len; ++i) {
  109. size_t j;
  110. for (j = 0; j < needle_len; ++j) {
  111. if (h[i + j] != n[j]) {
  112. break; // 如果当前字符不匹配,则跳出内层循环
  113. }
  114. }
  115. if (j == needle_len) {
  116. return (void*)(h + i); // 找到匹配的序列,返回其在haystack中的位置
  117. }
  118. }
  119. // 如果遍历了整个haystack都没有找到匹配的序列,则返回NULL
  120. return NULL;
  121. }
  122. // 接收并比较响应字符串
  123. uint8_t Accept_and_Compare_Str(const char* needle){
  124. uint8_t temp = 0;
  125. static uint16_t timeOutCnt = 0; // 超时计数
  126. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  127. // __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
  128. while(!temp) {
  129. osDelay(1);
  130. if(search_sequence(g_usart4_rx_buf, USART4_REC_LEN, needle, strlen(needle))){
  131. temp = 1;
  132. }
  133. if(search_sequence(g_usart4_rx_buf, USART4_REC_LEN, "ERROR", strlen("ERROR"))){
  134. temp = 2;
  135. }
  136. if(timeOutCnt < REC_TIMEOUT){
  137. timeOutCnt++;
  138. }else{
  139. timeOutCnt = 0;
  140. temp = 3;
  141. }
  142. }
  143. g_usart4_rx_sta = 0;
  144. HAL_UART_DMAStop(&huart4);
  145. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  146. // __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE); // 关闭空闲中断
  147. return temp;
  148. }
  149. /**
  150. * @brief 接收使能函数
  151. * @note
  152. * @retval 无
  153. */
  154. void EC800_recEnable(void){
  155. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  156. __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
  157. while(g_usart4_rx_sta == 0) osDelay(1);
  158. }
  159. /**
  160. * @brief 提取检查信号响应字符串字段中的信号量并进行信号判断
  161. * @param data: 响应的信号量指针
  162. * @note
  163. * @retval 无
  164. */
  165. static uint8_t EC800_extractSignal(char *data){
  166. char *token;
  167. int field_1;
  168. // 使用strtok函数提取字段
  169. token = strtok(data, ":,");
  170. while (token != NULL) {
  171. field_1 = atoi(token); // 转换为整数类型
  172. token = strtok(NULL, ":,");
  173. memset(data, 0, USART4_REC_LEN); // 清除数据buff
  174. g_usart4_rx_sta = 0; // 清除接收状态
  175. if (field_1 < 31) {
  176. return 1;
  177. } else {
  178. return 0;
  179. }
  180. }
  181. return 0;
  182. }
  183. /**
  184. * @brief 获取IMSI号
  185. * @param 无
  186. * @note
  187. * @retval 1: imsi没有获取到 0;imsi获取到
  188. */
  189. uint8_t EC800_getIMSI(void){
  190. char* found = NULL;
  191. char number[16]; // 15 digits + 1 for null terminator
  192. int numberIndex = 0;
  193. int numberFound = 0;
  194. EC800M_SendCommand(AT_CIMI);
  195. EC800_recEnable();
  196. found = strstr(g_usart4_rx_buf, AT_RESP_OK);
  197. if (found != NULL) {
  198. // printf("IMSI is get\r\n");
  199. }else{
  200. // printf("IMSI is not get\r\n");
  201. return 1;
  202. }
  203. for (int i = 0; i < strlen(g_usart4_rx_buf); i++) {
  204. if (isdigit(g_usart4_rx_buf[i])) {
  205. number[numberIndex] = g_usart4_rx_buf[i];
  206. numberIndex++;
  207. if (numberIndex >= 15) {
  208. numberFound = 1;
  209. break;
  210. }
  211. }
  212. }
  213. number[numberIndex] = '\0';
  214. if (numberFound) {
  215. memcpy(s_messageDate.imsi, number, strlen(number));
  216. // printf("IMSI is: %s\n", number);
  217. } else {
  218. // printf("Error: Unable to extract the number\n");
  219. }
  220. return 0;
  221. }
  222. /**
  223. * @brief EC800M确认网络并链接MQTT服务器
  224. * @param NONE
  225. * @note
  226. * @retval 无
  227. */
  228. uint8_t linkStep = 0;
  229. void EC800M_link(void){
  230. uint8_t temp = 0;
  231. char command[100] = {0};
  232. char errrCnt = 0; // 错误计数
  233. switch(linkStep){
  234. case 0: // 基础配置
  235. EC800M_SendCommand(AT_CMD_TEST);
  236. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  237. // printf("Module status normal\r\n");
  238. }else{
  239. errrCnt++;
  240. // printf("Module status abnormal\r\n");
  241. }
  242. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  243. if(errrCnt == 0){
  244. linkStep = 1;
  245. }else{
  246. errrCnt = 0;
  247. }
  248. break;
  249. case 1: // 获取ISMI号
  250. errrCnt = EC800_getIMSI();
  251. if(errrCnt == 0){
  252. linkStep = 2;
  253. }else{
  254. errrCnt = 0;
  255. }
  256. break;
  257. case 2:
  258. EC800M_SendCommand(AT_CMD_ATE0);
  259. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  260. // printf("Cancel module echo\r\n");
  261. }else{
  262. errrCnt++;
  263. // printf("Failed to cancel module echo\r\n");
  264. }
  265. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  266. if(errrCnt == 0){
  267. linkStep = 3;
  268. }else{
  269. errrCnt = 0;
  270. }
  271. break;
  272. case 3:
  273. EC800M_SendCommand(AT_CMD_CPIN);
  274. if (Accept_and_Compare_Str(AT_RESP_CPIN_READY) == 1) {
  275. // printf("SIM card normal\r\n");
  276. }else{
  277. errrCnt++;
  278. // printf("SIM card abnormal\r\n");
  279. }
  280. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  281. if(errrCnt == 0){
  282. linkStep = 4;
  283. }else{
  284. errrCnt = 0;
  285. }
  286. break;
  287. case 4:
  288. EC800M_SendCommand(AT_CMD_CSQ);
  289. EC800_recEnable();
  290. temp = EC800_extractSignal(g_usart4_rx_buf);
  291. if (temp) {
  292. // printf("signal normal\r\n");
  293. }else{
  294. errrCnt++;
  295. // printf("signal abnormal\r\n");
  296. }
  297. if(errrCnt == 0){
  298. linkStep = 5;
  299. }else{
  300. errrCnt = 0;
  301. }
  302. break;
  303. case 5:
  304. EC800M_SendCommand(AT_CMD_CREG);
  305. if((Accept_and_Compare_Str("+CREG: 0,1") == 1) || (Accept_and_Compare_Str("+CREG: 0,5") == 1)){
  306. // printf("The module successfully registered on the GSM network\r\n");
  307. }else{
  308. errrCnt++;
  309. // printf("The module failed to register on the GSM network\r\n");
  310. }
  311. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  312. g_usart4_rx_sta = 0; // 清除接收状态
  313. if(errrCnt == 0){
  314. linkStep = 6;
  315. }else{
  316. errrCnt = 0;
  317. }
  318. break;
  319. case 6:
  320. EC800M_SendCommand(AT_CMD_QIDEACT);
  321. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  322. // printf("Successfully disabled mobile scene\r\n");
  323. }else{
  324. errrCnt++;
  325. // printf("Failed to disable mobile scene\r\n");
  326. }
  327. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  328. if(errrCnt == 0){
  329. linkStep = 7;
  330. }else{
  331. errrCnt = 0;
  332. }
  333. break;
  334. case 7:
  335. EC800M_SendCommand(AT_CMD_QIACT);
  336. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  337. // printf("Successfully enabled mobile scene\r\n");
  338. }else{
  339. errrCnt++;
  340. // printf("Failed to enable mobile scene\r\n");
  341. }
  342. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  343. if(errrCnt == 0){
  344. linkStep = 8;
  345. }else{
  346. errrCnt = 0;
  347. }
  348. break;
  349. case 8: // 设置MQTT心跳
  350. sprintf(command, "AT+QMTCFG=\"qmtping\",%d,%d\r\n", client_idx, 30);
  351. EC800M_SendCommand(command);
  352. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  353. // printf("MQTT heartbeat set successfully\r\n");
  354. }else{
  355. errrCnt++;
  356. // printf("Failed to set MQTT heartbeat\r\n");
  357. }
  358. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  359. if(errrCnt == 0){
  360. linkStep = 9;
  361. }else{
  362. errrCnt = 0;
  363. }
  364. break;
  365. case 9: // 设置MQTT接收数据的模式
  366. sprintf(command,"AT+QMTCFG=\"recv/mode\",%d,0,1\r\n", client_idx);
  367. EC800M_SendCommand(command);
  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: // 设置QOS为1至少发送1次
  382. // sprintf(command,"AT+QMTCFG=\"will\",%d,1,1,0,255,255\r\n", client_idx);
  383. // EC800M_SendCommand(command);
  384. //
  385. // if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  386. //// printf("Data receiving mode set successfully.\r\n");
  387. // }else{
  388. // errrCnt++;
  389. //// printf("Failed to set data receiving mode\r\n");
  390. // }
  391. //
  392. // memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  393. //
  394. // if(errrCnt == 0){
  395. linkStep = 11;
  396. // }else{
  397. // errrCnt = 0;
  398. // }
  399. break;
  400. case 11: // 为模块打开一个网络
  401. sprintf(command,"AT+QMTOPEN=0,\"%s\",%d\r\n", s_ec800Date.ip, s_ec800Date.port);
  402. EC800M_SendCommand(command);
  403. temp = Accept_and_Compare_Str(AT_RESP_QMTOPEN);
  404. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  405. if (temp == 1) {
  406. // printf("Successfully opened a network for the module\r\n");
  407. }else{
  408. errrCnt++;
  409. // printf("Failed to open a network for the module\r\n");
  410. }
  411. if(errrCnt == 0){
  412. linkStep = 12;
  413. }else{
  414. errrCnt = 0;
  415. }
  416. break;
  417. case 12: // 连接MQTT服务器
  418. sprintf(command,"AT+QMTCONN=0,%s,%s,%s\r\n",s_ec800Date.clientid, s_ec800Date.username, s_ec800Date.password);
  419. EC800M_SendCommand(command);
  420. if (Accept_and_Compare_Str(AT_RESP_QMTCONN) == 1) {
  421. // printf("Successfully connected to the MQTT server\r\n");
  422. }else{
  423. errrCnt++;
  424. // printf("Failed to connect to the MQTT server\r\n");
  425. }
  426. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  427. if(errrCnt == 0){
  428. linkStep = 13;
  429. }else{
  430. errrCnt = 0;
  431. }
  432. break;
  433. default: break;
  434. }
  435. }
  436. /**
  437. * @brief EC800M初始化GNSS
  438. * @param NONE
  439. * @note
  440. * @retval 无
  441. */
  442. uint8_t gnssStep = 0;
  443. void EC800_gnss_init(void){
  444. char errrCnt = 0; // 错误计数
  445. switch(gnssStep){
  446. case 6:
  447. EC800M_SendCommand(AT_QGPS_0);
  448. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  449. // printf("Turn off the GNSS module\r\n");
  450. }else{
  451. errrCnt++;
  452. // printf("Failed to turn off the GNSS module\r\n");
  453. }
  454. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  455. if(errrCnt == 0){
  456. gnssStep = 7;
  457. }else{
  458. gnssStep = 7;
  459. }
  460. break;
  461. case 7:
  462. EC800M_SendCommand(AT_QGPS_1);
  463. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  464. // printf("Turn on the GNSS module\r\n");
  465. }else{
  466. errrCnt++;
  467. // printf("Failed to turn on the GNSS module\r\n");
  468. }
  469. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  470. if(errrCnt == 0){
  471. gnssStep = 8;
  472. }else{
  473. errrCnt = 0;
  474. }
  475. break;
  476. case 0:
  477. EC800M_SendCommand(AT_QGPSCFG_out_port);
  478. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  479. // printf("Outputting NMEA Sentences via Serial Debugging\r\n");
  480. }else{
  481. errrCnt++;
  482. // printf("The NMEA sentence output is set incorrectly\r\n");
  483. }
  484. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  485. if(errrCnt == 0){
  486. gnssStep = 1;
  487. }else{
  488. errrCnt = 0;
  489. }
  490. break;
  491. case 1:
  492. EC800M_SendCommand(AT_QGPSCFG_nmeasrc);
  493. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  494. // printf("Enabling NMEA sentence retrieval via AT+QGPSGNMEA\r\n");
  495. }else{
  496. errrCnt++;
  497. // printf("Failed to enable retrieving NMEA sentences via AT+QGPSGNMEA\r\n");
  498. }
  499. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  500. if(errrCnt == 0){
  501. gnssStep = 2;
  502. }else{
  503. errrCnt = 0;
  504. }
  505. break;
  506. case 2:
  507. EC800M_SendCommand(AT_QGPSCFG_gpsnmeatype);
  508. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  509. // printf("Configuring the output type of NMEA sentences to all formats\r\n");
  510. }else{
  511. errrCnt++;
  512. // printf("Failed to configure the output type of NMEA sentences to all formats\r\n");
  513. }
  514. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  515. if(errrCnt == 0){
  516. gnssStep = 3;
  517. }else{
  518. errrCnt = 0;
  519. }
  520. break;
  521. case 3:
  522. EC800M_SendCommand(AT_QGPSCFG_gnssconfig);
  523. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  524. // printf("Configuring the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  525. }else{
  526. errrCnt++;
  527. // printf("Failed to configure the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  528. }
  529. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  530. if(errrCnt == 0){
  531. gnssStep = 4;
  532. }else{
  533. errrCnt = 0;
  534. }
  535. break;
  536. case 4:
  537. EC800M_SendCommand(AT_QGPSCFG_autogps);
  538. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  539. // printf("Disable GNSS auto-start\r\n");
  540. }else{
  541. errrCnt++;
  542. // printf("Failed to disable GNSS auto-start\r\n");
  543. }
  544. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  545. if(errrCnt == 0){
  546. gnssStep = 5;
  547. }else{
  548. errrCnt = 0;
  549. }
  550. break;
  551. case 5:
  552. EC800M_SendCommand(AT_QGPSCFG_apflash);
  553. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  554. // printf("Disable AP-Flash quick start function\r\n");
  555. }else{
  556. errrCnt++;
  557. // printf("Failed to disable AP-Flash quick start function\r\n");
  558. }
  559. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  560. if(errrCnt == 0){
  561. gnssStep = 6;
  562. }else{
  563. errrCnt = 0;
  564. }
  565. break;
  566. default:
  567. break;
  568. }
  569. }
  570. //UTC时间转换为任意时区时间,如果是转换为北京时间,timezone传8即可
  571. void utc_to_local_time(nmea_utc_time* utc_time, int8_t timezone, nmea_utc_time* local_time)
  572. {
  573. int year,month,day,hour;
  574. int lastday = 0; //last day of this month 本月天数
  575. int lastlastday = 0; //last day of last month 上个月天数
  576. year = utc_time->year; //utc time
  577. month = utc_time->month;
  578. day = utc_time->date;
  579. hour = utc_time->hour + timezone;
  580. //1月大,2月小,3月大,4月小,5月大,6月小,7月大,8月大,9月小,10月大,11月小,12月大
  581. if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
  582. {
  583. lastday = 31;//本月天数
  584. lastlastday = 30;//这里应该补上上个月的天数
  585. if(month == 3)
  586. {
  587. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year
  588. lastlastday = 29;
  589. else
  590. lastlastday = 28;
  591. }
  592. if(month == 8 || month == 1)//这里应该是8月和1月,因为8月和1月的上一个月(7月和12月)的天数是31天的
  593. lastlastday = 31;
  594. }
  595. else if(month == 4 || month == 6 || month == 9 || month == 11)
  596. {
  597. lastday = 30;
  598. lastlastday = 31;
  599. }
  600. else
  601. {
  602. lastlastday = 31;
  603. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
  604. lastday = 29;
  605. else
  606. lastday = 28;
  607. }
  608. if(hour >= 24)// if >24, day+1
  609. {
  610. hour -= 24;
  611. day += 1;
  612. if(day > lastday)// next month, day-lastday of this month
  613. {
  614. day -= lastday;
  615. month += 1;
  616. if(month > 12)// next year, month-12
  617. {
  618. month -= 12;
  619. year += 1;
  620. }
  621. }
  622. }
  623. if(hour < 0)// if <0, day-1
  624. {
  625. hour += 24;
  626. day -= 1;
  627. if(day < 1)// month-1, day=last day of last month
  628. {
  629. day = lastlastday;
  630. month -= 1;
  631. if(month < 1)// last year, month=12
  632. {
  633. month = 12;
  634. year -= 1;
  635. }
  636. }
  637. }
  638. // transfer value to local_time
  639. local_time->year = year;
  640. local_time->month = month;
  641. local_time->date = day;
  642. local_time->hour = hour;
  643. local_time->min = utc_time->min;
  644. local_time->sec = utc_time->sec;
  645. }
  646. /**
  647. * @brief 常用时间格式转时间戳
  648. * @param NONE
  649. * @note 要用utc时间进行转换,如果输入为北京时间的话,需要 hour-8;
  650. * @retval 无
  651. */
  652. uint32_t EC800_mktime (unsigned int year, unsigned int mon,
  653. unsigned int day, unsigned int hour,
  654. unsigned int min, unsigned int sec)
  655. {
  656. if (0 >= (int) (mon -= 2)){ /**//* 1..12 -> 11,12,1..10 */
  657. mon += 12; /**//* Puts Feb last since it has leap day */
  658. year -= 1;
  659. }
  660. return (((
  661. (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  662. year*365 - 719499
  663. )*24 + hour /**//* now have hours */
  664. )*60 + min /**//* now have minutes */
  665. )*60 + sec; /**//* finally seconds */
  666. }
  667. /**
  668. * @brief 读取GNSS的数据
  669. * @param NONE
  670. * @note
  671. * @retval 0:接收失败 1:接收成功
  672. */
  673. uint8_t EC800_readGnssDate(void){
  674. EC800M_SendCommand(AT_QGPSGNMEA_RMC);
  675. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  676. // printf("Location information received successfully\r\n");
  677. }else{
  678. // printf("Location information reception failed\r\n");
  679. return 0;
  680. }
  681. // printf("Location information:%s\r\n", g_usart4_rx_buf);
  682. NMEA_GNRMC_Analysis(&s_nmea_msg, (uint8_t*)g_usart4_rx_buf);
  683. utc_to_local_time(&(s_nmea_msg.utc), 8, &s_nmea_utc_time);
  684. // 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);
  685. 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);
  686. g_usart4_rx_sta= 0;
  687. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  688. // 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);
  689. /* 尝试获取互斥量,等待无限长时间 */
  690. if(osMutexAcquire(s_messageDate_locationHandle, osWaitForever) == osOK)
  691. {
  692. /* 安全地访问s_rs485RecDate结构体 */
  693. s_messageDate.latitude = s_nmea_msg.latitude;
  694. s_messageDate.nshemi = s_nmea_msg.nshemi;
  695. s_messageDate.longitude = s_nmea_msg.longitude;
  696. s_messageDate.ewhemi = s_nmea_msg.ewhemi;
  697. /* 访问完成,释放互斥量 */
  698. osMutexRelease(s_messageDate_locationHandle);
  699. }
  700. return 1;
  701. }
  702. /**
  703. * @brief EC800M订阅主题
  704. * @param topic1: 订阅的主题名称1字符串形式
  705. * @param topic2: 订阅的主题名称2字符串形式
  706. * @note
  707. * @retval 无
  708. */
  709. uint8_t EC800_subscribeToTopic(const char* topic1){
  710. char command[100] = {0};
  711. uint8_t recTemp = 0;
  712. sprintf(command, "AT+QMTSUB=0,1,\"%s\",0\r\n", topic1);
  713. EC800M_SendCommand(command);
  714. recTemp = Accept_and_Compare_Str(AT_RESP_QMTSUB);
  715. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  716. if (recTemp == 1) {
  717. // printf("Subscription to the topic successful\r\n");
  718. return 1;
  719. }else if (recTemp == 2){
  720. // printf("Subscription to the topic failed\r\n");
  721. return 0;
  722. }
  723. return 0;
  724. }
  725. /**
  726. * @brief EC800M发布消息
  727. * @param topic: 消息发布的主题 字符串形式
  728. * @param message: 发布的消息 字符串形式
  729. * @param len: 发布消息的字节长度
  730. * @note
  731. * @retval 无
  732. */
  733. uint8_t EC800_publishMessage(const char* topic, const char* message, uint16_t len){
  734. char command[100] = {0};
  735. char* responseCmd = ">";
  736. static uint8_t publishStep = 0;
  737. uint8_t recBack = 0;
  738. switch(publishStep){
  739. case 0:
  740. sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
  741. EC800M_SendCommand(command);
  742. if (Accept_and_Compare_Str(responseCmd) == 1) {
  743. // printf("The topic has been linked\r\n");
  744. publishStep = 1;
  745. }else{
  746. // printf("The topic link failed\r\n");
  747. publishStep = 0;
  748. }
  749. // break;
  750. case 1:
  751. EC800M_SendCommand(message);
  752. recBack = Accept_and_Compare_Str(AT_RESP_QMTPUBEX);
  753. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  754. if (recBack == 1) {
  755. publishStep = 0;
  756. // printf("The message has been successfully posted\r\n");
  757. }else{
  758. // printf("Message publication failed\r\n");
  759. publishStep = 0;
  760. }
  761. break;
  762. default:
  763. break;
  764. }
  765. return recBack;
  766. }
  767. // 将uint8_t数组中的ASCII码数据转换为字符串的函数
  768. void uint8ArrayToAsciiString(const uint8_t* array, size_t length, char* str) {
  769. for (size_t i = 0; i < length; ++i) {
  770. str[i] = (char)array[i]; // 直接将每个字节转换为字符
  771. }
  772. str[length] = '\0'; // 添加字符串结束符
  773. }
  774. /**
  775. * @brief 登录 topic
  776. * @param NONE
  777. * @note topic 101
  778. * @retval 无
  779. */
  780. uint8_t Login_Topic(void){
  781. cJSON *root = NULL;
  782. char *jsonString = NULL;
  783. uint8_t temp = 0;
  784. char command[50] = {0};
  785. // 定义对象 { }
  786. root = cJSON_CreateObject();
  787. // 插入元素,对应 键值对
  788. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  789. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  790. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  791. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  792. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  793. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  794. memset(command, 0, 50);
  795. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  796. cJSON_AddItemToObject(root, "hardwareId", cJSON_CreateString(command));
  797. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  798. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  799. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  800. // 将 JSON 对象转换为字符串
  801. jsonString = cJSON_Print(root);
  802. cJSON_Delete(root);
  803. temp = EC800_publishMessage("toServer/mqtt/101", jsonString, strlen(jsonString));
  804. free(jsonString);
  805. return temp;
  806. }
  807. /**
  808. * @brief 下位机请求围栏数据
  809. * @param NONE
  810. * @note topic 604
  811. * @retval 无
  812. */
  813. uint8_t requesting_Fence_Data(void){
  814. cJSON *root = NULL;
  815. char *jsonString = NULL;
  816. uint8_t temp = 0;
  817. char command[50] = {0};
  818. // 定义对象 { }
  819. root = cJSON_CreateObject();
  820. // 插入元素,对应 键值对
  821. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  822. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  823. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  824. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  825. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  826. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  827. memset(command, 0, 50);
  828. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  829. cJSON_AddItemToObject(root, "vin", cJSON_CreateString(command));
  830. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  831. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  832. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  833. // 将 JSON 对象转换为字符串
  834. jsonString = cJSON_Print(root);
  835. cJSON_Delete(root);
  836. temp = EC800_publishMessage("toServer/mqtt/604", jsonString, strlen(jsonString));
  837. free(jsonString);
  838. return temp;
  839. }
  840. /**
  841. * @brief 创建单个报警项的JSON对象
  842. * @param NONE
  843. * @note topic toServer/mqtt/501
  844. * @retval 无
  845. */
  846. cJSON* createAlarmItem(const char* id, uint32_t alarmTime, const char* alarmDesc, int alarmFlag) {
  847. cJSON* alarmItem = cJSON_CreateObject();
  848. cJSON_AddStringToObject(alarmItem, "id", id);
  849. cJSON_AddNumberToObject(alarmItem, "alarmTime", alarmTime);
  850. cJSON_AddStringToObject(alarmItem, "alarmDesc", alarmDesc);
  851. cJSON_AddNumberToObject(alarmItem, "alarmFlag", alarmFlag);
  852. return alarmItem;
  853. }
  854. /**
  855. * @brief 根据Malfunction的值插入故障元素
  856. * @param NONE
  857. * @note topic toServer/mqtt/501
  858. * @retval 无
  859. */
  860. void insertFaultItems(cJSON* alarmList, uint32_t Malfunction, uint32_t fault_timeStamp) {
  861. for (int i = 0; i < 16; i++) {
  862. if (Malfunction & (1 << i)) {
  863. // 确保i在数组的有效范围内
  864. if (i < 8) {
  865. cJSON_AddItemToArray(alarmList, createAlarmItem(faults[i].errid, fault_timeStamp, faults[i].errorInfo, 1));
  866. }
  867. }
  868. }
  869. }
  870. /**
  871. * @brief 故障数据上传
  872. * @param NONE
  873. * @note topic toServer/mqtt/501
  874. * @retval 无
  875. */
  876. uint8_t EC800_uploadFaultReport(uint32_t fault_time_stamp, uint32_t fault_Code){
  877. cJSON *root = NULL;
  878. char *jsonString = NULL;
  879. uint8_t temp = 0;
  880. char command[50] = {0};
  881. // 定义对象 { }
  882. root = cJSON_CreateObject();
  883. // 插入元素,对应 键值对
  884. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  885. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  886. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  887. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  888. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  889. // 插入故障元素
  890. cJSON* alarmList = cJSON_CreateArray();
  891. insertFaultItems(alarmList, fault_Code, fault_time_stamp);
  892. cJSON_AddItemToObject(root, "alarmList", alarmList);
  893. // 将 JSON 对象转换为字符串
  894. jsonString = cJSON_Print(root);
  895. cJSON_Delete(root);
  896. temp = EC800_publishMessage("toServer/mqtt/501", jsonString, strlen(jsonString));
  897. free(jsonString);
  898. return temp;
  899. }
  900. /**
  901. * @brief EC800M与服务器进行时间对时
  902. * @note
  903. * @retval 无
  904. */
  905. uint8_t EC800_time_calibration(void){
  906. cJSON *root = NULL;
  907. char *jsonString = NULL;
  908. uint8_t temp = 0;
  909. char command[50] = {0};
  910. // 定义对象 { }
  911. root = cJSON_CreateObject();
  912. // 插入元素,对应 键值对
  913. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  914. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  915. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  916. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  917. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  918. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  919. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  920. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  921. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  922. // 将 JSON 对象转换为字符串
  923. jsonString = cJSON_Print(root);
  924. cJSON_Delete(root);
  925. temp = EC800_publishMessage("toServer/mqtt/105", jsonString, strlen(jsonString));
  926. free(jsonString);
  927. return temp;
  928. }
  929. /**
  930. * @brief 将服务器响应的字符串中的年月日进行分离
  931. * @note
  932. * @retval 无
  933. */
  934. void parseServerTime(char* str){
  935. char year[5];
  936. char month[3];
  937. char day[3];
  938. char hour[3];
  939. char minute[3];
  940. char second[3];
  941. strncpy(year, str, 4);
  942. year[4] = '\0';
  943. strncpy(month, str + 4, 2);
  944. month[2] = '\0';
  945. strncpy(day, str + 6, 2);
  946. day[2] = '\0';
  947. strncpy(hour, str + 8, 2);
  948. hour[2] = '\0';
  949. strncpy(minute, str + 10, 2);
  950. minute[2] = '\0';
  951. strncpy(second, str + 12, 2);
  952. second[2] = '\0';
  953. s_recDate.year = atoi(year);
  954. s_recDate.month = atoi(month);
  955. s_recDate.day = atoi(day);
  956. s_recDate.hour = atoi(hour);
  957. s_recDate.minute = atoi(minute);
  958. s_recDate.sec = atoi(second);
  959. s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour - 8,s_recDate.minute,s_recDate.sec);
  960. // printf("Year (int): %d\n", s_recDate.year);
  961. // printf("Month (int): %d\n", s_recDate.month);
  962. // printf("Day (int): %d\n", s_recDate.day);
  963. // printf("Hour (int): %d\n", s_recDate.hour);
  964. // printf("Minute (int): %d\n", s_recDate.minute);
  965. // printf("Second (int): %d\n", s_recDate.sec);
  966. }
  967. /**
  968. * @brief 解析对时数据帧的响应
  969. * @note
  970. * @retval 无
  971. */
  972. void EC800_parseRespondTime(cJSON *root, cJSON *item){
  973. uint8_t temp = 0;
  974. // 获取"resultCode"字段的值
  975. item = cJSON_GetObjectItem(root, "resultCode");
  976. if (item == NULL) {
  977. // printf("Field \"resultCode\" not found\n");
  978. }else{
  979. // 打印"resultCode"字段的值
  980. // printf("resultCode: %d\n", item->valueint);
  981. if((item->valueint) == 1){
  982. // printf("The upload of synchronized data was successful.\n");
  983. temp = 1; // 上传成功
  984. }else{
  985. // printf("The upload of synchronized data has failed.\n");
  986. }
  987. }
  988. if(temp == 1){
  989. item = cJSON_GetObjectItem(root, "serverTime");
  990. if (item == NULL) {
  991. // printf("Field \"serverTime\" not found\n");
  992. }else{
  993. // 打印"serverTime"字段的值
  994. // printf("serverTime: %s\n", item->valuestring);
  995. parseServerTime(item->valuestring);
  996. }
  997. }
  998. }
  999. /**
  1000. * @brief EC800M上传实时数据
  1001. * @note
  1002. * @retval 无
  1003. */
  1004. uint8_t EC800_uploadRealDate(void){
  1005. cJSON *root = NULL;
  1006. char *jsonString = NULL;
  1007. cJSON *rt000 = NULL;
  1008. cJSON *rt000Object1 = NULL;
  1009. char command[50] = {0};
  1010. uint32_t temp = 0; // 临时存储
  1011. // 定义对象 { }
  1012. root = cJSON_CreateObject();
  1013. //
  1014. // 插入元素,对应 键值对
  1015. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  1016. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  1017. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  1018. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  1019. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  1020. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  1021. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  1022. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  1023. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  1024. // 定义 { } 对象
  1025. rt000Object1 = cJSON_CreateObject();
  1026. cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
  1027. cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber((float)s_messageDate.demandVol / 10));
  1028. cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber((float)s_messageDate.demandCur / 10));
  1029. memset(command, 0, 50);
  1030. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  1031. cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString(command));
  1032. double point1Lat = (double)s_messageDate.latitude / 100000;
  1033. double point1Lng = (double)s_messageDate.longitude / 100000;
  1034. memset(command, 0, 50);
  1035. snprintf(command, sizeof(command), "%.5f,%.5f", point1Lng, point1Lat);
  1036. cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(command));
  1037. cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
  1038. cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
  1039. cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(s_messageDate.dailyDrivTime >> 8));
  1040. cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage / 10));
  1041. temp = (s_messageDate.accTotalDrivTime_day * 24) + (s_messageDate.accTotalDrivTime_h_min >> 8);
  1042. cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(temp));
  1043. cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
  1044. temp = 0;
  1045. temp = (s_messageDate.runTime >> 8) * 60 + (s_messageDate.runTime & 0x00ff);
  1046. cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(temp));
  1047. // test use 发送转弯标志
  1048. // cJSON_AddItemToObject(rt000Object1, "rt036", cJSON_CreateNumber(turnFlag));
  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. static uint8_t infoUploadError = 0; // 信息上传错误计数
  1406. switch(stateStep){
  1407. case 0: // 初始化模块
  1408. interCnt++;
  1409. EC800M_link();
  1410. // 初始化连接MQTT失败
  1411. if(interCnt > 15){
  1412. interCnt = 16;
  1413. p_global_par->InitFaultFlag_4G = 1;
  1414. }
  1415. // 初始化成功
  1416. if(linkStep == 13){
  1417. interCnt = 0; // 清除进入计数
  1418. p_global_par->InitFaultFlag_4G = 0;
  1419. stateStep = 1;
  1420. p_ec800Date->ec800InitFlag = 1; // 初始化完成
  1421. }else{
  1422. p_ec800Date->ec800InitFlag = 0; // 初始化未完成
  1423. }
  1424. break;
  1425. case 1: // 初始化GNSS
  1426. interCnt++;
  1427. EC800_gnss_init();
  1428. // 初始化GNSS失败
  1429. if(interCnt > 11){
  1430. interCnt = 16;
  1431. p_global_par->InitFaultFlag_4G = 1;
  1432. }
  1433. if(gnssStep == 8){
  1434. stateStep = 2;
  1435. interCnt = 0; // 清除进入计数
  1436. p_global_par->InitFaultFlag_4G = 0;
  1437. }
  1438. break;
  1439. case 2: // 订阅主题
  1440. sprintf(command, "toclient/%d/%x%x%x", DEV_TYPE, s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  1441. right = EC800_subscribeToTopic(command);
  1442. if(right != 1){ // 发布消息失败,可能断开链接
  1443. if(interCnt < 3){
  1444. interCnt++;
  1445. }else{
  1446. interCnt = 16;
  1447. p_global_par->InitFaultFlag_4G = 1;
  1448. }
  1449. }else{
  1450. interCnt = 0; // 清除进入计数
  1451. p_global_par->InitFaultFlag_4G = 0;
  1452. stateStep = 3;
  1453. }
  1454. break;
  1455. case 3: // 登录服务器主题
  1456. right = Login_Topic();
  1457. if(right == 1){
  1458. respondSt = 1; // 等待响应
  1459. stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
  1460. }
  1461. break;
  1462. case 4: // 下位机请求围栏数据
  1463. if(s_comData.vinRecSuccess == 1){ // 成功接收到车辆vin
  1464. right = requesting_Fence_Data();
  1465. if(right == 1){
  1466. respondSt = 1; // 等待响应
  1467. stateStep = 8; // 等待围栏数据响应
  1468. }
  1469. }else{ // 初始化时与车通信故障 跳转上传实时信息,上报故障,跳过围栏获取
  1470. if(BIT_CHECK(s_comData.Malfunction, com485)){
  1471. stateStep = 5;
  1472. }
  1473. }
  1474. // 后台要求重新请求围栏数据,请求完成,清除标志
  1475. if(p_global_par->fence_update == 1){
  1476. p_global_par->fence_update = 0;
  1477. }
  1478. break;
  1479. case 5: // 与服务器对时
  1480. right = EC800_time_calibration();
  1481. if(right == 1){
  1482. respondSt = 1; // 等待响应
  1483. stateStep = 8; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
  1484. }
  1485. break;
  1486. case 6:
  1487. // 获取定位信息
  1488. right = EC800_readGnssDate();
  1489. // 时间数据是否获取到
  1490. s_global_par.time_stamp_flag = (right == 1);
  1491. // 没有获取到定位信息 重复获取三次,网络存在,但gps信息不存在的情况
  1492. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  1493. if(p_global_par->positionErrorCnt < 3){
  1494. p_global_par->positionErrorCnt++;
  1495. }else{
  1496. p_global_par->positionErrorCnt = 3;
  1497. stateStep = 7;
  1498. }
  1499. }else{
  1500. p_global_par->positionErrorCnt = 0;
  1501. stateStep = 7;
  1502. }
  1503. break;
  1504. case 7: // 发布消息
  1505. right = EC800_uploadRealDate();
  1506. if(right == 0){ // 上传信息失败
  1507. if(infoUploadError < task_upload_times_2s){
  1508. infoUploadError++;
  1509. }else{ // 2s未发出信息 尝试重连
  1510. infoUploadError = 0;
  1511. s_global_par.reconnect_server_flag = 1;
  1512. }
  1513. }else if(right == 1){
  1514. infoUploadError = 0;
  1515. stateStep = 8;
  1516. }
  1517. break;
  1518. case 8: // 接收消息
  1519. // 转弯行驶中
  1520. if(turnFlag == 1){
  1521. if(timesCnt < TURN_INTERVAL_TIME_MS){
  1522. timesCnt++;
  1523. }else{
  1524. timesCnt= 0;
  1525. stateStep = 6;
  1526. HAL_UART_DMAStop(&huart4);
  1527. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1528. }
  1529. }else{ // 正常行驶中
  1530. if(timesCnt < PUBLISH_TIME_MS){
  1531. timesCnt++;
  1532. }else{
  1533. timesCnt= 0;
  1534. stateStep = 6;
  1535. HAL_UART_DMAStop(&huart4);
  1536. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1537. }
  1538. }
  1539. // 后台要求重新请求围栏数据或者初始化获取到vin请求围栏数据
  1540. if((p_global_par->fence_update == 1) || ((s_comData.vinRecSuccess == 1) && (p_ec800Date->fenceRecSuccess == 0))){
  1541. stateStep = 4;
  1542. }
  1543. // 通信超时判断
  1544. if(respondSt == 1){ // 等待响应状态
  1545. if(p_global_par->mqttTimeoutCnt < PUBLISH_TIME_MS){
  1546. p_global_par->mqttTimeoutCnt ++;
  1547. }else{
  1548. p_global_par->mqttTimeoutCnt = PUBLISH_TIME_MS;
  1549. }
  1550. }else{
  1551. p_global_par->mqttTimeoutCnt = 0;
  1552. }
  1553. // 当MQTT通信异常且gnss回复错误的情况下,4g模块可能断线,重新连接4G模块
  1554. // stateStep = 0;
  1555. // linkStep = 0;
  1556. // gnssStep = 0;
  1557. break;
  1558. default:
  1559. break;
  1560. }
  1561. }
  1562. /**
  1563. * @brief 解析MQTT 链路层状态变化的 URC
  1564. * @note +QMTSTAT: <client_idx>,<err_code> err_code不为0,则重新连接
  1565. * @retval 无
  1566. */
  1567. void parse_mqttUrc(void){
  1568. int val1, val2 = 0;
  1569. // 检查字符串是否以"+QMTSTAT: "开头
  1570. if (strncmp(g_usart4_rx_buf, "+QMTSTAT: ", 10) == 0) {
  1571. // 使用sscanf从字符串中解析两个整数
  1572. if (sscanf(g_usart4_rx_buf + 10, "%d,%d", &val1, &val2) == 2) {
  1573. // 如果成功解析出两个整数,则判断
  1574. if(val1 != client_idx){
  1575. return;
  1576. }
  1577. if(val2 != 0){
  1578. // 需重新连接服务器
  1579. s_global_par.reconnect_server_flag = 1;
  1580. s_param_boot.reconnect++; // test use
  1581. Write_paramArea();
  1582. }
  1583. }
  1584. }
  1585. // 如果字符串不符合格式或解析失败,则返回
  1586. return ;
  1587. }
  1588. /**
  1589. * @brief 判断是否执行重连MQTT服务器
  1590. * @note
  1591. * @retval 无
  1592. */
  1593. void reconnect_mqtt_server(void){
  1594. // 重连MQTT服务器
  1595. if(s_global_par.reconnect_server_flag == 1){
  1596. s_global_par.reconnect_server_flag = 0;
  1597. stateStep = 0;
  1598. linkStep = 0;
  1599. gnssStep = 0;
  1600. }
  1601. }
  1602. /**
  1603. * @brief MQTT服务器数据接收处理
  1604. * @note
  1605. * @retval 无
  1606. */
  1607. void handleReceive(void) {
  1608. uint16_t right = 1; // 返回是否为正确
  1609. if(g_usart4_rx_sta!=0){
  1610. // HAL_UART_Transmit(&huart1, (uint8_t*)g_usart4_rx_buf, strlen(g_usart4_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  1611. parse_mqttUrc(); // 解析MQTT 链路层状态变化的 URC
  1612. reconnect_mqtt_server(); // 判断是否执行重连MQTT服务器
  1613. right = EC800_respondParse(&s_ec800Date);
  1614. if((right == CTR_CODE_LOGIN) && (s_messageDate.loginResult == 1)){ // 登录响应
  1615. stateStep = 4;
  1616. right = 0;
  1617. timesCnt = 0;
  1618. respondSt = 2; // 成功响应
  1619. }else if(right == CTR_CODE_LOGIN){ // 登录响应 未登录成功
  1620. stateStep = 3;
  1621. right = 0;
  1622. timesCnt = 0;
  1623. respondSt = 2; // 成功响应
  1624. }else if(right == CTR_CODE_FENCES){ // 围栏数据响应
  1625. stateStep = 5;
  1626. right = 0;
  1627. timesCnt = 0;
  1628. respondSt = 2; // 成功响应
  1629. }
  1630. else if(right == CTR_CODE_JUDETIME){ // 对时响应
  1631. stateStep = 6;
  1632. right = 0;
  1633. // timesCnt = 0;
  1634. respondSt = 2; // 成功响应
  1635. }else if((right == CTR_CODE_REMOTE) && (s_ec800Date.hardwareUpdate == 1)){ // 固件更新
  1636. stateStep = 0xff; // 停止占用4G模块
  1637. }
  1638. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff,接收新的数据
  1639. g_usart4_rx_sta = 0; // 清除接收状态
  1640. HAL_UART_DMAStop(&huart4);
  1641. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1642. }
  1643. }