EC800(3706).c 42 KB

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