gprs.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. #define LOG_TAG "gprs"
  2. #define DBG_LVL DBG_LOG
  3. #include <rtdbg.h>
  4. #include "gprs.h"
  5. #include "stm32g0xx_hal.h"
  6. #include "at_client.h"
  7. #include "loar.h"
  8. #include <easyflash.h>
  9. #include "usart.h"
  10. uint8_t Get_Location_flag = 0;
  11. uint8_t Get_GPS_mode_falg = 0;
  12. extern uint8_t LTE_Ready;
  13. extern uint8_t run_status;
  14. extern uint8_t Node_type_read; // 开机按节点类型进行配置标记位
  15. extern uint8_t Sever_CMD_Response(uint8_t CMD_Type);
  16. extern UART_HandleTypeDef huart4;
  17. extern uint16_t LTEReadCnt; // 串口4接收到的数据个数
  18. extern uint8_t LTEReadFlag; // 串口4接收数据完成标记位
  19. extern uint8_t LTE_ReadBuff[LTE_CMD_BUFSIZE]; // 串口4接收数据缓存
  20. extern void format_uartbuf(uint8_t uart);
  21. // usart4输出字符串
  22. void Usart4_SendString(char *str)
  23. {
  24. unsigned int k = 0;
  25. do
  26. {
  27. HAL_UART_Transmit(&huart4, (uint8_t *)(str + k), 1, 1000);
  28. k++;
  29. } while (*(str + k) != '\0');
  30. // LOG_E("\r\nsend4:%s\r\n",str);LOG_E("send4:%s",str);
  31. }
  32. // usart4输出固定长度
  33. void Usart4_Send_Len(uint8_t *str, uint16_t len)
  34. {
  35. HAL_UART_Transmit(&huart4, str, len, 1000);
  36. }
  37. extern uint8_t occupy_LTE;
  38. void data_process(void)
  39. {
  40. if (LTEReadFlag == 1) // 串口接收到一条数据
  41. {
  42. LTEReadFlag = 0;
  43. if (ConfigConnectDispose != NULL)
  44. {
  45. run_status = 1;
  46. LOG_I("at_re4:%s\r\n", LTE_ReadBuff);
  47. ConfigConnectDispose((char *)LTE_ReadBuff); // 处理模块返回的数据
  48. format_uartbuf(4);
  49. }
  50. }
  51. }
  52. extern uint8_t LTE_Configured;
  53. extern uint8_t Valve_Mode;
  54. extern uint8_t RC_Mode;
  55. extern uint8_t LTE_config(uint8_t type);
  56. extern uint8_t RC_Service(void);
  57. uint8_t cmd_list[16] = {0}; // 存放解析到的命令
  58. uint8_t Wait_Cmd = 30;
  59. // 4G模块设置联网任务
  60. unsigned char attask(void)
  61. {
  62. uint8_t i = 0, result = 0xFF;
  63. Get_GPS_mode_falg = 100;
  64. while (1)
  65. {
  66. result = LTE_config(2);
  67. rt_thread_mdelay(3);
  68. if (result == 0 || result == 1)
  69. break;
  70. }
  71. extern uint8_t CMD_Find_Flag;
  72. extern uint16_t CMD_Find_Cnt;
  73. extern uint8_t CMD_Find_Buf[LTE_CMD_BUFSIZE];
  74. Get_GPS_mode_falg = 0;
  75. if ((result == 1) || (result == 0))
  76. {
  77. /* 首先清空串口缓存 */
  78. format_uartbuf(4);
  79. DataReturnFlage = 0;
  80. /* 首先清空串口缓存 */
  81. ///
  82. /// cmd_list 用于存放本次收到的命令的数量及类型
  83. ///
  84. if (result == 1)
  85. {
  86. CMD_Find();
  87. }
  88. if (result == 0)
  89. {
  90. LOG_W("Report failed");
  91. }
  92. /**
  93. * @brief 控制模式逻辑 仅支持LTE模式
  94. *
  95. */
  96. RC_Service();
  97. /**
  98. * @brief 控制模式逻辑 仅支持LTE模式
  99. *
  100. */
  101. All_standby(sys_info.interval, 3);
  102. }
  103. return i; // 返回0连接失败,返回1连接成功并完成,返回2连接进行中
  104. }
  105. // MSH_CMD_EXPORT(MQTT_Send, MQTT_Send);
  106. extern uint8_t Active_detection;
  107. // extern uint8_t Matching_running; // 标明配对模式运行中
  108. extern uint8_t CMD_Find_Buf[LTE_CMD_BUFSIZE]; // 用于存放MSGGET收到的数据 不影响LTE_buf的正常使用
  109. extern uint16_t CMD_Find_Cnt; // 存放msgget到的字节数量
  110. extern uint8_t CMD_Find_Flag; // 收取完成标记
  111. uint8_t cmd1[30] = {0}, cmd2[30] = {0}; // 存放命令
  112. #define cmd "cmd"
  113. #define valve_ctl "valve_ctl"
  114. #define read "read"
  115. void CMD_Find(void)
  116. {
  117. // 处于阀控模式下 使用的是缓存订阅消息的方式 需要在此处手动获取
  118. ///
  119. ///
  120. /// 经过不断测试验证 最终确认
  121. ///
  122. /// 开机后的一次使用三方实现
  123. ///
  124. /// 第一方是在main中发送MSGGET
  125. ///
  126. /// 第二方是在cmd_response_thread中触发检测
  127. ///
  128. /// 第三方是在monitor_MSUB_thread中调用检测
  129. ///
  130. /// 方可实现不重复发送MSGGET
  131. ///
  132. ///
  133. while (Wait_Cmd > 0) // 持续检索命令
  134. {
  135. if (CMD_Find_Flag == 1 && CMD_Find_Cnt > 20)
  136. {
  137. rt_sprintf((char *)cmd1, cmd);
  138. rt_sprintf((char *)cmd2, "update");
  139. if (str_cmd() == 1)
  140. {
  141. cmd_list[cmd_list[0]] = 110;
  142. }
  143. rt_sprintf((char *)cmd1, cmd);
  144. rt_sprintf((char *)cmd2, "restore from bak one");
  145. if (str_cmd() == 1)
  146. {
  147. cmd_list[cmd_list[0]] = 120;
  148. }
  149. rt_sprintf((char *)cmd1, cmd);
  150. rt_sprintf((char *)cmd2, "restore from bak two");
  151. if (str_cmd() == 1)
  152. {
  153. cmd_list[cmd_list[0]] = 130;
  154. }
  155. rt_sprintf((char *)cmd1, cmd);
  156. rt_sprintf((char *)cmd2, "upgrade bak one");
  157. if (str_cmd() == 1)
  158. {
  159. cmd_list[cmd_list[0]] = 140;
  160. }
  161. rt_sprintf((char *)cmd1, cmd);
  162. rt_sprintf((char *)cmd2, "upgrade bak two");
  163. if (str_cmd() == 1)
  164. {
  165. cmd_list[cmd_list[0]] = 150;
  166. }
  167. ///
  168. ///
  169. /// ------------ 分割线 --------------
  170. ///
  171. /// 命令的检索和执行之间 使用先入后出的逻辑
  172. /// 本分割线之上 添加不可同时执行的命令(执行完会重启)
  173. /// 本分割线之下 添加可同时执行的命令
  174. ///
  175. /// 同时下发多个命令时 可同时执行的命令不限个数
  176. /// 不可同时执行的命令 有且只能有一个
  177. /// 支持一个不可同时命令+多个同时命令一起使用
  178. ///
  179. ///
  180. ///
  181. rt_sprintf((char *)cmd1, "config");
  182. rt_sprintf((char *)cmd2, "interval");
  183. if (str_cmd() == 1)
  184. {
  185. cmd_list[cmd_list[0]] = 100;
  186. }
  187. rt_sprintf((char *)cmd1, read);
  188. rt_sprintf((char *)cmd2, "bak_info");
  189. if (str_cmd() == 1)
  190. {
  191. cmd_list[cmd_list[0]] = 160;
  192. }
  193. #if (Node_Type == LTE)
  194. rt_sprintf((char *)cmd1, read);
  195. rt_sprintf((char *)cmd2, "status");
  196. if (str_cmd() == 1)
  197. {
  198. cmd_list[cmd_list[0]] = 170;
  199. }
  200. rt_sprintf((char *)cmd1, cmd);
  201. rt_sprintf((char *)cmd2, "Clear Boot_time");
  202. if (str_cmd() == 1)
  203. {
  204. cmd_list[cmd_list[0]] = 180;
  205. }
  206. #endif
  207. rt_sprintf((char *)cmd1, "One");
  208. rt_sprintf((char *)cmd2, "platform");
  209. if (str_cmd() == 1)
  210. {
  211. cmd_list[cmd_list[0]] = 181;
  212. }
  213. rt_sprintf((char *)cmd1, "Zero");
  214. rt_sprintf((char *)cmd2, "platform");
  215. if (str_cmd() == 1)
  216. {
  217. cmd_list[cmd_list[0]] = 182;
  218. }
  219. rt_sprintf((char *)cmd1, cmd);
  220. rt_sprintf((char *)cmd2, "report data");
  221. if (str_cmd() == 1)
  222. {
  223. cmd_list[cmd_list[0]] = 183;
  224. }
  225. rt_sprintf((char *)cmd1, "element");
  226. rt_sprintf((char *)cmd2, "eKey");
  227. if (str_cmd() == 1)
  228. {
  229. cmd_list[cmd_list[0]] = 184;
  230. }
  231. rt_sprintf((char *)cmd1, read);
  232. rt_sprintf((char *)cmd2, "sensor");
  233. if (str_cmd() == 1)
  234. {
  235. cmd_list[cmd_list[0]] = 185;
  236. }
  237. rt_sprintf((char *)cmd1, cmd);
  238. rt_sprintf((char *)cmd2, "upgrade_url");
  239. if (str_cmd() == 1)
  240. {
  241. cmd_list[cmd_list[0]] = 186;
  242. }
  243. rt_sprintf((char *)cmd1, read);
  244. rt_sprintf((char *)cmd2, "e_code");
  245. if (str_cmd() == 1)
  246. {
  247. cmd_list[cmd_list[0]] = 188;
  248. }
  249. rt_sprintf((char *)cmd1, "del");
  250. rt_sprintf((char *)cmd2, "e_code");
  251. if (str_cmd() == 1)
  252. {
  253. cmd_list[cmd_list[0]] = 189;
  254. }
  255. /**
  256. * @brief 为LTE模式新增得专用指令 因为主机模式下 flash不够了
  257. *
  258. */
  259. // 远程控制模式进入
  260. rt_sprintf((char *)cmd1, cmd);
  261. rt_sprintf((char *)cmd2, "RC_IN");
  262. if (str_cmd() == 1)
  263. {
  264. cmd_list[cmd_list[0]] = 193;
  265. }
  266. // 远程控制模式退出
  267. rt_sprintf((char *)cmd1, cmd);
  268. rt_sprintf((char *)cmd2, "RC_OUT");
  269. if (str_cmd() == 1)
  270. {
  271. cmd_list[cmd_list[0]] = 194;
  272. }
  273. // 远程控制模式退出
  274. rt_sprintf((char *)cmd1, cmd);
  275. rt_sprintf((char *)cmd2, "RC_LOC");
  276. if (str_cmd() == 1)
  277. {
  278. cmd_list[cmd_list[0]] = 195;
  279. }
  280. #if Node_Type == LTE
  281. // 配置要素修正
  282. rt_sprintf((char *)cmd1, cmd);
  283. rt_sprintf((char *)cmd2, "elem_cfg");
  284. if (str_cmd() == 1)
  285. {
  286. cmd_list[cmd_list[0]] = 196;
  287. }
  288. // 配置要素修正
  289. rt_sprintf((char *)cmd1, read);
  290. rt_sprintf((char *)cmd2, "elem_cfg");
  291. if (str_cmd() == 1)
  292. {
  293. cmd_list[cmd_list[0]] = 197;
  294. }
  295. #endif
  296. #if 1 // 生产阶段使用功能
  297. // 读取上报几次实际上用于前n次主动上报GPS
  298. rt_sprintf((char *)cmd1, cmd);
  299. rt_sprintf((char *)cmd2, "Clear startTime");
  300. if (str_cmd() == 1)
  301. {
  302. cmd_list[cmd_list[0]] = 198;
  303. }
  304. rt_sprintf((char *)cmd1, read);
  305. rt_sprintf((char *)cmd2, "startTime");
  306. if (str_cmd() == 1)
  307. {
  308. cmd_list[cmd_list[0]] = 199;
  309. }
  310. rt_sprintf((char *)cmd1, cmd);
  311. rt_sprintf((char *)cmd2, "skip startTime");
  312. if (str_cmd() == 1)
  313. {
  314. cmd_list[cmd_list[0]] = 201;
  315. }
  316. #endif
  317. }
  318. if (cmd_list[0] != 0)
  319. break;
  320. rt_thread_mdelay(100);
  321. Wait_Cmd--;
  322. }
  323. while (cmd_list[0] != 0) // 执行收到的所有命令
  324. {
  325. Sever_CMD_Response(cmd_list[cmd_list[0]]);
  326. cmd_list[0]--;
  327. rt_thread_mdelay(3);
  328. }
  329. format_uartbuf(5);
  330. occupy_LTE = Zero;
  331. LOG_I("CMD_Find $");
  332. }
  333. /**
  334. * 数据采集开始前 尝试读取定位
  335. */
  336. #define GPRS_ADDR 0x99
  337. #include "modbus_host.h"
  338. extern MODH_T g_tModH;
  339. extern uint16_t Read_data[];
  340. extern uint8_t MODH_ReadParam_03H(uint16_t slaveAddr, uint16_t _reg, uint16_t _num, uint8_t sort);
  341. Location loc = {0};
  342. Location loc_1[7] = {0};
  343. char flag_getGPS_ok = 0;
  344. char flag_pointer = 0;
  345. U16_Float UtoF = {0};
  346. uint16_t Gte_GpsCnt;
  347. uint8_t Get_GpsFlag;
  348. uint8_t Get_GpsBuff[Get_GpsBuffCnt];
  349. extern uint32_t GPS_Start_Cnt;
  350. void FunctionParseCompare(char *data); // 对比判断是否返回想要的数据
  351. int32_t string_to_u32(const char *str, uint8_t *num)
  352. {
  353. int value = 0;
  354. int sign = 1;
  355. int has_decimal = 0;
  356. // 处理符号(如负数)
  357. if (*str == '-')
  358. {
  359. sign = -1;
  360. str++;
  361. }
  362. // printf("a %d\n",*num);
  363. // 遍历字符
  364. while (*str != '\0')
  365. {
  366. if (*str >= '0' && *str <= '9')
  367. {
  368. if (!has_decimal)
  369. {
  370. value = value * 10 + (*str - '0');
  371. }
  372. else
  373. {
  374. value = value * 10 + (*str - '0');
  375. (*num)++;
  376. }
  377. }
  378. else if (*str == '.')
  379. {
  380. has_decimal = 1;
  381. }
  382. str++;
  383. }
  384. return value * sign;
  385. }
  386. // m^n函数
  387. // 返回值:m^n次方.
  388. uint32_t NMEA_Pow(uint8_t m, uint8_t n)
  389. {
  390. uint32_t result = 1;
  391. while (n--)
  392. result *= m;
  393. return result;
  394. }
  395. // 从buf里面得到第cx个逗号所在的位置
  396. // 返回值:0~0XFE,代表逗号所在位置的偏移.
  397. // 0XFF,代表不存在第cx个逗号
  398. uint8_t NMEA_Comma_Pos(uint8_t *buf, uint8_t cx)
  399. {
  400. uint8_t *p = buf;
  401. while (cx)
  402. {
  403. if (*buf == '*' || *buf < ' ' || *buf > 'z')
  404. return 0XFF; // 遇到'*'或者非法字符,则不存在第cx个逗号
  405. if (*buf == ',')
  406. cx--;
  407. buf++;
  408. }
  409. return buf - p;
  410. }
  411. // str转换为数字,以','或者'*'结束
  412. // buf:数字存储区
  413. // dx:小数点位数,返回给调用函数
  414. // 返回值:转换后的数值
  415. int NMEA_Str2num(uint8_t *buf, uint8_t *dx)
  416. {
  417. uint8_t *p = buf;
  418. uint32_t ires = 0, fres = 0;
  419. uint8_t ilen = 0, flen = 0, i;
  420. uint8_t mask = 0;
  421. int res;
  422. while (1) // 得到整数和小数的长度
  423. {
  424. if (*p == '-')
  425. {
  426. mask |= 0X02;
  427. p++;
  428. } // 是负数
  429. if (*p == ',' || (*p == '*'))
  430. break; // 遇到结束了
  431. if (*p == '.')
  432. {
  433. mask |= 0X01;
  434. p++;
  435. } // 遇到小数点了
  436. else if (*p > '9' || (*p < '0')) // 有非法字符
  437. {
  438. ilen = 0;
  439. flen = 0;
  440. break;
  441. }
  442. if (mask & 0X01)
  443. flen++;
  444. else
  445. ilen++;
  446. p++;
  447. }
  448. if (mask & 0X02)
  449. buf++; // 去掉负号
  450. for (i = 0; i < ilen; i++) // 得到整数部分数据
  451. {
  452. ires += NMEA_Pow(10, ilen - 1 - i) * (buf[i] - '0');
  453. }
  454. if (flen > 5)
  455. flen = 5; // 最多取5位小数
  456. *dx = flen; // 小数点位数
  457. for (i = 0; i < flen; i++) // 得到小数部分数据
  458. {
  459. fres += NMEA_Pow(10, flen - 1 - i) * (buf[ilen + 1 + i] - '0');
  460. }
  461. res = ires * NMEA_Pow(10, flen) + fres;
  462. if (mask & 0X02)
  463. res = -res;
  464. return res;
  465. }
  466. // 分析GPGGA信息
  467. // gpsx:nmea信息结构体
  468. // buf:接收到的GPS数据缓冲区首地址
  469. void NMEA_GPGGA_Analysis(Location *gpsx, uint8_t *buf)
  470. {
  471. uint8_t *p1, dx;
  472. uint8_t posx;
  473. p1 = (uint8_t *)strstr((const char *)buf, "$GNGGA");
  474. posx = NMEA_Comma_Pos(p1, 9); // 得到海拔高度
  475. if (posx != 0XFF)
  476. gpsx->HGT = NMEA_Str2num(p1 + posx, &dx);
  477. uint32_t temp;
  478. float rs;
  479. posx = NMEA_Comma_Pos(p1, 2); // 得到纬度
  480. if (posx != 0XFF)
  481. {
  482. temp = NMEA_Str2num(p1 + posx, &dx);
  483. gpsx->LAT = temp / NMEA_Pow(10, dx + 2); // 得到°
  484. rs = temp % NMEA_Pow(10, dx + 2); // 得到'
  485. gpsx->LAT = gpsx->LAT * NMEA_Pow(10, 5) + (rs * NMEA_Pow(10, 5 - dx)) / 60; // 转换为°
  486. }
  487. // posx=NMEA_Comma_Pos(p1,3); //南纬还是北纬
  488. // if(posx!=0XFF)gpsx->nshemi=*(p1+posx);
  489. posx = NMEA_Comma_Pos(p1, 4); // 得到经度
  490. if (posx != 0XFF)
  491. {
  492. temp = NMEA_Str2num(p1 + posx, &dx);
  493. gpsx->LON = temp / NMEA_Pow(10, dx + 2); // 得到°
  494. rs = temp % NMEA_Pow(10, dx + 2); // 得到'
  495. gpsx->LON = gpsx->LON * NMEA_Pow(10, 5) + (rs * NMEA_Pow(10, 5 - dx)) / 60; // 转换为°
  496. }
  497. // posx=NMEA_Comma_Pos(p1,5); //东经还是西经
  498. // if(posx!=0XFF)gpsx->ewhemi=*(p1+posx);
  499. }
  500. // 按 HGT(高度)从小到大排序
  501. void sortByHeight(Location arr[], int n, char mode)
  502. {
  503. switch (mode)
  504. {
  505. case 1:
  506. {
  507. for (int i = 0; i < n - 1; i++)
  508. {
  509. for (int j = 0; j < n - i - 1; j++)
  510. {
  511. if (arr[j].LON > arr[j + 1].LON)
  512. {
  513. // 交换整个结构体
  514. uint32_t temp = arr[j].LON;
  515. arr[j].LON = arr[j + 1].LON;
  516. arr[j + 1].LON = temp;
  517. }
  518. }
  519. }
  520. }
  521. break;
  522. case 2:
  523. {
  524. for (int i = 0; i < n - 1; i++)
  525. {
  526. for (int j = 0; j < n - i - 1; j++)
  527. {
  528. if (arr[j].LAT > arr[j + 1].LAT)
  529. {
  530. // 交换整个结构体
  531. uint32_t temp = arr[j].LAT;
  532. arr[j].LAT = arr[j + 1].LAT;
  533. arr[j + 1].LAT = temp;
  534. }
  535. }
  536. }
  537. }
  538. break;
  539. case 3:
  540. {
  541. for (int i = 0; i < n - 1; i++)
  542. {
  543. for (int j = 0; j < n - i - 1; j++)
  544. {
  545. if (arr[j].HGT > arr[j + 1].HGT)
  546. {
  547. // 交换整个结构体
  548. uint32_t temp = arr[j].HGT;
  549. arr[j].HGT = arr[j + 1].HGT;
  550. arr[j + 1].HGT = temp;
  551. }
  552. }
  553. }
  554. }
  555. break;
  556. default:
  557. {
  558. }
  559. break;
  560. }
  561. }
  562. /* return
  563. * init = 10
  564. * 0 解析成功 mode 1 2
  565. * 1 没接收到串口数据 同
  566. * 2 接收到串口数据但不含有 "OK" 无
  567. * 3 接收到"OK" 但长度小于60-> 返回的字符无数据 长度小于500
  568. * 4 有数据但没匹配到"+CG" 无
  569. * 5 匹配到 但是没有对齐数据 -> [空格] 无
  570. * 6 无 数据全为0
  571. * 7 不够7次
  572. * 8 一次也没定上
  573. * mode 1:开启打印 变动接受数据 (解析较长数据) 2:关闭打印,主动获取并解析(解析较短)
  574. */
  575. uint8_t Accept_4GGPS(char mode_1)
  576. {
  577. char N_S_f[15] = {0}, E_W_f[15] = {0}, H_m_f[15] = {0};
  578. int N_S = 0, E_W = 0, H_m = 0;
  579. char N_S_num = 0, E_W_num = 0, H_m_num = 0;
  580. char a;
  581. uint8_t rut1 = 10, i;
  582. if (mode_1 == 2)
  583. {
  584. #if 0 // 手动屏蔽
  585. if(LTEReadFlag == 1)
  586. {
  587. if (rt_strstr((const char *)LTE_ReadBuff, "OK") != RT_NULL)
  588. {
  589. if (LTEReadCnt > 60)
  590. {
  591. rut1 = 4;
  592. for(i=0;i<LTEReadCnt;i++)
  593. {
  594. if((LTE_ReadBuff[i] == '+' )&& (LTE_ReadBuff[i+1] == 'C' ) && (LTE_ReadBuff[i+2] == 'G' ))
  595. {
  596. if((LTE_ReadBuff[i+10] == ' ' ))
  597. {
  598. const char *ptr = &LTE_ReadBuff[i+10];
  599. a = 0;
  600. while (*ptr != ',') {
  601. N_S_f[a++] = *ptr++;
  602. }
  603. N_S_f[a] = '\0';
  604. N_S = string_to_u32(N_S_f,&N_S_num);
  605. loc.LON = N_S * 10000;
  606. ptr++; // 跳过 ','
  607. while (*ptr != ',') ptr++;
  608. ptr++;
  609. // 3. 解析 纬度
  610. a = 0;
  611. while (*ptr != ',') {
  612. E_W_f[a++] = *ptr++;
  613. }
  614. E_W_f[a] = '\0';
  615. E_W = string_to_u32(E_W_f,&E_W_num);
  616. loc.LAT = E_W * 10000;
  617. ptr++; // 跳过 ','
  618. // 4. 跳过 "E,"
  619. while (*ptr != ',') ptr++;
  620. ptr++;
  621. // 5. 解析 ddmmyy
  622. while (*ptr != ',') ptr++;
  623. ptr++; // 跳过 ','
  624. // 6. 解析hhmmss.s
  625. while (*ptr != ',') ptr++;
  626. ptr++; // 跳过 ','
  627. // 7. 解析 H
  628. a = 0;
  629. while (*ptr != ',') {
  630. H_m_f[a++] = *ptr++;
  631. }
  632. H_m_f[a] = '\0';
  633. H_m = string_to_u32(H_m_f,&H_m_num);
  634. loc.HGT = H_m * 1000;
  635. rut1 = 0;
  636. loc.succ = 1;
  637. if (loc.succ == 1) // 定位成功 更新位置
  638. {
  639. ef_set_env_blob("loc", &loc, sizeof(loc));
  640. rt_kprintf("Upgrade LOC\n");
  641. }
  642. }
  643. else
  644. rut1 = 5;
  645. break;
  646. }
  647. }
  648. }
  649. else
  650. rut1 = 3;
  651. }
  652. else
  653. rut1 = 2;
  654. }
  655. else
  656. rut1 = 1;
  657. #endif
  658. }
  659. else if (mode_1 == 1) // 处理被动接受的
  660. {
  661. if (Get_GpsFlag == 1)
  662. {
  663. rt_kprintf("Accept_4GGPS \n\n");
  664. if (Gte_GpsCnt > 300)
  665. {
  666. #if 0
  667. NMEA_GPGGA_Analysis(&loc,Get_GpsBuff); //GPGGA解析
  668. if((loc.LON != 0) || (loc.LAT != 0) || (loc.HGT != 0)) // 解析成功
  669. loc.succ = 1;
  670. if (loc.succ == 1) // 定位成功 更新位置
  671. {
  672. ef_set_env_blob("loc", &loc, sizeof(loc));
  673. rt_kprintf("Upgrade LOC\n");
  674. rut1 = 0;
  675. }
  676. else
  677. rut1 = 6;
  678. #else
  679. if (flag_getGPS_ok == 1)
  680. {
  681. flag_pointer++;
  682. flag_getGPS_ok = 0;
  683. rt_kprintf("flag_pointer:%d\n", flag_pointer);
  684. }
  685. NMEA_GPGGA_Analysis(&loc_1[flag_pointer], Get_GpsBuff);
  686. rt_kprintf("\nflag_pointer:%d,LON:%d,LAT:%d,HGT:%d\n", flag_pointer, loc_1[flag_pointer].LON, loc_1[flag_pointer].LAT, loc_1[flag_pointer].HGT);
  687. if ((loc_1[flag_pointer].LON != 0) || (loc_1[flag_pointer].LAT != 0) || (loc_1[flag_pointer].HGT != 0))
  688. {
  689. flag_getGPS_ok = 1;
  690. rt_kprintf("\nGPS_ALL!=0");
  691. }
  692. if (flag_pointer == 6)
  693. {
  694. flag_pointer = 0;
  695. sortByHeight(loc_1, 7, 1);
  696. sortByHeight(loc_1, 7, 2);
  697. sortByHeight(loc_1, 7, 3);
  698. loc.HGT = (loc_1[2].HGT + loc_1[3].HGT + loc_1[4].HGT) / 3;
  699. loc.LON = (loc_1[2].LON + loc_1[3].LON + loc_1[4].LON) / 3;
  700. loc.LAT = (loc_1[2].LAT + loc_1[3].LAT + loc_1[4].LAT) / 3;
  701. // if ((loc.LON != 0) || (loc.LAT != 0) || (loc.HGT != 0))
  702. // loc.succ = 1;
  703. if (loc.succ == 1) // 定位成功 更新位置
  704. {
  705. ef_set_env_blob("loc", &loc, sizeof(loc));
  706. rt_kprintf("Upgrade LOC\n");
  707. LED_buling(200, 2);
  708. rut1 = 0;
  709. }
  710. else
  711. rut1 = 6;
  712. }
  713. else
  714. {
  715. if (flag_pointer == 0)
  716. rut1 = 8;
  717. else
  718. {
  719. rut1 = 7;
  720. }
  721. }
  722. #endif
  723. }
  724. else
  725. rut1 = 3;
  726. }
  727. else
  728. rut1 = 1;
  729. }
  730. return rut1;
  731. }
  732. #define Get_GSP_number_times 60000
  733. void Get_Location(void)
  734. {
  735. char num_zx = 4;
  736. uint32_t i = 0;
  737. uint32_t Num_on_gps_data = 0;
  738. uint32_t Ste_num = 0;
  739. uint8_t rtreturn = 10;
  740. rt_thread_mdelay(2000);
  741. while (1)
  742. {
  743. data_process(); // 处理接受函数
  744. rt_thread_mdelay(2000);
  745. if (DataReturnFlage == 1)
  746. {
  747. DataReturnFlage = 0;
  748. switch (num_zx)
  749. {
  750. case 0:
  751. {
  752. num_zx = 1;
  753. };
  754. break;
  755. case 1:
  756. {
  757. num_zx = 2;
  758. };
  759. break;
  760. case 2:
  761. {
  762. num_zx = 3;
  763. };
  764. break;
  765. case 3:
  766. {
  767. num_zx = 4;
  768. };
  769. break;
  770. case 4:
  771. {
  772. num_zx = 5;
  773. };
  774. break;
  775. case 5:
  776. {
  777. num_zx = 6;
  778. };
  779. break;
  780. case 6:
  781. {
  782. num_zx = 7;
  783. };
  784. break;
  785. case 7:
  786. {
  787. num_zx = 8;
  788. };
  789. break;
  790. case 8:
  791. {
  792. num_zx = 9;
  793. };
  794. break; // 选择模式
  795. case 9:
  796. {
  797. num_zx = 10;
  798. };
  799. break; // mode 1
  800. case 10:
  801. {
  802. num_zx = 11;
  803. };
  804. break;
  805. case 11:
  806. {
  807. num_zx = 12;
  808. };
  809. break;
  810. case 12:
  811. {
  812. num_zx = 12;
  813. };
  814. break;
  815. case 13:
  816. {
  817. num_zx = 14;
  818. };
  819. break; // mode 2
  820. case 14:
  821. {
  822. num_zx = 15;
  823. };
  824. break;
  825. case 15:
  826. {
  827. num_zx = 16;
  828. };
  829. break;
  830. case 16:
  831. {
  832. num_zx = 16;
  833. };
  834. break;
  835. case 17:
  836. {
  837. num_zx = 19;
  838. };
  839. break;
  840. case 18:
  841. {
  842. num_zx = 19;
  843. };
  844. break;
  845. case 19:
  846. {
  847. num_zx = 20;
  848. };
  849. break; // 跳转
  850. case 20:
  851. {
  852. num_zx = 20;
  853. };
  854. break;
  855. case 21:
  856. {
  857. num_zx = 12;
  858. };
  859. break;
  860. case 22:
  861. {
  862. num_zx = 12;
  863. };
  864. break;
  865. case 23:
  866. {
  867. num_zx = 12;
  868. };
  869. break;
  870. };
  871. }
  872. switch (num_zx)
  873. {
  874. case 4:
  875. {
  876. SendConfigFunction("AT\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  877. }
  878. break;
  879. case 5:
  880. {
  881. SendConfigFunction("AT+RSTSET\r\n", NULL, NULL, NULL, FunctionParseCompare, CompareValue);
  882. GPS_Start_Cnt = 0;
  883. rt_thread_mdelay(8000);
  884. if (LTEReadFlag == 1)
  885. {
  886. DataReturnFlage = 1;
  887. }
  888. else
  889. {
  890. num_zx = 4;
  891. }
  892. }
  893. break;
  894. case 6:
  895. {
  896. SendConfigFunction("AT\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  897. }
  898. break;
  899. case 7:
  900. {
  901. SendConfigFunction("ATE0\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  902. }
  903. break;
  904. case 8:
  905. {
  906. while (GPS_Start_Cnt < 20000) // 20s开机20s后开启GPS
  907. {
  908. rt_thread_mdelay(2000);
  909. }
  910. Num_on_gps_data = GPS_Start_Cnt;
  911. DataReturnFlage = 1;
  912. format_uartbuf(4);
  913. }
  914. break;
  915. // 方法1
  916. case 9:
  917. { // 开启自动打印
  918. SendConfigFunction("AT+CGPSPORT=3\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  919. }
  920. break;
  921. case 10:
  922. {
  923. SendConfigFunction("AT+CGPSPORT?\r\n", NULL, "OK", "+CGPSPORT: 3", FunctionParseCompare, CompareValue);
  924. rt_thread_mdelay(100);
  925. }
  926. break;
  927. case 11:
  928. {
  929. SendConfigFunction("AT+CGPS=1\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  930. rt_thread_mdelay(400);
  931. }
  932. break;
  933. case 12:
  934. {
  935. if (Get_GpsFlag == 1)
  936. {
  937. // Get_GpsFlag = 0;
  938. rtreturn = Accept_4GGPS(1);
  939. if (rtreturn == 0) // 解析成功
  940. {
  941. num_zx = 18;
  942. }
  943. if ((rtreturn != 7) && (GPS_Start_Cnt - Num_on_gps_data > Get_GSP_number_times))
  944. {
  945. num_zx = 18;
  946. }
  947. Get_GpsFlag = 0;
  948. }
  949. else
  950. {
  951. rt_kprintf("new get err \n");
  952. }
  953. DataReturnFlage = 1;
  954. }
  955. break;
  956. // 方法2
  957. case 13:
  958. {
  959. SendConfigFunction("AT+CGPSPORT=0\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  960. }
  961. break;
  962. case 14:
  963. {
  964. SendConfigFunction("AT+CGPSPORT?\r\n", NULL, "OK", "+CGPSPORT: 0", FunctionParseCompare, CompareValue);
  965. rt_thread_mdelay(100);
  966. }
  967. break;
  968. case 15:
  969. {
  970. SendConfigFunction("AT+CGPS=1\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  971. rt_thread_mdelay(400);
  972. }
  973. break;
  974. case 16:
  975. {
  976. Usart4_SendString("AT+CGPSINFO\r\n");
  977. i = 1000;
  978. while (i > 0) // 总共延时1000ms 等待接受数据
  979. {
  980. rt_thread_mdelay(1);
  981. if (LTEReadFlag == 1)
  982. break;
  983. i--;
  984. }
  985. rtreturn = Accept_4GGPS(2);
  986. if (rtreturn == 0)
  987. {
  988. num_zx = 18;
  989. }
  990. if ((GPS_Start_Cnt - Num_on_gps_data > Get_GSP_number_times)) // 解析超时
  991. {
  992. num_zx = 18;
  993. }
  994. LTEReadFlag = 0;
  995. DataReturnFlage = 1;
  996. }
  997. break;
  998. case 17:
  999. {
  1000. }
  1001. break;
  1002. case 18:
  1003. {
  1004. }
  1005. break;
  1006. case 19:
  1007. {
  1008. SendConfigFunction("AT+CGPS=0\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
  1009. rt_thread_mdelay(400);
  1010. }
  1011. break;
  1012. case 20:
  1013. {
  1014. return;
  1015. }
  1016. break;
  1017. default:
  1018. {
  1019. rt_kprintf("new GPS case err %d\n", num_zx);
  1020. }
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. char *Send_Buff; // mqtt上报所有的字节 包含AT
  1026. extern uint8_t Init_ok;
  1027. extern int16_t READ_MODBUS_DATA_1(uint8_t times);
  1028. extern uint8_t Start_LTE;
  1029. extern uint8_t RTC_Check;
  1030. extern RTC_DateTypeDef GetDate;
  1031. extern uint8_t Tigger_RTC_Check;
  1032. extern Trans_para Trans_p;
  1033. extern void Get_Location(void);
  1034. static void gprs_entry(void *param)
  1035. {
  1036. uint8_t count = 0;
  1037. while (Start_LTE != One || RTC_Check != One)
  1038. {
  1039. rt_thread_mdelay(100);
  1040. // LOG_I("wait start lte %d %d ",Start_LTE,RTC_Check);
  1041. }
  1042. Config_GPRS_State = -6;
  1043. format_uartbuf(4);
  1044. DataReturnFlage = 0;
  1045. /**
  1046. * 数据采集开始前 尝试读取定位
  1047. */
  1048. uint8_t Get_Loc_day = 0;
  1049. uint8_t IAP_success = 0;
  1050. ef_get_env_blob("IAP_success", &IAP_success, sizeof(IAP_success), NULL);
  1051. ef_get_env_blob("Get_Loc_day", &Get_Loc_day, sizeof(Get_Loc_day), NULL);
  1052. // n次前每次都上报数据
  1053. int startUp_time = 0;
  1054. ef_get_env_blob("startUp_time", &startUp_time, sizeof(startUp_time), NULL);
  1055. if (startUp_time <= 0)
  1056. {
  1057. // 变量不存在,设置默认值并写入
  1058. startUp_time = 1; // 你的默认值
  1059. ef_set_env_blob("startUp_time", &startUp_time, sizeof(startUp_time));
  1060. }
  1061. else
  1062. {
  1063. startUp_time++;
  1064. ef_set_env_blob("startUp_time", &startUp_time, sizeof(startUp_time));
  1065. if (startUp_time < 20)
  1066. {
  1067. Get_Loc_day = 99;
  1068. }
  1069. }
  1070. if (Get_Loc_day == GetDate.Date && (IAP_success != One) && (Tigger_RTC_Check != One)) // 每天执行一次定位(可下发指令触发) 升级成功时不执行
  1071. {
  1072. ef_get_env_blob("loc", &loc, sizeof(loc), NULL);
  1073. rt_kprintf(" read loc \n ");
  1074. }
  1075. else if(IAP_success != One)
  1076. {
  1077. Get_Loc_day = GetDate.Date;
  1078. ef_set_env_blob("Get_Loc_day", &Get_Loc_day, sizeof(Get_Loc_day));
  1079. Get_Location(); // 定位采集器
  1080. }
  1081. Tigger_RTC_Check = Zero;
  1082. /**
  1083. * 数据采集开始
  1084. */
  1085. rt_kprintf("Sensor init %d ms\n", GPS_Start_Cnt);
  1086. while (GPS_Start_Cnt < 5 * 1000)
  1087. {
  1088. rt_thread_mdelay(1000);
  1089. rt_kprintf("Sensor init %dms\n", GPS_Start_Cnt);
  1090. }
  1091. while (sys_info.Sensor_num > 0) // 读取数据
  1092. {
  1093. READ_MODBUS_DATA_1(count); // 数据采集
  1094. if (Init_ok >= sys_info.Sensor_num * 3 /* || 1*/) // 默认让他退出
  1095. {
  1096. // LOG_I("read data succ: %d", Init_ok);
  1097. if (Node_Type == LTE && Trans_p.en == One) // 成功了 退出保护模式
  1098. {
  1099. Trans_p.en = Zero;
  1100. Trans_p.na_cnt = 0;
  1101. ef_set_env_blob("Trans_p", &Trans_p, sizeof(Trans_p));
  1102. Recode_e_code(29, 200);
  1103. LOG_I("exit Trans_p");
  1104. }
  1105. break;
  1106. }
  1107. count++;
  1108. if (count >= 6) // 本来读3次就应该齐活 这读6次还不行 就不试了
  1109. {
  1110. if (Node_Type == LTE && Trans_p.en != One && Init_ok == 0) // 长期NA 进入保护模式
  1111. {
  1112. if (++Trans_p.na_cnt >= 10)
  1113. {
  1114. Trans_p.en = One;
  1115. Recode_e_code(29, 100);
  1116. }
  1117. ef_set_env_blob("Trans_p", &Trans_p, sizeof(Trans_p));
  1118. LOG_W("na_cnt %d en %d", Trans_p.na_cnt, Trans_p.en);
  1119. }
  1120. count = 0;
  1121. break;
  1122. }
  1123. }
  1124. rt_thread_mdelay(30);
  1125. if (Send_Buff == RT_NULL)
  1126. {
  1127. Send_Buff = rt_malloc(send_buf_len); /*at指令发送缓存*/
  1128. }
  1129. if (Send_Buff == RT_NULL)
  1130. {
  1131. rt_kprintf("Send_Buff ×");
  1132. return;
  1133. }
  1134. while (1)
  1135. {
  1136. attask();
  1137. rt_thread_mdelay(10);
  1138. }
  1139. }
  1140. uint8_t str_cmd(void)
  1141. {
  1142. uint8_t result = 0;
  1143. if ((rt_strstr((const char *)CMD_Find_Buf, (const char *)cmd1) != RT_NULL) && (rt_strstr((const char *)CMD_Find_Buf, (const char *)cmd2) != RT_NULL))
  1144. {
  1145. cmd_list[0]++;
  1146. result = 1;
  1147. }
  1148. memset(cmd1, 0, 30);
  1149. memset(cmd2, 0, 30);
  1150. return result;
  1151. }
  1152. #define THREAD_PRIORITY 12
  1153. #define THREAD_STACK_SIZE 3072
  1154. #define THREAD_TIMESLICE 5
  1155. int gprs_thread(void)
  1156. {
  1157. rt_thread_t tid1 = RT_NULL;
  1158. tid1 = rt_thread_create("gprs",
  1159. gprs_entry, RT_NULL,
  1160. THREAD_STACK_SIZE,
  1161. THREAD_PRIORITY, THREAD_TIMESLICE);
  1162. /* 如果获得线程控制块,启动这个线程 */
  1163. if (tid1 != RT_NULL)
  1164. rt_thread_startup(tid1);
  1165. return 0;
  1166. }
  1167. INIT_APP_EXPORT(gprs_thread);
  1168. /*
  1169. * Copyright (c) 2006-2021, RT-Thread Development Team
  1170. *
  1171. * SPDX-License-Identifier: Apache-2.0
  1172. *
  1173. * Change Logs:
  1174. * Date Author Notes
  1175. * 2021-09-15 19813 the first version
  1176. */
  1177. extern uint32_t GPS_Start_Cnt; // 开机计算时间
  1178. void FunctionParseCompare(char *data); // 对比判断是否返回想要的数据
  1179. static void gprs_entryzx(void *param)
  1180. {
  1181. // Get_Location();
  1182. }
  1183. int gprs_threadzx(void)
  1184. {
  1185. rt_thread_t tid1 = RT_NULL;
  1186. tid1 = rt_thread_create("gprszx",
  1187. gprs_entryzx, RT_NULL,
  1188. THREAD_STACK_SIZE,
  1189. THREAD_PRIORITY, THREAD_TIMESLICE);
  1190. /* 如果获得线程控制块,启动这个线程 */
  1191. if (tid1 != RT_NULL)
  1192. rt_thread_startup(tid1);
  1193. return 0;
  1194. }
  1195. INIT_APP_EXPORT(gprs_threadzx);