EC800(1830).c 47 KB

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