EC800(1391).c 41 KB

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