EC800(4740).c 53 KB

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