EC800(6416).c 49 KB

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