EC800(1208).c 40 KB

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