EC800(7491).c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. s_messageDate.latitude = s_nmea_msg.latitude;
  661. s_messageDate.nshemi = s_nmea_msg.nshemi;
  662. s_messageDate.longitude = s_nmea_msg.longitude;
  663. s_messageDate.ewhemi = s_nmea_msg.ewhemi;
  664. }
  665. /**
  666. * @brief EC800M订阅主题
  667. * @param topic1: 订阅的主题名称1字符串形式
  668. * @param topic2: 订阅的主题名称2字符串形式
  669. * @note
  670. * @retval 无
  671. */
  672. uint8_t EC800_subscribeToTopic(const char* topic1){
  673. char command[100] = {0};
  674. uint8_t recTemp = 0;
  675. sprintf(command, "AT+QMTSUB=0,1,\"%s\",0\r\n", topic1);
  676. EC800M_SendCommand(command);
  677. recTemp = Accept_and_Compare_Str(AT_RESP_QMTSUB);
  678. if (recTemp == 1) {
  679. printf("Subscription to the topic successful\r\n");
  680. return 1;
  681. }else if (recTemp == 2){
  682. printf("Subscription to the topic failed\r\n");
  683. return 0;
  684. }
  685. return 0;
  686. }
  687. /**
  688. * @brief EC800M发布消息
  689. * @param topic: 消息发布的主题 字符串形式
  690. * @param message: 发布的消息 字符串形式
  691. * @param len: 发布消息的字节长度
  692. * @note
  693. * @retval 无
  694. */
  695. uint8_t EC800_publishMessage(const char* topic, const char* message, uint16_t len){
  696. char command[100] = {0};
  697. char* found = NULL;
  698. char* responseCmd = ">";
  699. static uint8_t publishStep = 0;
  700. uint8_t recBack = 0;
  701. switch(publishStep){
  702. case 0:
  703. sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
  704. EC800M_SendCommand(command);
  705. found = EC800M_RecRespond(g_usart3_rx_buf, responseCmd);
  706. if (found != NULL) {
  707. found = NULL; // 将found指针指向NULL处
  708. printf("The topic has been linked\r\n");
  709. publishStep = 1;
  710. }else{
  711. printf("The topic link failed\r\n");
  712. publishStep = 0;
  713. }
  714. // break;
  715. case 1:
  716. EC800M_SendCommand(message);
  717. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  718. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTPUBEX);
  719. // if (found != NULL) {
  720. // printf("The message has been successfully posted\r\n");
  721. // publishStep = 0;
  722. // }else{
  723. // printf("Message publication failed\r\n");
  724. // publishStep = 0;
  725. // }
  726. recBack = Accept_and_Compare_Str(AT_RESP_QMTPUBEX);
  727. if (recBack == 1) {
  728. publishStep = 0;
  729. printf("The message has been successfully posted\r\n");
  730. }else{
  731. printf("Message publication failed\r\n");
  732. publishStep = 0;
  733. }
  734. break;
  735. default:
  736. break;
  737. }
  738. return recBack;
  739. }
  740. /**
  741. * @brief EC800M与服务器进行时间对时
  742. * @note
  743. * @retval 无
  744. */
  745. uint8_t EC800_time_calibration(void){
  746. cJSON *root = NULL;
  747. char *jsonString = NULL;
  748. uint8_t temp = 0;
  749. // 定义对象 { }
  750. root = cJSON_CreateObject();
  751. // 插入元素,对应 键值对
  752. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  753. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  754. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  755. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  756. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  757. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  758. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  759. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  760. // 将 JSON 对象转换为字符串
  761. jsonString = cJSON_Print(root);
  762. cJSON_Delete(root);
  763. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  764. free(jsonString);
  765. return temp;
  766. }
  767. /**
  768. * @brief 将服务器响应的字符串中的年月日进行分离
  769. * @note
  770. * @retval 无
  771. */
  772. void parseServerTime(char* str){
  773. char year[5];
  774. char month[3];
  775. char day[3];
  776. char hour[3];
  777. char minute[3];
  778. char second[3];
  779. strncpy(year, str, 4);
  780. year[4] = '\0';
  781. strncpy(month, str + 4, 2);
  782. month[2] = '\0';
  783. strncpy(day, str + 6, 2);
  784. day[2] = '\0';
  785. strncpy(hour, str + 8, 2);
  786. hour[2] = '\0';
  787. strncpy(minute, str + 10, 2);
  788. minute[2] = '\0';
  789. strncpy(second, str + 12, 2);
  790. second[2] = '\0';
  791. s_recDate.year = atoi(year);
  792. s_recDate.month = atoi(month);
  793. s_recDate.day = atoi(day);
  794. s_recDate.hour = atoi(hour);
  795. s_recDate.minute = atoi(minute);
  796. s_recDate.sec = atoi(second);
  797. s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour,s_recDate.minute,s_recDate.sec);
  798. printf("Year (int): %d\n", s_recDate.year);
  799. printf("Month (int): %d\n", s_recDate.month);
  800. printf("Day (int): %d\n", s_recDate.day);
  801. printf("Hour (int): %d\n", s_recDate.hour);
  802. printf("Minute (int): %d\n", s_recDate.minute);
  803. printf("Second (int): %d\n", s_recDate.sec);
  804. }
  805. /**
  806. * @brief 解析对时数据帧的响应
  807. * @note
  808. * @retval 无
  809. */
  810. void EC800_parseRespondTime(cJSON *root, cJSON *item){
  811. uint8_t temp = 0;
  812. // 获取"resultCode"字段的值
  813. item = cJSON_GetObjectItem(root, "resultCode");
  814. if (item == NULL) {
  815. printf("Field \"resultCode\" not found\n");
  816. }else{
  817. // 打印"resultCode"字段的值
  818. printf("resultCode: %d\n", item->valueint);
  819. if((item->valueint) == 1){
  820. printf("The upload of synchronized data was successful.\n");
  821. temp = 1; // 上传成功
  822. }else{
  823. printf("The upload of synchronized data has failed.\n");
  824. }
  825. }
  826. if(temp == 1){
  827. item = cJSON_GetObjectItem(root, "serverTime");
  828. if (item == NULL) {
  829. printf("Field \"serverTime\" not found\n");
  830. }else{
  831. // 打印"serverTime"字段的值
  832. printf("serverTime: %s\n", item->valuestring);
  833. parseServerTime(item->valuestring);
  834. }
  835. }
  836. }
  837. /**
  838. * @brief EC800M上传实时数据
  839. * @note
  840. * @retval 无
  841. */
  842. uint8_t EC800_uploadRealDate(void){
  843. cJSON *root = NULL;
  844. char *jsonString = NULL;
  845. cJSON *rt000 = NULL;
  846. cJSON *rt000Object1 = NULL;
  847. uint8_t temp = 0;
  848. // 定义对象 { }
  849. root = cJSON_CreateObject();
  850. //
  851. // 插入元素,对应 键值对
  852. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  853. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  854. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  855. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  856. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  857. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  858. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  859. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  860. // 定义 { } 对象
  861. rt000Object1 = cJSON_CreateObject();
  862. cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
  863. cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber(s_messageDate.demandVol));
  864. cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber(s_messageDate.demandCur));
  865. cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString((char *)s_messageDate.VIN));
  866. cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(s_messageDate.lat_long_data));
  867. cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
  868. cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
  869. cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(s_messageDate.dailyDrivTime));
  870. cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage));
  871. cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(s_messageDate.accTotalDrivTime_h_min));
  872. cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
  873. cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(s_messageDate.runTime));
  874. // 定义 [ ] 数组
  875. rt000 = cJSON_CreateArray();
  876. // 往数组中添加元素
  877. cJSON_AddItemToArray(rt000, rt000Object1);
  878. // 将子项插入根项中
  879. cJSON_AddItemToObject(root, "rt000", rt000);
  880. //
  881. // 将 JSON 对象转换为字符串
  882. jsonString = cJSON_Print(root);
  883. cJSON_Delete(root);
  884. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  885. free(jsonString);
  886. return temp;
  887. }
  888. /**
  889. * @brief 解析实时数据响应
  890. * @note
  891. * @retval 无
  892. */
  893. void EC800_respondRealDate(cJSON *root, cJSON *item){
  894. // 获取"name"字段的值
  895. item = cJSON_GetObjectItem(root, "resultCode");
  896. if (item == NULL) {
  897. printf("Field \"resultCode\" not found\n");
  898. }else{
  899. // 打印"name"字段的值
  900. printf("resultCode: %d\n", item->valueint);
  901. if((item->valueint) == 1){
  902. printf("Uploaded real-time data successfully\n");
  903. }else{
  904. printf("The upload of real-time data has failed\n");
  905. }
  906. }
  907. }
  908. /**
  909. * @brief 解析升級数据
  910. * @note
  911. * @retval 无
  912. */
  913. void EC800_respondUpdateDate(cJSON *root, cJSON *item, ftpInfo *p_ftpInfo){
  914. /* 获取address对象 */
  915. cJSON *hardwareUpdate = cJSON_GetObjectItem(root, "paramList");
  916. if (hardwareUpdate == NULL) {
  917. printf("Failed to parse paramList data\n");
  918. return ;
  919. }
  920. // 获取"name"字段的值
  921. item = cJSON_GetObjectItem(hardwareUpdate, "function");
  922. if (item == NULL) {
  923. printf("Field \"update\" not found\n");
  924. }else{
  925. // 打印"name"字段的值
  926. printf("function: %d\n", item->valueint);
  927. // 1:表示固件更新 2:下发时速
  928. if((item->valueint) == 1){
  929. s_ec800Date.hardwareUpdate = 1; // 更新固件
  930. printf("update begin\n");
  931. }else{
  932. printf("no update\n");
  933. }
  934. }
  935. // 获取"ftpAddr"字段的值
  936. item = cJSON_GetObjectItem(hardwareUpdate, "ftpAddr");
  937. if (item == NULL) {
  938. printf("Field \"ftpAddr\" not found\n");
  939. }else{
  940. printf("ftpAddr: %s\n", item->valuestring);
  941. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  942. strncpy(p_ftpInfo->ftpAddr, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  943. /* 确保字符串以空字符结尾 */
  944. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->ftpAddr) - 1] = '\0';
  945. }
  946. // 获取"ftpPort"字段的值
  947. item = cJSON_GetObjectItem(hardwareUpdate, "ftpPort");
  948. if (item == NULL) {
  949. printf("Field \"ftpPort\" not found\n");
  950. }else{
  951. // 打印"name"字段的值
  952. printf("ftpPort: %d\n", item->valueint);
  953. p_ftpInfo->ftpPort = item->valueint;
  954. }
  955. // 获取"account"字段的值
  956. item = cJSON_GetObjectItem(hardwareUpdate, "account");
  957. if (item == NULL) {
  958. printf("Field \"account\" not found\n");
  959. }else{
  960. printf("account: %s\n", item->valuestring);
  961. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  962. strncpy(p_ftpInfo->account, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  963. /* 确保字符串以空字符结尾 */
  964. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->account) - 1] = '\0';
  965. }
  966. // 获取"passWord"字段的值
  967. item = cJSON_GetObjectItem(hardwareUpdate, "passWord");
  968. if (item == NULL) {
  969. printf("Field \"passWord\" not found\n");
  970. }else{
  971. printf("account: %s\n", item->valuestring);
  972. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  973. strncpy(p_ftpInfo->passWord, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  974. /* 确保字符串以空字符结尾 */
  975. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->passWord) - 1] = '\0';
  976. }
  977. // 获取"passWord"字段的值
  978. item = cJSON_GetObjectItem(hardwareUpdate, "textDirectory");
  979. if (item == NULL) {
  980. printf("Field \"textDirectory\" not found\n");
  981. }else{
  982. printf("textDirectory: %s\n", item->valuestring);
  983. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  984. strncpy(p_ftpInfo->textDirectory, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  985. /* 确保字符串以空字符结尾 */
  986. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textDirectory) - 1] = '\0';
  987. }
  988. // 获取"passWord"字段的值
  989. item = cJSON_GetObjectItem(hardwareUpdate, "textName");
  990. if (item == NULL) {
  991. printf("Field \"textName\" not found\n");
  992. }else{
  993. printf("textName: %s\n", item->valuestring);
  994. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  995. strncpy(p_ftpInfo->textName, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  996. /* 确保字符串以空字符结尾 */
  997. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textName) - 1] = '\0';
  998. }
  999. }
  1000. /**
  1001. * @brief 通信响应解析
  1002. * @note
  1003. * @return 当前的控制命令
  1004. */
  1005. short EC800_respondParse(void){
  1006. cJSON *root = NULL;
  1007. cJSON *item = NULL;
  1008. uint16_t cmd = 0;
  1009. // 找到JSON数据的起始位置
  1010. const char* start = strchr(g_usart3_rx_buf, '{');
  1011. if (start == NULL) {
  1012. printf("JSON data not found\n");
  1013. return 0;
  1014. }
  1015. // 解析JSON数据
  1016. root = cJSON_Parse(start);
  1017. if (root == NULL) {
  1018. printf("Failed to parse JSON data\n");
  1019. cJSON_Delete(root);
  1020. return 0;
  1021. }
  1022. // 获取"devId"字段的值
  1023. item = cJSON_GetObjectItem(root, "devId");
  1024. if (item == NULL) {
  1025. printf("Field \"devId\" not found\n");
  1026. }else{
  1027. // 打印"controlCode"字段的值
  1028. printf("devId: %d\n", item->valueint);
  1029. cmd = item->valueint;
  1030. }
  1031. // 解析对应命令的对应数据
  1032. switch(cmd){
  1033. case 106:
  1034. EC800_parseRespondTime(root, item);
  1035. break;
  1036. case 202:
  1037. EC800_respondRealDate(root, item);
  1038. break;
  1039. case 602:
  1040. EC800_respondUpdateDate(root, item, &s_ftpInfo);
  1041. break;
  1042. default :
  1043. break;
  1044. }
  1045. cJSON_Delete(root);
  1046. return (cmd);
  1047. }
  1048. /**
  1049. * @brief EC800M状态转换与使用
  1050. * @note
  1051. * @retval 无
  1052. */
  1053. uint8_t stateStep = 0;
  1054. void EC800_stateTransition_use(void){
  1055. uint16_t right = 1; // 返回是否为正确
  1056. static uint16_t timesCnt = 0;
  1057. static uint8_t tudeErrcnt = 0; // 定位信息错误计数
  1058. ec800Date *p_ec800Date = &s_ec800Date;
  1059. switch(stateStep){
  1060. case 0: // 初始化模块
  1061. EC800M_link();
  1062. if(linkStep == 12){
  1063. stateStep = 1;
  1064. p_ec800Date->ec800InitFlag = 1; // 初始化完成
  1065. }else{
  1066. p_ec800Date->ec800InitFlag = 0; // 初始化未完成
  1067. }
  1068. break;
  1069. case 1: // 初始化GNSS
  1070. EC800_gnss_init();
  1071. if(gnssStep == 8){
  1072. stateStep = 2;
  1073. }
  1074. break;
  1075. case 2: // 订阅主题
  1076. right = EC800_subscribeToTopic("aabbcc");
  1077. if(right != 1){ // 发布消息失败,可能断开链接
  1078. // stateStep = 0; // 重新初始化
  1079. }else{
  1080. stateStep = 3;
  1081. }
  1082. break;
  1083. case 3:
  1084. // 获取定位信息
  1085. EC800_readGnssDate();
  1086. // 没有获取到定位信息 重复获取三次
  1087. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  1088. tudeErrcnt++;
  1089. if(tudeErrcnt == 4){
  1090. tudeErrcnt = 0;
  1091. stateStep = 4;
  1092. }
  1093. }else{
  1094. tudeErrcnt = 0;
  1095. stateStep = 4;
  1096. }
  1097. break;
  1098. case 4: // 与服务器对时
  1099. right = EC800_time_calibration();
  1100. if(right == 1){
  1101. stateStep = 6; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
  1102. }
  1103. break;
  1104. case 5: // 发布消息
  1105. right = EC800_uploadRealDate();
  1106. stateStep = 6;
  1107. // if(right == 0){ // 发布消息失败,可能断开链接
  1108. // stateStep = 0; // 重新初始化
  1109. // linkStep = 0;
  1110. // stateStep = 6;
  1111. // }else if(right == 1){
  1112. // stateStep = 6;
  1113. // }
  1114. break;
  1115. case 6: // 接收消息
  1116. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  1117. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  1118. if(g_usart3_rx_sta!=0){
  1119. HAL_UART_Transmit(&huart1, (uint8_t*)g_usart3_rx_buf, strlen(g_usart3_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  1120. right = EC800_respondParse();
  1121. if(right == CTR_CODE_JUDETIME){ // 对时响应
  1122. stateStep = 5;
  1123. right = 0;
  1124. timesCnt = 0;
  1125. }else if(right == CTR_CODE_REMOTE){ // 固件更新
  1126. stateStep = 0xff; // 停止占用4G模块
  1127. }
  1128. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff,接收新的数据
  1129. g_usart3_rx_sta = 0; // 清除接收状态
  1130. }
  1131. // 转弯行驶中
  1132. if(turnFlag == 1){
  1133. if(timesCnt < TURN_INTERVAL_TIME_MS){
  1134. timesCnt++;
  1135. }else{
  1136. timesCnt= 0;
  1137. stateStep = 3;
  1138. HAL_UART_DMAStop(&huart3);
  1139. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1140. }
  1141. }else{ // 正常行驶中
  1142. if(timesCnt < PUBLISH_TIME_MS){
  1143. timesCnt++;
  1144. }else{
  1145. timesCnt= 0;
  1146. stateStep = 3;
  1147. HAL_UART_DMAStop(&huart3);
  1148. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1149. }
  1150. }
  1151. break;
  1152. default:
  1153. break;
  1154. }
  1155. }