EC800(3516).c 48 KB

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