EC800(1603).c 39 KB

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