EC800(7815).c 40 KB

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