EC800(5626).c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. };
  13. messageDate s_messageDate = {0};
  14. typedef struct{
  15. uint8_t maxSpeed; // 服务器下发的允许最大车速
  16. uint16_t year; // 上传时间 年
  17. uint8_t month; // 上传时间 月
  18. uint8_t day; // 上传时间 日
  19. uint8_t hour; // 上传时间 时
  20. uint8_t minute; // 上传时间 分
  21. uint8_t sec; // 上传时间 秒
  22. }recDate;
  23. recDate s_recDate = {0};
  24. nmea_msg s_nmea_msg = {0};
  25. nmea_utc_time s_nmea_utc_time = {0};
  26. // AT指令响应超时时间定义
  27. #define REC_TIMEOUT (10000) // 1ms
  28. /**
  29. * @brief 计算字符串的长度
  30. * @param str: 所需计算字符串的指针
  31. * @note
  32. * @retval 无
  33. */
  34. int EC800_calculateStringLength(const char* str) {
  35. int length = 0;
  36. while (str[length] != '\0') {
  37. length++;
  38. }
  39. return length;
  40. }
  41. /**
  42. * @brief 发送指令函数
  43. * @param command: 指令
  44. * @param enterNum: 进入函数次数,只有第一次的时候清除buff
  45. * @note
  46. * @retval 无
  47. */
  48. void EC800M_SendCommand(const char* command)
  49. {
  50. uint32_t stringLen = 0;
  51. stringLen = EC800_calculateStringLength(command);
  52. //等待发送状态OK
  53. while(HAL_DMA_GetState(&hdma_usart3_tx) == HAL_DMA_STATE_BUSY) osDelay(1);
  54. //发送数据
  55. HAL_UART_Transmit_DMA(&huart3, (uint8_t*)command, stringLen);
  56. }
  57. /**
  58. * @brief 接收指令回复函数
  59. * @param haystack: 接收的字符串数据
  60. * @param needle: 正确回复的数据
  61. * @note
  62. * @retval 无
  63. */
  64. char* EC800M_RecRespond(char *haystack, const char *needle ){
  65. char* p = NULL;
  66. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  67. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  68. while(g_usart3_rx_sta == 0) osDelay(1);
  69. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE); // 关闭空闲中断
  70. g_usart3_rx_sta= 0;
  71. p = strstr(haystack, needle);
  72. memset(haystack, 0, USART3_REC_LEN); // 清除数据buff
  73. return p;
  74. }
  75. // 自定义的搜索函数
  76. void* search_sequence(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len) {
  77. const unsigned char* h = (const unsigned char*)haystack;
  78. const unsigned char* n = (const unsigned char*)needle;
  79. // 如果needle为空或haystack长度小于needle长度,则直接返回NULL
  80. if (needle_len == 0 || haystack_len < needle_len) {
  81. return NULL;
  82. }
  83. for (size_t i = 0; i <= haystack_len - needle_len; ++i) {
  84. size_t j;
  85. for (j = 0; j < needle_len; ++j) {
  86. if (h[i + j] != n[j]) {
  87. break; // 如果当前字符不匹配,则跳出内层循环
  88. }
  89. }
  90. if (j == needle_len) {
  91. return (void*)(h + i); // 找到匹配的序列,返回其在haystack中的位置
  92. }
  93. }
  94. // 如果遍历了整个haystack都没有找到匹配的序列,则返回NULL
  95. return NULL;
  96. }
  97. // 接收并比较响应字符串
  98. uint8_t Accept_and_Compare_Str(const char* needle){
  99. uint8_t temp = 0;
  100. static uint16_t timeOutCnt = 0; // 超时计数
  101. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  102. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  103. while(!temp) {
  104. osDelay(1);
  105. if(search_sequence(g_usart3_rx_buf, USART3_REC_LEN, needle, strlen(needle))){
  106. temp = 1;
  107. }
  108. if(search_sequence(g_usart3_rx_buf, USART3_REC_LEN, "ERROR", strlen("ERROR"))){
  109. temp = 2;
  110. }
  111. if(timeOutCnt < REC_TIMEOUT){
  112. timeOutCnt++;
  113. }else{
  114. timeOutCnt = 0;
  115. temp = 3;
  116. }
  117. }
  118. g_usart3_rx_sta = 0;
  119. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE); // 关闭空闲中断
  120. return temp;
  121. }
  122. /**
  123. * @brief 接收使能函数
  124. * @note
  125. * @retval 无
  126. */
  127. void EC800_recEnable(void){
  128. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  129. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  130. while(g_usart3_rx_sta == 0) osDelay(1);
  131. }
  132. /**
  133. * @brief 提取检查信号响应字符串字段中的信号量并进行信号判断
  134. * @param data: 响应的信号量指针
  135. * @note
  136. * @retval 无
  137. */
  138. static uint8_t EC800_extractSignal(char *data){
  139. char *token;
  140. int field_1;
  141. // 使用strtok函数提取字段
  142. token = strtok(data, ":,");
  143. while (token != NULL) {
  144. field_1 = atoi(token); // 转换为整数类型
  145. token = strtok(NULL, ":,");
  146. memset(data, 0, USART3_REC_LEN); // 清除数据buff
  147. g_usart3_rx_sta = 0; // 清除接收状态
  148. if (field_1 < 31) {
  149. return 1;
  150. } else {
  151. return 0;
  152. }
  153. }
  154. return 0;
  155. }
  156. /**
  157. * @brief 获取IMSI号
  158. * @param 无
  159. * @note
  160. * @retval 1: imsi没有获取到 0;imsi获取到
  161. */
  162. uint8_t EC800_getIMSI(void){
  163. char* found = NULL;
  164. char number[16]; // 15 digits + 1 for null terminator
  165. int numberIndex = 0;
  166. int numberFound = 0;
  167. EC800M_SendCommand(AT_CIMI);
  168. EC800_recEnable();
  169. found = strstr(g_usart3_rx_buf, AT_RESP_OK);
  170. if (found != NULL) {
  171. printf("IMSI is get\r\n");
  172. }else{
  173. printf("IMSI is not get\r\n");
  174. return 1;
  175. }
  176. for (int i = 0; i < strlen(g_usart3_rx_buf); i++) {
  177. if (isdigit(g_usart3_rx_buf[i])) {
  178. number[numberIndex] = g_usart3_rx_buf[i];
  179. numberIndex++;
  180. if (numberIndex >= 15) {
  181. numberFound = 1;
  182. break;
  183. }
  184. }
  185. }
  186. number[numberIndex] = '\0';
  187. if (numberFound) {
  188. memcpy(s_messageDate.imsi, number, strlen(number));
  189. printf("IMSI is: %s\n", number);
  190. } else {
  191. printf("Error: Unable to extract the number\n");
  192. }
  193. return 0;
  194. }
  195. /**
  196. * @brief EC800M确认网络并链接MQTT服务器
  197. * @param NONE
  198. * @note
  199. * @retval 无
  200. */
  201. uint8_t linkStep = 0;
  202. void EC800M_link(void){
  203. uint8_t temp = 0;
  204. char command[100] = {0};
  205. char errrCnt = 0; // 错误计数
  206. char* found = NULL;
  207. switch(linkStep){
  208. case 0: // 基础配置
  209. EC800M_SendCommand(AT_CMD_TEST);
  210. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  211. if (found != NULL) {
  212. printf("Module status normal\r\n");
  213. }else{
  214. errrCnt++;
  215. printf("Module status abnormal\r\n");
  216. }
  217. if(errrCnt == 0){
  218. linkStep = 1;
  219. }else{
  220. errrCnt = 0;
  221. }
  222. break;
  223. case 1: // 获取ISMI号
  224. errrCnt = EC800_getIMSI();
  225. if(errrCnt == 0){
  226. linkStep = 2;
  227. }else{
  228. errrCnt = 0;
  229. }
  230. break;
  231. case 2:
  232. EC800M_SendCommand(AT_CMD_ATE0);
  233. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  234. if (found != NULL) {
  235. printf("Cancel module echo\r\n");
  236. }else{
  237. errrCnt++;
  238. printf("Failed to cancel module echo\r\n");
  239. }
  240. if(errrCnt == 0){
  241. linkStep = 3;
  242. }else{
  243. errrCnt = 0;
  244. }
  245. break;
  246. case 3:
  247. EC800M_SendCommand(AT_CMD_CPIN);
  248. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_CPIN_READY);
  249. if (found != NULL) {
  250. printf("SIM card normal\r\n");
  251. }else{
  252. errrCnt++;
  253. printf("SIM card abnormal\r\n");
  254. }
  255. if(errrCnt == 0){
  256. linkStep = 4;
  257. }else{
  258. errrCnt = 0;
  259. }
  260. break;
  261. case 4:
  262. EC800M_SendCommand(AT_CMD_CSQ);
  263. EC800_recEnable();
  264. temp = EC800_extractSignal(g_usart3_rx_buf);
  265. if (temp) {
  266. printf("signal normal\r\n");
  267. }else{
  268. errrCnt++;
  269. printf("signal abnormal\r\n");
  270. }
  271. if(errrCnt == 0){
  272. linkStep = 5;
  273. }else{
  274. errrCnt = 0;
  275. }
  276. break;
  277. case 5:
  278. EC800M_SendCommand(AT_CMD_CREG);
  279. EC800_recEnable();
  280. // 使用字符串处理函数判断是否为正常状态
  281. if (strstr(g_usart3_rx_buf, "1") != NULL || strstr(g_usart3_rx_buf, "5") != NULL) {
  282. printf("The module successfully registered on the GSM network\r\n");
  283. } else {
  284. errrCnt++;
  285. printf("The module failed to register on the GSM network\r\n");
  286. }
  287. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff
  288. g_usart3_rx_sta = 0; // 清除接收状态
  289. if(errrCnt == 0){
  290. linkStep = 6;
  291. }else{
  292. errrCnt = 0;
  293. }
  294. break;
  295. case 6:
  296. EC800M_SendCommand(AT_CMD_QIDEACT);
  297. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  298. if (found != NULL) {
  299. printf("Successfully disabled mobile scene\r\n");
  300. }else{
  301. errrCnt++;
  302. printf("Failed to disable mobile scene\r\n");
  303. }
  304. if(errrCnt == 0){
  305. linkStep = 7;
  306. }else{
  307. errrCnt = 0;
  308. }
  309. break;
  310. case 7:
  311. EC800M_SendCommand(AT_CMD_QIACT);
  312. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  313. if (found != NULL) {
  314. printf("Successfully enabled mobile scene\r\n");
  315. }else{
  316. errrCnt++;
  317. printf("Failed to enable mobile scene\r\n");
  318. }
  319. if(errrCnt == 0){
  320. linkStep = 8;
  321. }else{
  322. errrCnt = 0;
  323. }
  324. break;
  325. case 8: // 连接mqtt服务器
  326. sprintf(command, "AT+QMTCFG=\"qmtping\",0,%d\r\n", 30);
  327. EC800M_SendCommand(command);
  328. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  329. if (found != NULL) {
  330. printf("MQTT heartbeat set successfully\r\n");
  331. }else{
  332. errrCnt++;
  333. printf("Failed to set MQTT heartbeat\r\n");
  334. }
  335. if(errrCnt == 0){
  336. linkStep = 9;
  337. }else{
  338. errrCnt = 0;
  339. }
  340. break;
  341. case 9:
  342. EC800M_SendCommand(AT_CMD_QMTCFG_SET_DATA_MODE);
  343. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  344. if (found != NULL) {
  345. printf("Data receiving mode set successfully.\r\n");
  346. }else{
  347. errrCnt++;
  348. printf("Failed to set data receiving mode\r\n");
  349. }
  350. if(errrCnt == 0){
  351. linkStep = 10;
  352. }else{
  353. errrCnt = 0;
  354. }
  355. break;
  356. case 10:
  357. sprintf(command,"AT+QMTOPEN=0,\"%s\",%d\r\n", s_ec800Date.ip, s_ec800Date.port);
  358. EC800M_SendCommand(command);
  359. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  360. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTOPEN);
  361. if (found != NULL) {
  362. printf("Successfully opened a network for the module\r\n");
  363. }else{
  364. errrCnt++;
  365. printf("Failed to open a network for the module\r\n");
  366. }
  367. if(errrCnt == 0){
  368. linkStep = 11;
  369. }else{
  370. errrCnt = 0;
  371. }
  372. break;
  373. case 11:
  374. sprintf(command,"AT+QMTCONN=0,%s,%s,%s\r\n",s_ec800Date.clientid, s_ec800Date.username, s_ec800Date.password);
  375. EC800M_SendCommand(command);
  376. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  377. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTCONN);
  378. if (found != NULL) {
  379. printf("Successfully connected to the MQTT server\r\n");
  380. }else{
  381. errrCnt++;
  382. printf("Failed to connect to the MQTT server\r\n");
  383. }
  384. if(errrCnt == 0){
  385. linkStep = 12;
  386. }else{
  387. errrCnt = 0;
  388. }
  389. break;
  390. default: break;
  391. }
  392. }
  393. /**
  394. * @brief EC800M初始化GNSS
  395. * @param NONE
  396. * @note
  397. * @retval 无
  398. */
  399. uint8_t gnssStep = 0;
  400. void EC800_gnss_init(void){
  401. char errrCnt = 0; // 错误计数
  402. char* found = NULL;
  403. switch(gnssStep){
  404. case 6:
  405. EC800M_SendCommand(AT_QGPS_0);
  406. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  407. if (found != NULL) {
  408. printf("Turn off the GNSS module\r\n");
  409. }else{
  410. errrCnt++;
  411. printf("Failed to turn off the GNSS module\r\n");
  412. }
  413. if(errrCnt == 0){
  414. gnssStep = 7;
  415. }else{
  416. gnssStep = 7;
  417. }
  418. break;
  419. case 7:
  420. EC800M_SendCommand(AT_QGPS_1);
  421. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  422. if (found != NULL) {
  423. printf("Turn on the GNSS module\r\n");
  424. }else{
  425. errrCnt++;
  426. printf("Failed to turn on the GNSS module\r\n");
  427. }
  428. if(errrCnt == 0){
  429. gnssStep = 8;
  430. }else{
  431. errrCnt = 0;
  432. }
  433. break;
  434. case 0:
  435. EC800M_SendCommand(AT_QGPSCFG_out_port);
  436. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  437. if (found != NULL) {
  438. printf("Outputting NMEA Sentences via Serial Debugging\r\n");
  439. }else{
  440. errrCnt++;
  441. printf("The NMEA sentence output is set incorrectly\r\n");
  442. }
  443. if(errrCnt == 0){
  444. gnssStep = 1;
  445. }else{
  446. errrCnt = 0;
  447. }
  448. break;
  449. case 1:
  450. EC800M_SendCommand(AT_QGPSCFG_nmeasrc);
  451. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  452. if (found != NULL) {
  453. printf("Enabling NMEA sentence retrieval via AT+QGPSGNMEA\r\n");
  454. }else{
  455. errrCnt++;
  456. printf("Failed to enable retrieving NMEA sentences via AT+QGPSGNMEA\r\n");
  457. }
  458. if(errrCnt == 0){
  459. gnssStep = 2;
  460. }else{
  461. errrCnt = 0;
  462. }
  463. break;
  464. case 2:
  465. EC800M_SendCommand(AT_QGPSCFG_gpsnmeatype);
  466. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  467. if (found != NULL) {
  468. printf("Configuring the output type of NMEA sentences to all formats\r\n");
  469. }else{
  470. errrCnt++;
  471. printf("Failed to configure the output type of NMEA sentences to all formats\r\n");
  472. }
  473. if(errrCnt == 0){
  474. gnssStep = 3;
  475. }else{
  476. errrCnt = 0;
  477. }
  478. break;
  479. case 3:
  480. EC800M_SendCommand(AT_QGPSCFG_gnssconfig);
  481. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  482. if (found != NULL) {
  483. printf("Configuring the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  484. }else{
  485. errrCnt++;
  486. printf("Failed to configure the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  487. }
  488. if(errrCnt == 0){
  489. gnssStep = 4;
  490. }else{
  491. errrCnt = 0;
  492. }
  493. break;
  494. case 4:
  495. EC800M_SendCommand(AT_QGPSCFG_autogps);
  496. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  497. if (found != NULL) {
  498. printf("Disable GNSS auto-start\r\n");
  499. }else{
  500. errrCnt++;
  501. printf("Failed to disable GNSS auto-start\r\n");
  502. }
  503. if(errrCnt == 0){
  504. gnssStep = 5;
  505. }else{
  506. errrCnt = 0;
  507. }
  508. break;
  509. case 5:
  510. EC800M_SendCommand(AT_QGPSCFG_apflash);
  511. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  512. if (found != NULL) {
  513. printf("Disable AP-Flash quick start function\r\n");
  514. }else{
  515. errrCnt++;
  516. printf("Failed to disable AP-Flash quick start function\r\n");
  517. }
  518. if(errrCnt == 0){
  519. gnssStep = 6;
  520. }else{
  521. errrCnt = 0;
  522. }
  523. break;
  524. default:
  525. break;
  526. }
  527. }
  528. //UTC时间转换为任意时区时间,如果是转换为北京时间,timezone传8即可
  529. void utc_to_local_time(nmea_utc_time* utc_time, int8_t timezone, nmea_utc_time* local_time)
  530. {
  531. int year,month,day,hour;
  532. int lastday = 0; //last day of this month 本月天数
  533. int lastlastday = 0; //last day of last month 上个月天数
  534. year = utc_time->year; //utc time
  535. month = utc_time->month;
  536. day = utc_time->date;
  537. hour = utc_time->hour + timezone;
  538. //1月大,2月小,3月大,4月小,5月大,6月小,7月大,8月大,9月小,10月大,11月小,12月大
  539. if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
  540. {
  541. lastday = 31;//本月天数
  542. lastlastday = 30;//这里应该补上上个月的天数
  543. if(month == 3)
  544. {
  545. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year
  546. lastlastday = 29;
  547. else
  548. lastlastday = 28;
  549. }
  550. if(month == 8 || month == 1)//这里应该是8月和1月,因为8月和1月的上一个月(7月和12月)的天数是31天的
  551. lastlastday = 31;
  552. }
  553. else if(month == 4 || month == 6 || month == 9 || month == 11)
  554. {
  555. lastday = 30;
  556. lastlastday = 31;
  557. }
  558. else
  559. {
  560. lastlastday = 31;
  561. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
  562. lastday = 29;
  563. else
  564. lastday = 28;
  565. }
  566. if(hour >= 24)// if >24, day+1
  567. {
  568. hour -= 24;
  569. day += 1;
  570. if(day > lastday)// next month, day-lastday of this month
  571. {
  572. day -= lastday;
  573. month += 1;
  574. if(month > 12)// next year, month-12
  575. {
  576. month -= 12;
  577. year += 1;
  578. }
  579. }
  580. }
  581. if(hour < 0)// if <0, day-1
  582. {
  583. hour += 24;
  584. day -= 1;
  585. if(day < 1)// month-1, day=last day of last month
  586. {
  587. day = lastlastday;
  588. month -= 1;
  589. if(month < 1)// last year, month=12
  590. {
  591. month = 12;
  592. year -= 1;
  593. }
  594. }
  595. }
  596. // transfer value to local_time
  597. local_time->year = year;
  598. local_time->month = month;
  599. local_time->date = day;
  600. local_time->hour = hour;
  601. local_time->min = utc_time->min;
  602. local_time->sec = utc_time->sec;
  603. }
  604. /**
  605. * @brief 常用时间格式转时间戳
  606. * @param NONE
  607. * @note
  608. * @retval 无
  609. */
  610. uint32_t EC800_mktime (unsigned int year, unsigned int mon,
  611. unsigned int day, unsigned int hour,
  612. unsigned int min, unsigned int sec)
  613. {
  614. if (0 >= (int) (mon -= 2)){ /**//* 1..12 -> 11,12,1..10 */
  615. mon += 12; /**//* Puts Feb last since it has leap day */
  616. year -= 1;
  617. }
  618. return (((
  619. (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  620. year*365 - 719499
  621. )*24 + hour /**//* now have hours */
  622. )*60 + min /**//* now have minutes */
  623. )*60 + sec; /**//* finally seconds */
  624. }
  625. /**
  626. * @brief 读取GNSS的数据
  627. * @param NONE
  628. * @note
  629. * @retval 无
  630. */
  631. void EC800_readGnssDate(void){
  632. char* found = NULL;
  633. EC800M_SendCommand(AT_QGPSGNMEA_RMC);
  634. EC800_recEnable();
  635. found = strstr(g_usart3_rx_buf, AT_RESP_OK);
  636. if (found != NULL) {
  637. printf("Location information received successfully\r\n");
  638. }else{
  639. printf("Location information reception failed\r\n");
  640. return ;
  641. }
  642. printf("Location information:%s\r\n", g_usart3_rx_buf);
  643. NMEA_GNRMC_Analysis(&s_nmea_msg, (uint8_t*)g_usart3_rx_buf);
  644. utc_to_local_time(&(s_nmea_msg.utc), 8, &s_nmea_utc_time);
  645. 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);
  646. 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);
  647. g_usart3_rx_sta= 0;
  648. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff
  649. 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);
  650. s_messageDate.latitude = s_nmea_msg.latitude;
  651. s_messageDate.nshemi = s_nmea_msg.nshemi;
  652. s_messageDate.longitude = s_nmea_msg.longitude;
  653. s_messageDate.ewhemi = s_nmea_msg.ewhemi;
  654. }
  655. /**
  656. * @brief EC800M订阅主题
  657. * @param topic1: 订阅的主题名称1字符串形式
  658. * @param topic2: 订阅的主题名称2字符串形式
  659. * @note
  660. * @retval 无
  661. */
  662. uint8_t EC800_subscribeToTopic(const char* topic1){
  663. char command[100] = {0};
  664. uint8_t recTemp = 0;
  665. sprintf(command, "AT+QMTSUB=0,1,\"%s\",0\r\n", topic1);
  666. EC800M_SendCommand(command);
  667. recTemp = Accept_and_Compare_Str(AT_RESP_QMTSUB);
  668. if (recTemp == 1) {
  669. printf("Subscription to the topic successful\r\n");
  670. return 1;
  671. }else if (recTemp == 2){
  672. printf("Subscription to the topic failed\r\n");
  673. return 0;
  674. }
  675. }
  676. /**
  677. * @brief EC800M发布消息
  678. * @param topic: 消息发布的主题 字符串形式
  679. * @param message: 发布的消息 字符串形式
  680. * @param len: 发布消息的字节长度
  681. * @note
  682. * @retval 无
  683. */
  684. uint8_t EC800_publishMessage(const char* topic, const char* message, uint16_t len){
  685. char command[100] = {0};
  686. char* found = NULL;
  687. char* responseCmd = ">";
  688. static uint8_t publishStep = 0;
  689. switch(publishStep){
  690. case 0:
  691. sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
  692. EC800M_SendCommand(command);
  693. found = EC800M_RecRespond(g_usart3_rx_buf, responseCmd);
  694. if (found != NULL) {
  695. found = NULL; // 将found指针指向NULL处
  696. printf("The topic has been linked\r\n");
  697. publishStep = 1;
  698. }else{
  699. printf("The topic link failed\r\n");
  700. publishStep = 0;
  701. }
  702. // break;
  703. case 1:
  704. EC800M_SendCommand(message);
  705. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  706. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTPUBEX);
  707. if (found != NULL) {
  708. printf("The message has been successfully posted\r\n");
  709. publishStep = 0;
  710. }else{
  711. printf("Message publication failed\r\n");
  712. publishStep = 0;
  713. }
  714. break;
  715. default:
  716. break;
  717. }
  718. return 2;
  719. }
  720. /**
  721. * @brief EC800M与服务器进行时间对时
  722. * @note
  723. * @retval 无
  724. */
  725. uint8_t EC800_time_calibration(void){
  726. cJSON *root = NULL;
  727. char *jsonString = NULL;
  728. uint8_t temp = 0;
  729. // 定义对象 { }
  730. root = cJSON_CreateObject();
  731. // 插入元素,对应 键值对
  732. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  733. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  734. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  735. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  736. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  737. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  738. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  739. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  740. // 将 JSON 对象转换为字符串
  741. jsonString = cJSON_Print(root);
  742. cJSON_Delete(root);
  743. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  744. free(jsonString);
  745. return temp;
  746. }
  747. /**
  748. * @brief 将服务器响应的字符串中的年月日进行分离
  749. * @note
  750. * @retval 无
  751. */
  752. void parseServerTime(char* str){
  753. char year[5];
  754. char month[3];
  755. char day[3];
  756. char hour[3];
  757. char minute[3];
  758. char second[3];
  759. strncpy(year, str, 4);
  760. year[4] = '\0';
  761. strncpy(month, str + 4, 2);
  762. month[2] = '\0';
  763. strncpy(day, str + 6, 2);
  764. day[2] = '\0';
  765. strncpy(hour, str + 8, 2);
  766. hour[2] = '\0';
  767. strncpy(minute, str + 10, 2);
  768. minute[2] = '\0';
  769. strncpy(second, str + 12, 2);
  770. second[2] = '\0';
  771. s_recDate.year = atoi(year);
  772. s_recDate.month = atoi(month);
  773. s_recDate.day = atoi(day);
  774. s_recDate.hour = atoi(hour);
  775. s_recDate.minute = atoi(minute);
  776. s_recDate.sec = atoi(second);
  777. s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour,s_recDate.minute,s_recDate.sec);
  778. printf("Year (int): %d\n", s_recDate.year);
  779. printf("Month (int): %d\n", s_recDate.month);
  780. printf("Day (int): %d\n", s_recDate.day);
  781. printf("Hour (int): %d\n", s_recDate.hour);
  782. printf("Minute (int): %d\n", s_recDate.minute);
  783. printf("Second (int): %d\n", s_recDate.sec);
  784. }
  785. /**
  786. * @brief 解析对时数据帧的响应
  787. * @note
  788. * @retval 无
  789. */
  790. void EC800_parseRespondTime(cJSON *root, cJSON *item){
  791. uint8_t temp = 0;
  792. // 获取"resultCode"字段的值
  793. item = cJSON_GetObjectItem(root, "resultCode");
  794. if (item == NULL) {
  795. printf("Field \"resultCode\" not found\n");
  796. }else{
  797. // 打印"resultCode"字段的值
  798. printf("resultCode: %d\n", item->valueint);
  799. if((item->valueint) == 1){
  800. printf("The upload of synchronized data was successful.\n");
  801. temp = 1; // 上传成功
  802. }else{
  803. printf("The upload of synchronized data has failed.\n");
  804. }
  805. }
  806. if(temp == 1){
  807. item = cJSON_GetObjectItem(root, "serverTime");
  808. if (item == NULL) {
  809. printf("Field \"serverTime\" not found\n");
  810. }else{
  811. // 打印"serverTime"字段的值
  812. printf("serverTime: %d\n", item->valuestring);
  813. parseServerTime(item->valuestring);
  814. }
  815. }
  816. }
  817. /**
  818. * @brief EC800M上传实时数据
  819. * @note
  820. * @retval 无
  821. */
  822. uint8_t EC800_uploadRealDate(void){
  823. cJSON *root = NULL;
  824. char *jsonString = NULL;
  825. cJSON *rt000 = NULL;
  826. cJSON *rt000Object1 = NULL;
  827. uint8_t temp = 0;
  828. // 定义对象 { }
  829. root = cJSON_CreateObject();
  830. //
  831. // 插入元素,对应 键值对
  832. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  833. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  834. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  835. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  836. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  837. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  838. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  839. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  840. // 定义 { } 对象
  841. rt000Object1 = cJSON_CreateObject();
  842. cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
  843. cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber(s_messageDate.demandVol));
  844. cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber(s_messageDate.demandCur));
  845. cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString((char *)s_messageDate.VIN));
  846. cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(s_messageDate.lat_long_data));
  847. cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
  848. cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
  849. cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(s_messageDate.dailyDrivTime));
  850. cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage));
  851. cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(s_messageDate.accTotalDrivTime_h_min));
  852. cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
  853. cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(s_messageDate.runTime));
  854. // 定义 [ ] 数组
  855. rt000 = cJSON_CreateArray();
  856. // 往数组中添加元素
  857. cJSON_AddItemToArray(rt000, rt000Object1);
  858. // 将子项插入根项中
  859. cJSON_AddItemToObject(root, "rt000", rt000);
  860. //
  861. // 将 JSON 对象转换为字符串
  862. jsonString = cJSON_Print(root);
  863. cJSON_Delete(root);
  864. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  865. free(jsonString);
  866. return temp;
  867. }
  868. /**
  869. * @brief 解析实时数据响应
  870. * @note
  871. * @retval 无
  872. */
  873. void EC800_respondRealDate(cJSON *root, cJSON *item){
  874. // 获取"name"字段的值
  875. item = cJSON_GetObjectItem(root, "resultCode");
  876. if (item == NULL) {
  877. printf("Field \"resultCode\" not found\n");
  878. }else{
  879. // 打印"name"字段的值
  880. printf("resultCode: %d\n", item->valueint);
  881. if((item->valueint) == 1){
  882. printf("Uploaded real-time data successfully\n");
  883. }else{
  884. printf("The upload of real-time data has failed\n");
  885. }
  886. }
  887. }
  888. /**
  889. * @brief 通信响应解析
  890. * @note
  891. * @return 当前的控制命令
  892. */
  893. short EC800_respondParse(void){
  894. cJSON *root = NULL;
  895. cJSON *item = NULL;
  896. short cmd = 0;
  897. // 找到JSON数据的起始位置
  898. const char* start = strchr(g_usart3_rx_buf, '{');
  899. if (start == NULL) {
  900. printf("JSON data not found\n");
  901. return 0;
  902. }
  903. // 解析JSON数据
  904. root = cJSON_Parse(start);
  905. if (root == NULL) {
  906. printf("Failed to parse JSON data\n");
  907. cJSON_Delete(root);
  908. return 0;
  909. }
  910. // 获取"controlCode"字段的值
  911. item = cJSON_GetObjectItem(root, "controlCode");
  912. if (item == NULL) {
  913. printf("Field \"controlCode\" not found\n");
  914. }else{
  915. // 打印"controlCode"字段的值
  916. printf("controlCode: %d\n", item->valueint);
  917. cmd = item->valueint;
  918. }
  919. // 解析对应命令的对应数据
  920. switch(cmd){
  921. case 106:
  922. EC800_parseRespondTime(root, item);
  923. break;
  924. case 202:
  925. EC800_respondRealDate(root, item);
  926. break;
  927. default :
  928. break;
  929. }
  930. cJSON_Delete(root);
  931. return (cmd);
  932. }
  933. /**
  934. * @brief EC800M状态转换与使用
  935. * @note
  936. * @retval 无
  937. */
  938. uint8_t stateStep = 0;
  939. void EC800_stateTransition_use(void){
  940. uint8_t right = 1; // 返回是否为正确
  941. static uint16_t timesCnt = 0;
  942. static uint8_t tudeErrcnt = 0; // 定位信息错误计数
  943. ec800Date *p_ec800Date = &s_ec800Date;
  944. switch(stateStep){
  945. case 0: // 初始化模块
  946. EC800M_link();
  947. if(linkStep == 9){
  948. stateStep = 1;
  949. p_ec800Date->ec800InitFlag = 1; // 初始化完成
  950. }else{
  951. p_ec800Date->ec800InitFlag = 0; // 初始化未完成
  952. }
  953. break;
  954. case 1: // 初始化GNSS
  955. EC800_gnss_init();
  956. if(gnssStep == 8){
  957. stateStep = 2;
  958. }
  959. break;
  960. case 2: // 订阅主题
  961. right = EC800_subscribeToTopic("aabbcc");
  962. if(right != 1){ // 发布消息失败,可能断开链接
  963. // stateStep = 0; // 重新初始化
  964. }else{
  965. stateStep = 3;
  966. }
  967. break;
  968. case 3:
  969. // 获取定位信息
  970. EC800_readGnssDate();
  971. // 没有获取到定位信息 重复获取三次
  972. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  973. tudeErrcnt++;
  974. if(tudeErrcnt == 4){
  975. tudeErrcnt = 0;
  976. stateStep = 4;
  977. }
  978. }else{
  979. tudeErrcnt = 0;
  980. stateStep = 4;
  981. }
  982. break;
  983. case 4: // 与服务器对时
  984. right = EC800_time_calibration();
  985. stateStep = 6; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
  986. break;
  987. case 5: // 发布消息
  988. right = EC800_uploadRealDate();
  989. stateStep = 6;
  990. // if(right == 0){ // 发布消息失败,可能断开链接
  991. // stateStep = 0; // 重新初始化
  992. // linkStep = 0;
  993. // stateStep = 6;
  994. // }else if(right == 1){
  995. // stateStep = 6;
  996. // }
  997. break;
  998. case 6: // 接收消息
  999. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  1000. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  1001. if(g_usart3_rx_sta!=0){
  1002. HAL_UART_Transmit(&huart1, (uint8_t*)g_usart3_rx_buf, strlen(g_usart3_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  1003. right = EC800_respondParse();
  1004. if(right == CTR_CODE_JUDETIME){ // 对时响应
  1005. stateStep = 5;
  1006. right = 0;
  1007. timesCnt = 0;
  1008. }
  1009. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff,接收新的数据
  1010. g_usart3_rx_sta = 0; // 清除接收状态
  1011. }
  1012. // 转弯行驶中
  1013. if(turnFlag == 1){
  1014. if(timesCnt < TURN_INTERVAL_TIME_MS){
  1015. timesCnt++;
  1016. }else{
  1017. timesCnt= 0;
  1018. stateStep = 3;
  1019. HAL_UART_DMAStop(&huart3);
  1020. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1021. }
  1022. }else{ // 正常行驶中
  1023. if(timesCnt < PUBLISH_TIME_MS){
  1024. timesCnt++;
  1025. }else{
  1026. timesCnt= 0;
  1027. stateStep = 3;
  1028. HAL_UART_DMAStop(&huart3);
  1029. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1030. }
  1031. }
  1032. break;
  1033. default:
  1034. break;
  1035. }
  1036. }