EC800(7464).c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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 8:
  456. // EC800M_SendCommand(AT_QGPSDEL_1);
  457. //
  458. // if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  459. // errrCnt = 0;
  460. //// printf("Turn on the GNSS module\r\n");
  461. // }else{
  462. // errrCnt++;
  463. //// printf("Failed to turn on the GNSS module\r\n");
  464. // }
  465. //
  466. // memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  467. //
  468. // // 返回正常,则进入下一步,否则判断错误次数,进行重连
  469. // gnssStep = (errrCnt == 0) ? 9 : ((errrCnt > task_upload_times_2s) ? 0 : gnssStep);
  470. // break;
  471. case 0:
  472. EC800M_SendCommand(AT_QGPSCFG_out_port);
  473. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  474. errrCnt = 0;
  475. // printf("Outputting NMEA Sentences via Serial Debugging\r\n");
  476. }else{
  477. errrCnt++;
  478. // printf("The NMEA sentence output is set incorrectly\r\n");
  479. }
  480. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  481. // 返回正常,则进入下一步,否则判断错误次数,进行重连
  482. gnssStep = (errrCnt == 0) ? 1 : ((errrCnt > task_upload_times_2s) ? 0 : gnssStep);
  483. break;
  484. case 1:
  485. EC800M_SendCommand(AT_QGPSCFG_nmeasrc);
  486. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  487. errrCnt = 0;
  488. // printf("Enabling NMEA sentence retrieval via AT+QGPSGNMEA\r\n");
  489. }else{
  490. errrCnt++;
  491. // printf("Failed to enable retrieving NMEA sentences via AT+QGPSGNMEA\r\n");
  492. }
  493. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  494. // 返回正常,则进入下一步,否则判断错误次数,进行重连
  495. gnssStep = (errrCnt == 0) ? 2 : ((errrCnt > task_upload_times_2s) ? 0 : gnssStep);
  496. break;
  497. case 2:
  498. EC800M_SendCommand(AT_QGPSCFG_gpsnmeatype);
  499. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  500. errrCnt = 0;
  501. // printf("Configuring the output type of NMEA sentences to all formats\r\n");
  502. }else{
  503. errrCnt++;
  504. // printf("Failed to configure the output type of NMEA sentences to all formats\r\n");
  505. }
  506. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  507. // 返回正常,则进入下一步,否则判断错误次数,进行重连
  508. gnssStep = (errrCnt == 0) ? 3 : ((errrCnt > task_upload_times_2s) ? 0 : gnssStep);
  509. break;
  510. case 3:
  511. EC800M_SendCommand(AT_QGPSCFG_gnssconfig);
  512. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  513. errrCnt = 0;
  514. // printf("Configuring the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  515. }else{
  516. errrCnt++;
  517. // printf("Failed to configure the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  518. }
  519. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  520. // 返回正常,则进入下一步,否则判断错误次数,进行重连
  521. gnssStep = (errrCnt == 0) ? 4 : ((errrCnt > task_upload_times_2s) ? 0 : gnssStep);
  522. break;
  523. case 4:
  524. EC800M_SendCommand(AT_QGPSCFG_autogps);
  525. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  526. errrCnt = 0;
  527. // printf("Disable GNSS auto-start\r\n");
  528. }else{
  529. errrCnt++;
  530. // printf("Failed to disable GNSS auto-start\r\n");
  531. }
  532. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  533. // 返回正常,则进入下一步,否则判断错误次数,进行重连
  534. gnssStep = (errrCnt == 0) ? 5 : ((errrCnt > task_upload_times_2s) ? 0 : gnssStep);
  535. break;
  536. case 5:
  537. EC800M_SendCommand(AT_QGPSCFG_apflash);
  538. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  539. errrCnt = 0;
  540. // printf("Disable AP-Flash quick start function\r\n");
  541. }else{
  542. errrCnt++;
  543. // printf("Failed to disable AP-Flash quick start function\r\n");
  544. }
  545. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  546. // 返回正常,则进入下一步,否则判断错误次数,进行重连
  547. gnssStep = (errrCnt == 0) ? 6 : ((errrCnt > task_upload_times_2s) ? 0 : gnssStep);
  548. break;
  549. default:
  550. break;
  551. }
  552. }
  553. //UTC时间转换为任意时区时间,如果是转换为北京时间,timezone传8即可
  554. void utc_to_local_time(nmea_utc_time* utc_time, int8_t timezone, nmea_utc_time* local_time)
  555. {
  556. int year,month,day,hour;
  557. int lastday = 0; //last day of this month 本月天数
  558. int lastlastday = 0; //last day of last month 上个月天数
  559. year = utc_time->year; //utc time
  560. month = utc_time->month;
  561. day = utc_time->date;
  562. hour = utc_time->hour + timezone;
  563. //1月大,2月小,3月大,4月小,5月大,6月小,7月大,8月大,9月小,10月大,11月小,12月大
  564. if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
  565. {
  566. lastday = 31;//本月天数
  567. lastlastday = 30;//这里应该补上上个月的天数
  568. if(month == 3)
  569. {
  570. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year
  571. lastlastday = 29;
  572. else
  573. lastlastday = 28;
  574. }
  575. if(month == 8 || month == 1)//这里应该是8月和1月,因为8月和1月的上一个月(7月和12月)的天数是31天的
  576. lastlastday = 31;
  577. }
  578. else if(month == 4 || month == 6 || month == 9 || month == 11)
  579. {
  580. lastday = 30;
  581. lastlastday = 31;
  582. }
  583. else
  584. {
  585. lastlastday = 31;
  586. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
  587. lastday = 29;
  588. else
  589. lastday = 28;
  590. }
  591. if(hour >= 24)// if >24, day+1
  592. {
  593. hour -= 24;
  594. day += 1;
  595. if(day > lastday)// next month, day-lastday of this month
  596. {
  597. day -= lastday;
  598. month += 1;
  599. if(month > 12)// next year, month-12
  600. {
  601. month -= 12;
  602. year += 1;
  603. }
  604. }
  605. }
  606. if(hour < 0)// if <0, day-1
  607. {
  608. hour += 24;
  609. day -= 1;
  610. if(day < 1)// month-1, day=last day of last month
  611. {
  612. day = lastlastday;
  613. month -= 1;
  614. if(month < 1)// last year, month=12
  615. {
  616. month = 12;
  617. year -= 1;
  618. }
  619. }
  620. }
  621. // transfer value to local_time
  622. local_time->year = year;
  623. local_time->month = month;
  624. local_time->date = day;
  625. local_time->hour = hour;
  626. local_time->min = utc_time->min;
  627. local_time->sec = utc_time->sec;
  628. }
  629. /**
  630. * @brief 常用时间格式转时间戳
  631. * @param NONE
  632. * @note 要用utc时间进行转换,如果输入为北京时间的话,需要 hour-8;
  633. * @retval 无
  634. */
  635. uint32_t EC800_mktime (unsigned int year, unsigned int mon,
  636. unsigned int day, unsigned int hour,
  637. unsigned int min, unsigned int sec)
  638. {
  639. if (0 >= (int) (mon -= 2)){ /**//* 1..12 -> 11,12,1..10 */
  640. mon += 12; /**//* Puts Feb last since it has leap day */
  641. year -= 1;
  642. }
  643. return (((
  644. (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  645. year*365 - 719499
  646. )*24 + hour /**//* now have hours */
  647. )*60 + min /**//* now have minutes */
  648. )*60 + sec; /**//* finally seconds */
  649. }
  650. /**
  651. * @brief 读取GNSS的数据
  652. * @param NONE
  653. * @note
  654. * @retval 0:接收失败 1:接收成功
  655. */
  656. uint8_t EC800_readGnssDate(void){
  657. EC800M_SendCommand(AT_QGPSGNMEA_RMC);
  658. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  659. // printf("Location information received successfully\r\n");
  660. }else{
  661. // printf("Location information reception failed\r\n");
  662. return 0;
  663. }
  664. // printf("Location information:%s\r\n", g_usart4_rx_buf);
  665. NMEA_GNRMC_Analysis(&s_nmea_msg, (uint8_t*)g_usart4_rx_buf);
  666. utc_to_local_time(&(s_nmea_msg.utc), 8, &s_nmea_utc_time);
  667. // 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);
  668. 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);
  669. g_usart4_rx_sta= 0;
  670. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  671. // 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);
  672. /* 尝试获取互斥量,等待无限长时间 */
  673. if(osMutexAcquire(s_messageDate_locationHandle, 100) == osOK)
  674. {
  675. /* 安全地访问s_rs485RecDate结构体 */
  676. s_messageDate.latitude = s_nmea_msg.latitude;
  677. s_messageDate.nshemi = s_nmea_msg.nshemi;
  678. s_messageDate.longitude = s_nmea_msg.longitude;
  679. s_messageDate.ewhemi = s_nmea_msg.ewhemi;
  680. /* 访问完成,释放互斥量 */
  681. osMutexRelease(s_messageDate_locationHandle);
  682. }
  683. return 1;
  684. }
  685. /**
  686. * @brief EC800M订阅主题
  687. * @param topic1: 订阅的主题名称1字符串形式
  688. * @param topic2: 订阅的主题名称2字符串形式
  689. * @note
  690. * @retval 无
  691. */
  692. uint8_t EC800_subscribeToTopic(const char* topic1){
  693. char command[100] = {0};
  694. uint8_t recTemp = 0;
  695. sprintf(command, "AT+QMTSUB=0,1,\"%s\",0\r\n", topic1);
  696. EC800M_SendCommand(command);
  697. recTemp = Accept_and_Compare_Str(AT_RESP_QMTSUB);
  698. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  699. if (recTemp == 1) {
  700. // printf("Subscription to the topic successful\r\n");
  701. return 1;
  702. }else if (recTemp == 2){
  703. // printf("Subscription to the topic failed\r\n");
  704. return 0;
  705. }
  706. return 0;
  707. }
  708. /**
  709. * @brief EC800M发布消息
  710. * @param topic: 消息发布的主题 字符串形式
  711. * @param message: 发布的消息 字符串形式
  712. * @param len: 发布消息的字节长度
  713. * @note
  714. * @retval 无
  715. */
  716. uint8_t EC800_publishMessage(const char* topic, const char* message, uint16_t len){
  717. char command[100] = {0};
  718. char* responseCmd = ">";
  719. static uint8_t publishStep = 0;
  720. uint8_t recBack = 0;
  721. switch(publishStep){
  722. case 0:
  723. sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
  724. EC800M_SendCommand(command);
  725. if (Accept_and_Compare_Str(responseCmd) == 1) {
  726. // printf("The topic has been linked\r\n");
  727. publishStep = 1;
  728. }else{
  729. // printf("The topic link failed\r\n");
  730. publishStep = 0;
  731. }
  732. // break;
  733. case 1:
  734. EC800M_SendCommand(message);
  735. recBack = Accept_and_Compare_Str(AT_RESP_QMTPUBEX);
  736. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  737. if (recBack == 1) {
  738. publishStep = 0;
  739. // printf("The message has been successfully posted\r\n");
  740. }else{
  741. // printf("Message publication failed\r\n");
  742. publishStep = 0;
  743. }
  744. break;
  745. default:
  746. break;
  747. }
  748. return recBack;
  749. }
  750. // 将uint8_t数组中的ASCII码数据转换为字符串的函数
  751. void uint8ArrayToAsciiString(const uint8_t* array, size_t length, char* str) {
  752. for (size_t i = 0; i < length; ++i) {
  753. str[i] = (char)array[i]; // 直接将每个字节转换为字符
  754. }
  755. str[length] = '\0'; // 添加字符串结束符
  756. }
  757. /**
  758. * @brief 登录 topic
  759. * @param NONE
  760. * @note topic 101
  761. * @retval 无
  762. */
  763. uint8_t Login_Topic(void){
  764. cJSON *root = NULL;
  765. char *jsonString = NULL;
  766. uint8_t temp = 0;
  767. char command[50] = {0};
  768. // 定义对象 { }
  769. root = cJSON_CreateObject();
  770. // 插入元素,对应 键值对
  771. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  772. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  773. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  774. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  775. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  776. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  777. memset(command, 0, 50);
  778. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  779. cJSON_AddItemToObject(root, "hardwareId", cJSON_CreateString(command));
  780. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  781. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  782. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  783. // 将 JSON 对象转换为字符串
  784. jsonString = cJSON_Print(root);
  785. cJSON_Delete(root);
  786. temp = EC800_publishMessage("toServer/mqtt/101", jsonString, strlen(jsonString));
  787. free(jsonString);
  788. return temp;
  789. }
  790. /**
  791. * @brief 下位机请求围栏数据
  792. * @param NONE
  793. * @note topic 604
  794. * @retval 无
  795. */
  796. uint8_t requesting_Fence_Data(void){
  797. cJSON *root = NULL;
  798. char *jsonString = NULL;
  799. uint8_t temp = 0;
  800. char command[50] = {0};
  801. // 定义对象 { }
  802. root = cJSON_CreateObject();
  803. // 插入元素,对应 键值对
  804. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  805. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  806. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  807. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  808. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  809. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  810. memset(command, 0, 50);
  811. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  812. cJSON_AddItemToObject(root, "vin", cJSON_CreateString(command));
  813. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  814. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  815. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  816. // 将 JSON 对象转换为字符串
  817. jsonString = cJSON_Print(root);
  818. cJSON_Delete(root);
  819. temp = EC800_publishMessage("toServer/mqtt/604", jsonString, strlen(jsonString));
  820. free(jsonString);
  821. return temp;
  822. }
  823. /**
  824. * @brief 创建单个报警项的JSON对象
  825. * @param NONE
  826. * @note topic toServer/mqtt/501
  827. * @retval 无
  828. */
  829. cJSON* createAlarmItem(const char* id, uint32_t alarmTime, const char* alarmDesc, int alarmFlag) {
  830. cJSON* alarmItem = cJSON_CreateObject();
  831. cJSON_AddStringToObject(alarmItem, "id", id);
  832. cJSON_AddNumberToObject(alarmItem, "alarmTime", alarmTime);
  833. cJSON_AddStringToObject(alarmItem, "alarmDesc", alarmDesc);
  834. cJSON_AddNumberToObject(alarmItem, "alarmFlag", alarmFlag);
  835. return alarmItem;
  836. }
  837. /**
  838. * @brief 根据Malfunction的值插入故障元素
  839. * @param NONE
  840. * @note topic toServer/mqtt/501
  841. * @retval 无
  842. */
  843. void insertFaultItems(cJSON* alarmList, uint32_t Malfunction, uint32_t fault_timeStamp) {
  844. for (int i = 0; i < 32; i++) {
  845. if (Malfunction & (1 << i)) {
  846. // 确保i在数组的有效范围内
  847. if (i < 12) {
  848. cJSON_AddItemToArray(alarmList, createAlarmItem(faults[i].errid, fault_timeStamp, faults[i].errorInfo, 1));
  849. }
  850. }
  851. }
  852. }
  853. /**
  854. * @brief 故障数据上传
  855. * @param NONE
  856. * @note topic toServer/mqtt/501
  857. * @retval 无
  858. */
  859. uint8_t EC800_uploadFaultReport(uint32_t fault_time_stamp, uint32_t fault_Code){
  860. cJSON *root = NULL;
  861. char *jsonString = NULL;
  862. uint8_t temp = 0;
  863. char command[50] = {0};
  864. // 定义对象 { }
  865. root = cJSON_CreateObject();
  866. // 插入元素,对应 键值对
  867. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  868. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  869. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  870. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  871. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  872. // 插入故障元素
  873. cJSON* alarmList = cJSON_CreateArray();
  874. insertFaultItems(alarmList, fault_Code, fault_time_stamp);
  875. cJSON_AddItemToObject(root, "alarmList", alarmList);
  876. // 将 JSON 对象转换为字符串
  877. jsonString = cJSON_Print(root);
  878. cJSON_Delete(root);
  879. temp = EC800_publishMessage("toServer/mqtt/501", jsonString, strlen(jsonString));
  880. free(jsonString);
  881. return temp;
  882. }
  883. /**
  884. * @brief EC800M与服务器进行时间对时
  885. * @note
  886. * @retval 无
  887. */
  888. uint8_t EC800_time_calibration(void){
  889. cJSON *root = NULL;
  890. char *jsonString = NULL;
  891. uint8_t temp = 0;
  892. char command[50] = {0};
  893. // 定义对象 { }
  894. root = cJSON_CreateObject();
  895. // 插入元素,对应 键值对
  896. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  897. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  898. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  899. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  900. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  901. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  902. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  903. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  904. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  905. // 将 JSON 对象转换为字符串
  906. jsonString = cJSON_Print(root);
  907. cJSON_Delete(root);
  908. temp = EC800_publishMessage("toServer/mqtt/105", jsonString, strlen(jsonString));
  909. free(jsonString);
  910. return temp;
  911. }
  912. /**
  913. * @brief 将服务器响应的字符串中的年月日进行分离
  914. * @note
  915. * @retval 无
  916. */
  917. void parseServerTime(char* str){
  918. char year[5];
  919. char month[3];
  920. char day[3];
  921. char hour[3];
  922. char minute[3];
  923. char second[3];
  924. strncpy(year, str, 4);
  925. year[4] = '\0';
  926. strncpy(month, str + 4, 2);
  927. month[2] = '\0';
  928. strncpy(day, str + 6, 2);
  929. day[2] = '\0';
  930. strncpy(hour, str + 8, 2);
  931. hour[2] = '\0';
  932. strncpy(minute, str + 10, 2);
  933. minute[2] = '\0';
  934. strncpy(second, str + 12, 2);
  935. second[2] = '\0';
  936. s_recDate.year = atoi(year);
  937. s_recDate.month = atoi(month);
  938. s_recDate.day = atoi(day);
  939. s_recDate.hour = atoi(hour);
  940. s_recDate.minute = atoi(minute);
  941. s_recDate.sec = atoi(second);
  942. s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour - 8,s_recDate.minute,s_recDate.sec);
  943. // printf("Year (int): %d\n", s_recDate.year);
  944. // printf("Month (int): %d\n", s_recDate.month);
  945. // printf("Day (int): %d\n", s_recDate.day);
  946. // printf("Hour (int): %d\n", s_recDate.hour);
  947. // printf("Minute (int): %d\n", s_recDate.minute);
  948. // printf("Second (int): %d\n", s_recDate.sec);
  949. }
  950. /**
  951. * @brief 解析对时数据帧的响应
  952. * @note
  953. * @retval 无
  954. */
  955. void EC800_parseRespondTime(cJSON *root, cJSON *item){
  956. uint8_t temp = 0;
  957. // 获取"resultCode"字段的值
  958. item = cJSON_GetObjectItem(root, "resultCode");
  959. if (item == NULL) {
  960. // printf("Field \"resultCode\" not found\n");
  961. }else{
  962. // 打印"resultCode"字段的值
  963. // printf("resultCode: %d\n", item->valueint);
  964. if((item->valueint) == 1){
  965. // printf("The upload of synchronized data was successful.\n");
  966. temp = 1; // 上传成功
  967. }else{
  968. // printf("The upload of synchronized data has failed.\n");
  969. }
  970. }
  971. if(temp == 1){
  972. item = cJSON_GetObjectItem(root, "serverTime");
  973. if (item == NULL) {
  974. // printf("Field \"serverTime\" not found\n");
  975. }else{
  976. // 打印"serverTime"字段的值
  977. // printf("serverTime: %s\n", item->valuestring);
  978. parseServerTime(item->valuestring);
  979. }
  980. }
  981. }
  982. /**
  983. * @brief EC800M上传实时数据
  984. * @note
  985. * @retval 无
  986. */
  987. uint8_t EC800_uploadRealDate(void){
  988. cJSON *root = NULL;
  989. char *jsonString = NULL;
  990. cJSON *rt000 = NULL;
  991. cJSON *rt000Object1 = NULL;
  992. char command[100] = {0};
  993. uint32_t temp = 0; // 临时存储
  994. // 定义对象 { }
  995. root = cJSON_CreateObject();
  996. //
  997. // 插入元素,对应 键值对
  998. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  999. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  1000. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  1001. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  1002. sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  1003. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
  1004. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  1005. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  1006. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  1007. // 定义 { } 对象
  1008. rt000Object1 = cJSON_CreateObject();
  1009. cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
  1010. cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber((float)s_messageDate.demandVol / 10));
  1011. cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber((float)s_messageDate.demandCur / 10));
  1012. // 判断是否不为空
  1013. if(s_messageDate.VIN[0] != NULL){
  1014. memset(command, 0, 50);
  1015. uint8ArrayToAsciiString(s_messageDate.VIN, sizeof(s_messageDate.VIN), command);
  1016. cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString(command));
  1017. }
  1018. double point1Lat = (double)s_messageDate.latitude / 100000;
  1019. double point1Lng = (double)s_messageDate.longitude / 100000;
  1020. memset(command, 0, 50);
  1021. snprintf(command, sizeof(command), "%.5f,%.5f", point1Lng, point1Lat);
  1022. cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(command));
  1023. cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
  1024. cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
  1025. temp = 0;
  1026. temp = (s_messageDate.dailyDrivTime >> 8) * 60 + (s_messageDate.dailyDrivTime & 0x00ff); // 转换单位为min
  1027. cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(temp));
  1028. cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage / 10));
  1029. temp = 0;
  1030. temp = ((s_messageDate.accTotalDrivTime_day * 24) + (s_messageDate.accTotalDrivTime_h_min >> 8)) * 60 + (s_messageDate.accTotalDrivTime_h_min & 0x00ff); // 转换单位为min
  1031. cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(temp));
  1032. cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
  1033. temp = 0;
  1034. temp = (s_messageDate.runTime >> 8) * 60 + (s_messageDate.runTime & 0x00ff);
  1035. cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(temp));
  1036. // test use 发送转弯标志
  1037. // cJSON_AddItemToObject(rt000Object1, "rt036", cJSON_CreateNumber(turnFlag));
  1038. // 定义 [ ] 数组
  1039. rt000 = cJSON_CreateArray();
  1040. // 往数组中添加元素
  1041. cJSON_AddItemToArray(rt000, rt000Object1);
  1042. // 将子项插入根项中
  1043. cJSON_AddItemToObject(root, "rt000", rt000);
  1044. //
  1045. // 将 JSON 对象转换为字符串
  1046. jsonString = cJSON_Print(root);
  1047. cJSON_Delete(root);
  1048. temp = 0;
  1049. temp = EC800_publishMessage("toServer/mqtt/201", jsonString, strlen(jsonString));
  1050. free(jsonString);
  1051. return temp;
  1052. }
  1053. /**
  1054. * @brief 解析实时数据响应
  1055. * @note
  1056. * @retval 无
  1057. */
  1058. void EC800_respondRealDate(cJSON *root, cJSON *item){
  1059. // 获取"name"字段的值
  1060. item = cJSON_GetObjectItem(root, "resultCode");
  1061. if (item == NULL) {
  1062. // printf("Field \"resultCode\" not found\n");
  1063. }else{
  1064. // 打印"name"字段的值
  1065. // printf("resultCode: %d\n", item->valueint);
  1066. if((item->valueint) == 1){
  1067. // printf("Uploaded real-time data successfully\n");
  1068. }else{
  1069. // printf("The upload of real-time data has failed\n");
  1070. }
  1071. }
  1072. }
  1073. /**
  1074. * @brief 解析升級数据
  1075. * @note
  1076. * @retval 无
  1077. */
  1078. void EC800_respondUpdateDate(cJSON *root, cJSON *item, ftpInfo *p_ftpInfo){
  1079. /* 获取address对象 */
  1080. cJSON *hardwareUpdate = cJSON_GetObjectItem(root, "paramList");
  1081. if (hardwareUpdate == NULL) {
  1082. // printf("Failed to parse paramList data\n");
  1083. return ;
  1084. }
  1085. // 获取"name"字段的值
  1086. item = cJSON_GetObjectItem(hardwareUpdate, "function");
  1087. if (item == NULL) {
  1088. // printf("Field \"update\" not found\n");
  1089. }else{
  1090. // 打印"name"字段的值
  1091. // printf("function: %d\n", item->valueint);
  1092. // 1:表示固件更新 当后台下发升级指令,并且行车状态为静止态允许更新固件
  1093. if(((item->valueint) == 1) && (s_comData.driveStatus == 0x02)){
  1094. s_ec800Date.hardwareUpdate = 1; // 更新固件
  1095. // printf("update begin\n");
  1096. }else{
  1097. // printf("no update\n");
  1098. }
  1099. }
  1100. // 获取"ftpAddr"字段的值
  1101. item = cJSON_GetObjectItem(hardwareUpdate, "ftpAddr");
  1102. if (item == NULL) {
  1103. // printf("Field \"ftpAddr\" not found\n");
  1104. }else{
  1105. // printf("ftpAddr: %s\n", item->valuestring);
  1106. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1107. strncpy(p_ftpInfo->ftpAddr, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1108. /* 确保字符串以空字符结尾 */
  1109. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->ftpAddr) - 1] = '\0';
  1110. }
  1111. // 获取"ftpPort"字段的值
  1112. item = cJSON_GetObjectItem(hardwareUpdate, "ftpPort");
  1113. if (item == NULL) {
  1114. // printf("Field \"ftpPort\" not found\n");
  1115. }else{
  1116. // 打印"name"字段的值
  1117. // printf("ftpPort: %d\n", item->valueint);
  1118. p_ftpInfo->ftpPort = item->valueint;
  1119. }
  1120. // 获取"account"字段的值
  1121. item = cJSON_GetObjectItem(hardwareUpdate, "account");
  1122. if (item == NULL) {
  1123. // printf("Field \"account\" not found\n");
  1124. }else{
  1125. // printf("account: %s\n", item->valuestring);
  1126. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1127. strncpy(p_ftpInfo->account, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1128. /* 确保字符串以空字符结尾 */
  1129. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->account) - 1] = '\0';
  1130. }
  1131. // 获取"passWord"字段的值
  1132. item = cJSON_GetObjectItem(hardwareUpdate, "passWord");
  1133. if (item == NULL) {
  1134. // printf("Field \"passWord\" not found\n");
  1135. }else{
  1136. // printf("account: %s\n", item->valuestring);
  1137. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1138. strncpy(p_ftpInfo->passWord, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1139. /* 确保字符串以空字符结尾 */
  1140. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->passWord) - 1] = '\0';
  1141. }
  1142. // 获取"passWord"字段的值
  1143. item = cJSON_GetObjectItem(hardwareUpdate, "textDirectory");
  1144. if (item == NULL) {
  1145. // printf("Field \"textDirectory\" not found\n");
  1146. }else{
  1147. // printf("textDirectory: %s\n", item->valuestring);
  1148. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1149. strncpy(p_ftpInfo->textDirectory, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1150. /* 确保字符串以空字符结尾 */
  1151. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textDirectory) - 1] = '\0';
  1152. }
  1153. // 获取"passWord"字段的值
  1154. item = cJSON_GetObjectItem(hardwareUpdate, "textName");
  1155. if (item == NULL) {
  1156. // printf("Field \"textName\" not found\n");
  1157. }else{
  1158. // printf("textName: %s\n", item->valuestring);
  1159. /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
  1160. strncpy(p_ftpInfo->textName, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
  1161. /* 确保字符串以空字符结尾 */
  1162. p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textName) - 1] = '\0';
  1163. }
  1164. }
  1165. /**
  1166. * @brief 解析围栏经纬度数据
  1167. * @note
  1168. * @param root: cJSON 根对象
  1169. * @param Latitude: 纬度数组
  1170. * @param Longitude: 经度数组
  1171. * @retval 成功返回1,失败返回0
  1172. */
  1173. int parse_Latitude_Longitude_Data(cJSON *root, double Latitude[], double Longitude[]) {
  1174. // 确保输入不为空
  1175. if (root == NULL) {
  1176. // printf("Root object is NULL\n");
  1177. return 0;
  1178. }
  1179. // 获取 polygonLat 数组
  1180. cJSON *polygonLat = cJSON_GetObjectItem(root, "polygonLat");
  1181. if (polygonLat == NULL || polygonLat->type != cJSON_Array) {
  1182. // printf("polygonLat is not an array or is missing\n");
  1183. return 0;
  1184. }
  1185. // 获取 polygonLng 数组
  1186. cJSON *polygonLng = cJSON_GetObjectItem(root, "polygonLon");
  1187. if (polygonLng == NULL || polygonLat->type != cJSON_Array) {
  1188. // printf("polygonLng is not an array or is missing\n");
  1189. return 0;
  1190. }
  1191. // 获取数组大小并检查是否匹配和是否超出预定义的最大大小
  1192. int polygonLatSize = cJSON_GetArraySize(polygonLat);
  1193. int polygonLngSize = cJSON_GetArraySize(polygonLng);
  1194. if (polygonLatSize != polygonLngSize || polygonLatSize > REC_COORDINATE_DEPTH) {
  1195. // printf("Array sizes mismatch or exceed maximum allowed size\n");
  1196. return 0;
  1197. }
  1198. // 解析 polygonLat 数组
  1199. for (int i = 0; i < polygonLatSize; ++i) {
  1200. cJSON *latItem = cJSON_GetArrayItem(polygonLat, i);
  1201. if (latItem == NULL || latItem->type != cJSON_Number) {
  1202. // printf("Lat item is not a number or is missing\n");
  1203. return 0;
  1204. }
  1205. Latitude[i] = latItem->valuedouble;
  1206. }
  1207. // 解析 polygonLng 数组
  1208. for (int i = 0; i < polygonLngSize; ++i) {
  1209. cJSON *lngItem = cJSON_GetArrayItem(polygonLng, i);
  1210. if (lngItem == NULL || lngItem->type != cJSON_Number) {
  1211. // printf("Lng item is not a number or is missing\n");
  1212. return 0;
  1213. }
  1214. Longitude[i] = lngItem->valuedouble;
  1215. }
  1216. // // 解析 speedLimit 字段
  1217. // cJSON* speedLimit = cJSON_GetObjectItem(root, "speedLimit");
  1218. // if (speedLimit == NULL) {
  1219. // printf("Field \"speedLimit\" not found\n");
  1220. // return 0;
  1221. // }else{
  1222. // s_param_boot.speed_limit = speedLimit->valueint;
  1223. // }
  1224. //
  1225. // // 解析 overspeedTimeout 字段
  1226. // cJSON* overspeedTimeout = cJSON_GetObjectItem(root, "overspeedTimeout");
  1227. // if (speedLimit == NULL) {
  1228. // printf("Field \"overspeedTimeout\" not found\n");
  1229. // return 0;
  1230. // }else{
  1231. // s_param_boot.overspeed_Timeout = speedLimit->valueint;
  1232. // }
  1233. //
  1234. // // 解析 fenceBreachTimeout 字段
  1235. // cJSON* fenceBreachTimeout = cJSON_GetObjectItem(root, "fenceBreachTimeout");
  1236. // if (speedLimit == NULL) {
  1237. // printf("Field \"fenceBreachTimeout\" not found\n");
  1238. // return 0;
  1239. // }else{
  1240. // s_param_boot.fenceBreach_Timeout = speedLimit->valueint;
  1241. // }
  1242. //
  1243. return 1; // 成功
  1244. }
  1245. /**
  1246. * @brief 解析登录响应
  1247. * @note
  1248. * @return
  1249. */
  1250. void parse_Login_Response(cJSON *root, cJSON *item){
  1251. // 获取"name"字段的值
  1252. item = cJSON_GetObjectItem(root, "resultCode");
  1253. if (item == NULL) {
  1254. // printf("Field \"resultCode\" not found\n");
  1255. }else{
  1256. // 打印"name"字段的值
  1257. // printf("resultCode: %d\n", item->valueint);
  1258. if((item->valueint) == 1){
  1259. s_messageDate.loginResult = 1; // 登录成功
  1260. // printf("Login response data successfully\n");
  1261. }else{
  1262. // printf("Login response has failed\n");
  1263. }
  1264. }
  1265. }
  1266. /**
  1267. * @brief 解析远程控制指令
  1268. * @note
  1269. * @return
  1270. */
  1271. void processRemoteControl(cJSON *root, cJSON *item){
  1272. global_par *p_global_par = &s_global_par;
  1273. // 解析paramList
  1274. cJSON* paramList = cJSON_GetObjectItem(root, "paramList");
  1275. if (cJSON_GetArraySize(paramList) == 0) {
  1276. // printf("paramList is empty\n");
  1277. return;
  1278. }
  1279. cJSON* firstParam = cJSON_GetArrayItem(paramList, 0);
  1280. if (!firstParam) {
  1281. // printf("paramList is empty\n");
  1282. return;
  1283. }
  1284. // 解析paramList中的第一个元素
  1285. cJSON* id = cJSON_GetObjectItem(firstParam, "id");
  1286. if (item == NULL){
  1287. // printf("id is not a string\n");
  1288. return;
  1289. }else{
  1290. // printf("id: %d\n", id->valueint);
  1291. }
  1292. item = cJSON_GetObjectItem(firstParam, "value");
  1293. if (item == NULL){
  1294. // printf("value is not a string\n");
  1295. return;
  1296. }else{
  1297. // printf("value: %d\n", item->valueint);
  1298. }
  1299. // 解析指令
  1300. switch(id->valueint){
  1301. case 0: // 超级权限
  1302. p_global_par->superUser = (item->valueint == 1);
  1303. break;
  1304. case 1: // 更新围栏数据
  1305. p_global_par->fence_update = (item->valueint == 1);
  1306. break;
  1307. case 2: // 车辆限速
  1308. s_param_boot.speed_limit = item->valueint;
  1309. break;
  1310. case 3: // 锁车
  1311. s_param_boot.car_lock_flag = (item->valueint == 1);
  1312. break;
  1313. default: break;
  1314. }
  1315. }
  1316. /**
  1317. * @brief 通信响应解析
  1318. * @param p_ec800Date: ec800Date结构体的指针数据
  1319. * @note
  1320. * @return 当前的控制命令
  1321. */
  1322. short EC800_respondParse(ec800Date *p_ec800Date){
  1323. cJSON *root = NULL;
  1324. cJSON *item = NULL;
  1325. uint8_t rec_back = 0;
  1326. uint16_t cmd = 0;
  1327. // 找到JSON数据的起始位置
  1328. const char* start = strchr(g_usart4_rx_buf, '{');
  1329. if (start == NULL) {
  1330. // printf("JSON data not found\n");
  1331. cJSON_Delete(root);
  1332. return 0;
  1333. }
  1334. // 解析JSON数据
  1335. root = cJSON_Parse(start);
  1336. if (root == NULL) {
  1337. // printf("Failed to parse JSON data\n");
  1338. cJSON_Delete(root);
  1339. return 0;
  1340. }
  1341. // 获取"controlCode"字段的值
  1342. item = cJSON_GetObjectItem(root, "controlCode");
  1343. if (item == NULL) {
  1344. // printf("Field \"devId\" not found\n");
  1345. }else{
  1346. // 打印"controlCode"字段的值
  1347. // printf("controlCode: %d\n", item->valueint);
  1348. cmd = item->valueint;
  1349. }
  1350. // 解析对应命令的对应数据
  1351. switch(cmd){
  1352. case 102:
  1353. parse_Login_Response(root, item);
  1354. break;
  1355. case 106:
  1356. EC800_parseRespondTime(root, item);
  1357. break;
  1358. case 202:
  1359. EC800_respondRealDate(root, item);
  1360. break;
  1361. case 602:
  1362. EC800_respondUpdateDate(root, item, &s_ftpInfo);
  1363. break;
  1364. case 605:
  1365. rec_back = parse_Latitude_Longitude_Data(root, polygonLat, polygonLng);
  1366. if(rec_back == 1){ // 接收成功
  1367. p_ec800Date->fenceRecSuccess = 1;
  1368. }
  1369. break;
  1370. case 502: // 告警响应
  1371. break;
  1372. case 601: // 远程控制
  1373. processRemoteControl(root, item);
  1374. break;
  1375. default :
  1376. break;
  1377. }
  1378. cJSON_Delete(root);
  1379. return (cmd);
  1380. }
  1381. uint8_t stateStep = 0;
  1382. uint16_t timesCnt = 0; // 实时发送计数器
  1383. uint8_t respondSt = 0; // 响应状态指示 1: 为等待响应 2:成功响应
  1384. /**
  1385. * @brief EC800M状态转换与使用
  1386. * @note
  1387. * @retval 无
  1388. */
  1389. void EC800_stateTransition_use(void){
  1390. uint16_t right = 1; // 返回是否为正确
  1391. ec800Date *p_ec800Date = &s_ec800Date;
  1392. char command[100] = {0};
  1393. static uint8_t interCnt = 0; // 当前区间进入次数
  1394. global_par *p_global_par = &s_global_par;
  1395. static char errrCnt = 0; // 错误计数
  1396. switch(stateStep){
  1397. case 0: // 初始化模块
  1398. interCnt++;
  1399. EC800M_link();
  1400. // 初始化连接MQTT失败
  1401. if(interCnt > 15){
  1402. interCnt = 16;
  1403. p_global_par->InitFaultFlag_4G = 1;
  1404. }
  1405. // 初始化成功
  1406. if(linkStep == 13){
  1407. interCnt = 0; // 清除进入计数
  1408. p_global_par->InitFaultFlag_4G = 0;
  1409. stateStep = 1;
  1410. p_ec800Date->ec800InitFlag = 1; // 初始化完成
  1411. }else{
  1412. p_ec800Date->ec800InitFlag = 0; // 初始化未完成
  1413. }
  1414. break;
  1415. case 1: // 初始化GNSS
  1416. interCnt++;
  1417. EC800_gnss_init();
  1418. // 初始化GNSS失败
  1419. if(interCnt > 12){
  1420. interCnt = 16;
  1421. p_global_par->InitFaultFlag_4G = 1;
  1422. }
  1423. if(gnssStep == 8){
  1424. stateStep = 2;
  1425. interCnt = 0; // 清除进入计数
  1426. p_global_par->InitFaultFlag_4G = 0;
  1427. }
  1428. break;
  1429. case 2: // 订阅主题
  1430. sprintf(command, "toclient/%d/%x%x%x", DEV_TYPE, s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
  1431. right = EC800_subscribeToTopic(command);
  1432. // 模块应答错误
  1433. if (right != 1) {
  1434. if (++errrCnt > task_upload_times_2s) {
  1435. errrCnt = task_upload_times_2s;
  1436. p_global_par->subscribe_fail = 1;
  1437. }
  1438. } else { // 模块应答正确
  1439. errrCnt = 0;
  1440. p_global_par->subscribe_fail = 0;
  1441. stateStep = 3;
  1442. }
  1443. break;
  1444. case 3: // 登录服务器主题
  1445. right = Login_Topic();
  1446. // 模块应答错误
  1447. if (right != 1) {
  1448. if (++errrCnt > task_upload_times_2s) {
  1449. errrCnt = task_upload_times_2s;
  1450. p_global_par->publish_fail = 1;
  1451. }
  1452. } else { // 模块应答正确
  1453. errrCnt = 0;
  1454. p_global_par->publish_fail = 0;
  1455. respondSt = 1; // 等待响应
  1456. stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
  1457. }
  1458. break;
  1459. case 4: // 下位机请求围栏数据
  1460. if(s_comData.vinRecSuccess == 1){ // 成功接收到车辆vin
  1461. right = requesting_Fence_Data();
  1462. // 模块应答错误
  1463. if (right != 1) {
  1464. if (++errrCnt > task_upload_times_2s) {
  1465. errrCnt = task_upload_times_2s;
  1466. p_global_par->publish_fail = 1;
  1467. }
  1468. } else { // 模块应答正确
  1469. errrCnt = 0;
  1470. p_global_par->publish_fail = 0;
  1471. respondSt = 1; // 等待响应
  1472. stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
  1473. }
  1474. }else{ // 初始化时与车通信故障 跳转上传实时信息,上报故障,跳过围栏获取
  1475. if(BIT_CHECK(s_comData.Malfunction, com485)){
  1476. stateStep = 5;
  1477. }
  1478. }
  1479. // 后台要求重新请求围栏数据,请求完成,清除标志
  1480. if(p_global_par->fence_update == 1){
  1481. p_global_par->fence_update = 0;
  1482. }
  1483. break;
  1484. case 5: // 与服务器对时
  1485. right = EC800_time_calibration();
  1486. // 模块应答错误
  1487. if (right != 1) {
  1488. if (++errrCnt > task_upload_times_2s) {
  1489. errrCnt = task_upload_times_2s;
  1490. p_global_par->publish_fail = 1;
  1491. }
  1492. } else { // 模块应答正确
  1493. errrCnt = 0;
  1494. p_global_par->publish_fail = 0;
  1495. respondSt = 1; // 等待响应
  1496. stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
  1497. }
  1498. break;
  1499. case 6:
  1500. // 获取定位信息
  1501. right = EC800_readGnssDate();
  1502. // 模块应答错误
  1503. if (right != 1) {
  1504. if (++errrCnt > task_upload_times_2s) {
  1505. errrCnt = task_upload_times_2s;
  1506. p_global_par->get_location_error = 1;
  1507. }
  1508. } else { // 模块应答正确
  1509. errrCnt = 0;
  1510. p_global_par->get_location_error = 0;
  1511. stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
  1512. }
  1513. // 时间数据是否获取到
  1514. s_global_par.time_stamp_flag = (right == 1);
  1515. // 没有获取到定位信息 重复获取三次,网络存在,但gps信息不存在的情况
  1516. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  1517. if(p_global_par->positionErrorCnt < 3){
  1518. p_global_par->positionErrorCnt++;
  1519. }else{
  1520. p_global_par->positionErrorCnt = 3;
  1521. stateStep = 7;
  1522. }
  1523. }else{
  1524. p_global_par->positionErrorCnt = 0;
  1525. stateStep = 7;
  1526. }
  1527. break;
  1528. case 7: // 发布消息
  1529. right = EC800_uploadRealDate();
  1530. // 模块应答错误
  1531. if (right != 1) {
  1532. if (++errrCnt > task_upload_times_2s) {
  1533. errrCnt = task_upload_times_2s;
  1534. p_global_par->publish_fail = 1;
  1535. }
  1536. } else { // 模块应答正确
  1537. errrCnt = 0;
  1538. p_global_par->publish_fail = 0;
  1539. stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
  1540. }
  1541. break;
  1542. case 8: // 接收消息
  1543. // 转弯行驶中
  1544. if(turnFlag == 1){
  1545. if(timesCnt < TURN_INTERVAL_TIME_MS){
  1546. timesCnt++;
  1547. }else{
  1548. timesCnt= 0;
  1549. stateStep = 6;
  1550. HAL_UART_DMAStop(&huart4);
  1551. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1552. }
  1553. }else{ // 正常行驶中
  1554. if(timesCnt < PUBLISH_TIME_MS){
  1555. timesCnt++;
  1556. }else{
  1557. timesCnt= 0;
  1558. stateStep = 6;
  1559. HAL_UART_DMAStop(&huart4);
  1560. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1561. }
  1562. }
  1563. // 后台要求重新请求围栏数据或者初始化获取到vin请求围栏数据
  1564. if((p_global_par->fence_update == 1) || ((s_comData.vinRecSuccess == 1) && (p_ec800Date->fenceRecSuccess == 0))){
  1565. stateStep = 4;
  1566. }
  1567. // 通信超时判断
  1568. if(respondSt == 1){ // 等待响应状态
  1569. if(p_global_par->mqttTimeoutCnt < PUBLISH_TIME_MS){
  1570. p_global_par->mqttTimeoutCnt ++;
  1571. }else{
  1572. p_global_par->mqttTimeoutCnt = PUBLISH_TIME_MS;
  1573. }
  1574. }else{
  1575. p_global_par->mqttTimeoutCnt = 0;
  1576. }
  1577. // 当MQTT通信异常且gnss回复错误的情况下,4g模块可能断线,重新连接4G模块
  1578. // stateStep = 0;
  1579. // linkStep = 0;
  1580. // gnssStep = 0;
  1581. break;
  1582. default:
  1583. break;
  1584. }
  1585. }
  1586. /**
  1587. * @brief 解析MQTT 链路层状态变化的 URC
  1588. * @note +QMTSTAT: <client_idx>,<err_code> err_code不为0,则重新连接
  1589. * @retval 无
  1590. */
  1591. void parse_mqttUrc(void){
  1592. int val1, val2 = 0;
  1593. // 检查字符串是否以"+QMTSTAT: "开头
  1594. if (strncmp(g_usart4_rx_buf, "+QMTSTAT: ", 10) == 0) {
  1595. // 使用sscanf从字符串中解析两个整数
  1596. if (sscanf(g_usart4_rx_buf + 10, "%d,%d", &val1, &val2) == 2) {
  1597. // 如果成功解析出两个整数,则判断
  1598. if(val1 != client_idx){
  1599. return;
  1600. }
  1601. if(val2 != 0){
  1602. // 需重新连接服务器
  1603. s_global_par.reconnect_server_flag = 1;
  1604. Write_paramArea();
  1605. }
  1606. }
  1607. }
  1608. // 如果字符串不符合格式或解析失败,则返回
  1609. return ;
  1610. }
  1611. /**
  1612. * @brief 判断是否执行重连MQTT服务器
  1613. * @note
  1614. * @retval 无
  1615. */
  1616. void reconnect_mqtt_server(void){
  1617. // 发布消息失败
  1618. if((BIT_CHECK(s_comData.Malfunction, publishFail))
  1619. || (BIT_CHECK(s_comData.Malfunction, subscribeFail))
  1620. || (BIT_CHECK(s_comData.Malfunction, getLocationError))
  1621. ){
  1622. BIT_CLEAR(s_comData.Malfunction, publishFail);
  1623. BIT_CLEAR(s_comData.Malfunction, subscribeFail);
  1624. s_global_par.subscribe_fail = 0;
  1625. s_global_par.publish_fail = 0;
  1626. s_global_par.get_location_error = 0;
  1627. BIT_CLEAR(s_comData.Malfunction, getLocationError);
  1628. // 步骤与标志清零
  1629. stateStep = 0;
  1630. linkStep = 0;
  1631. gnssStep = 0;
  1632. s_ec800Date.ec800InitFlag = 0;
  1633. }
  1634. // 得到gps信息失败
  1635. // if((BIT_CHECK(s_comData.Malfunction, getLocationError))){
  1636. // s_global_par.get_location_error = 0;
  1637. // BIT_CLEAR(s_comData.Malfunction, getLocationError);
  1638. //
  1639. // // 步骤与标志清零
  1640. // stateStep = 0;
  1641. // linkStep = 0;
  1642. // gnssStep = 0;
  1643. // s_ec800Date.ec800InitFlag = 0;
  1644. // }
  1645. }
  1646. /**
  1647. * @brief MQTT服务器数据接收处理
  1648. * @note
  1649. * @retval 无
  1650. */
  1651. void handleReceive(void) {
  1652. uint16_t right = 1; // 返回是否为正确
  1653. if(g_usart4_rx_sta!=0){
  1654. // HAL_UART_Transmit(&huart1, (uint8_t*)g_usart4_rx_buf, strlen(g_usart4_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  1655. parse_mqttUrc(); // 解析MQTT 链路层状态变化的 URC
  1656. right = EC800_respondParse(&s_ec800Date);
  1657. if((right == CTR_CODE_LOGIN) && (s_messageDate.loginResult == 1)){ // 登录响应
  1658. stateStep = 4;
  1659. right = 0;
  1660. timesCnt = 0;
  1661. respondSt = 2; // 成功响应
  1662. }else if(right == CTR_CODE_LOGIN){ // 登录响应 未登录成功
  1663. stateStep = 3;
  1664. right = 0;
  1665. timesCnt = 0;
  1666. respondSt = 2; // 成功响应
  1667. }else if(right == CTR_CODE_FENCES){ // 围栏数据响应
  1668. stateStep = 5;
  1669. right = 0;
  1670. timesCnt = 0;
  1671. respondSt = 2; // 成功响应
  1672. }
  1673. else if(right == CTR_CODE_JUDETIME){ // 对时响应
  1674. stateStep = 6;
  1675. right = 0;
  1676. // timesCnt = 0;
  1677. respondSt = 2; // 成功响应
  1678. }else if((right == CTR_CODE_REMOTE) && (s_ec800Date.hardwareUpdate == 1)){ // 固件更新
  1679. stateStep = 0xff; // 停止占用4G模块
  1680. }
  1681. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff,接收新的数据
  1682. g_usart4_rx_sta = 0; // 清除接收状态
  1683. HAL_UART_DMAStop(&huart4);
  1684. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
  1685. }
  1686. }