EC800(6630).c 50 KB

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