EC800(7945).c 53 KB

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