| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291 |
- #define LOG_TAG "gprs"
- #define DBG_LVL DBG_LOG
- #include <rtdbg.h>
- #include "gprs.h"
- #include "stm32g0xx_hal.h"
- #include "at_client.h"
- #include "loar.h"
- #include <easyflash.h>
- #include "usart.h"
- uint8_t Get_Location_flag = 0;
- uint8_t Get_GPS_mode_falg = 0;
- extern uint8_t LTE_Ready;
- extern uint8_t run_status;
- extern uint8_t Node_type_read; // 开机按节点类型进行配置标记位
- extern uint8_t Sever_CMD_Response(uint8_t CMD_Type);
- extern UART_HandleTypeDef huart4;
- extern uint16_t LTEReadCnt; // 串口4接收到的数据个数
- extern uint8_t LTEReadFlag; // 串口4接收数据完成标记位
- extern uint8_t LTE_ReadBuff[LTE_CMD_BUFSIZE]; // 串口4接收数据缓存
- extern void format_uartbuf(uint8_t uart);
- // usart4输出字符串
- void Usart4_SendString(char *str)
- {
- unsigned int k = 0;
- do
- {
- HAL_UART_Transmit(&huart4, (uint8_t *)(str + k), 1, 1000);
- k++;
- } while (*(str + k) != '\0');
- // LOG_E("\r\nsend4:%s\r\n",str);LOG_E("send4:%s",str);
- }
- // usart4输出固定长度
- void Usart4_Send_Len(uint8_t *str, uint16_t len)
- {
- HAL_UART_Transmit(&huart4, str, len, 1000);
- }
- extern uint8_t occupy_LTE;
- void data_process(void)
- {
- if (LTEReadFlag == 1) // 串口接收到一条数据
- {
- LTEReadFlag = 0;
- if (ConfigConnectDispose != NULL)
- {
- run_status = 1;
- LOG_I("at_re4:%s\r\n", LTE_ReadBuff);
- ConfigConnectDispose((char *)LTE_ReadBuff); // 处理模块返回的数据
- format_uartbuf(4);
- }
- }
- }
- extern uint8_t LTE_Configured;
- extern uint8_t Valve_Mode;
- extern uint8_t RC_Mode;
- extern uint8_t LTE_config(uint8_t type);
- extern uint8_t RC_Service(void);
- uint8_t cmd_list[16] = {0}; // 存放解析到的命令
- uint8_t Wait_Cmd = 30;
- // 4G模块设置联网任务
- unsigned char attask(void)
- {
- uint8_t i = 0, result = 0xFF;
- Get_GPS_mode_falg = 100;
- while (1)
- {
- result = LTE_config(2);
- rt_thread_mdelay(3);
- if (result == 0 || result == 1)
- break;
- }
- extern uint8_t CMD_Find_Flag;
- extern uint16_t CMD_Find_Cnt;
- extern uint8_t CMD_Find_Buf[LTE_CMD_BUFSIZE];
- Get_GPS_mode_falg = 0;
- if ((result == 1) || (result == 0))
- {
- /* 首先清空串口缓存 */
- format_uartbuf(4);
- DataReturnFlage = 0;
- /* 首先清空串口缓存 */
- ///
- /// cmd_list 用于存放本次收到的命令的数量及类型
- ///
- if (result == 1)
- {
- CMD_Find();
- }
- if (result == 0)
- {
- LOG_W("Report failed");
- }
- /**
- * @brief 控制模式逻辑 仅支持LTE模式
- *
- */
- RC_Service();
- /**
- * @brief 控制模式逻辑 仅支持LTE模式
- *
- */
- All_standby(sys_info.interval, 3);
- }
- return i; // 返回0连接失败,返回1连接成功并完成,返回2连接进行中
- }
- // MSH_CMD_EXPORT(MQTT_Send, MQTT_Send);
- extern uint8_t Active_detection;
- // extern uint8_t Matching_running; // 标明配对模式运行中
- extern uint8_t CMD_Find_Buf[LTE_CMD_BUFSIZE]; // 用于存放MSGGET收到的数据 不影响LTE_buf的正常使用
- extern uint16_t CMD_Find_Cnt; // 存放msgget到的字节数量
- extern uint8_t CMD_Find_Flag; // 收取完成标记
- uint8_t cmd1[30] = {0}, cmd2[30] = {0}; // 存放命令
- #define cmd "cmd"
- #define valve_ctl "valve_ctl"
- #define read "read"
- void CMD_Find(void)
- {
- // 处于阀控模式下 使用的是缓存订阅消息的方式 需要在此处手动获取
- ///
- ///
- /// 经过不断测试验证 最终确认
- ///
- /// 开机后的一次使用三方实现
- ///
- /// 第一方是在main中发送MSGGET
- ///
- /// 第二方是在cmd_response_thread中触发检测
- ///
- /// 第三方是在monitor_MSUB_thread中调用检测
- ///
- /// 方可实现不重复发送MSGGET
- ///
- ///
- while (Wait_Cmd > 0) // 持续检索命令
- {
- if (CMD_Find_Flag == 1 && CMD_Find_Cnt > 20)
- {
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "update");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 110;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "restore from bak one");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 120;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "restore from bak two");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 130;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "upgrade bak one");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 140;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "upgrade bak two");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 150;
- }
- ///
- ///
- /// ------------ 分割线 --------------
- ///
- /// 命令的检索和执行之间 使用先入后出的逻辑
- /// 本分割线之上 添加不可同时执行的命令(执行完会重启)
- /// 本分割线之下 添加可同时执行的命令
- ///
- /// 同时下发多个命令时 可同时执行的命令不限个数
- /// 不可同时执行的命令 有且只能有一个
- /// 支持一个不可同时命令+多个同时命令一起使用
- ///
- ///
- ///
- rt_sprintf((char *)cmd1, "config");
- rt_sprintf((char *)cmd2, "interval");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 100;
- }
- rt_sprintf((char *)cmd1, read);
- rt_sprintf((char *)cmd2, "bak_info");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 160;
- }
- #if (Node_Type == LTE)
- rt_sprintf((char *)cmd1, read);
- rt_sprintf((char *)cmd2, "status");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 170;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "Clear Boot_time");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 180;
- }
- #endif
- rt_sprintf((char *)cmd1, "One");
- rt_sprintf((char *)cmd2, "platform");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 181;
- }
- rt_sprintf((char *)cmd1, "Zero");
- rt_sprintf((char *)cmd2, "platform");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 182;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "report data");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 183;
- }
- rt_sprintf((char *)cmd1, "element");
- rt_sprintf((char *)cmd2, "eKey");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 184;
- }
- rt_sprintf((char *)cmd1, read);
- rt_sprintf((char *)cmd2, "sensor");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 185;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "upgrade_url");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 186;
- }
- rt_sprintf((char *)cmd1, read);
- rt_sprintf((char *)cmd2, "e_code");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 188;
- }
- rt_sprintf((char *)cmd1, "del");
- rt_sprintf((char *)cmd2, "e_code");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 189;
- }
- /**
- * @brief 为LTE模式新增得专用指令 因为主机模式下 flash不够了
- *
- */
- // 远程控制模式进入
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "RC_IN");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 193;
- }
- // 远程控制模式退出
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "RC_OUT");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 194;
- }
- // 远程控制模式退出
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "RC_LOC");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 195;
- }
- #if Node_Type == LTE
- // 配置要素修正
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "elem_cfg");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 196;
- }
- // 配置要素修正
- rt_sprintf((char *)cmd1, read);
- rt_sprintf((char *)cmd2, "elem_cfg");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 197;
- }
- #endif
- #if 1 // 生产阶段使用功能
- // 读取上报几次实际上用于前n次主动上报GPS
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "Clear startTime");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 198;
- }
- rt_sprintf((char *)cmd1, read);
- rt_sprintf((char *)cmd2, "startTime");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 199;
- }
- rt_sprintf((char *)cmd1, cmd);
- rt_sprintf((char *)cmd2, "skip startTime");
- if (str_cmd() == 1)
- {
- cmd_list[cmd_list[0]] = 201;
- }
- #endif
- }
- if (cmd_list[0] != 0)
- break;
- rt_thread_mdelay(100);
- Wait_Cmd--;
- }
- while (cmd_list[0] != 0) // 执行收到的所有命令
- {
- Sever_CMD_Response(cmd_list[cmd_list[0]]);
- cmd_list[0]--;
- rt_thread_mdelay(3);
- }
- format_uartbuf(5);
- occupy_LTE = Zero;
- LOG_I("CMD_Find $");
- }
- /**
- * 数据采集开始前 尝试读取定位
- */
- #define GPRS_ADDR 0x99
- #include "modbus_host.h"
- extern MODH_T g_tModH;
- extern uint16_t Read_data[];
- extern uint8_t MODH_ReadParam_03H(uint16_t slaveAddr, uint16_t _reg, uint16_t _num, uint8_t sort);
- Location loc = {0};
- Location loc_1[7] = {0};
- char flag_getGPS_ok = 0;
- char flag_pointer = 0;
- U16_Float UtoF = {0};
- uint16_t Gte_GpsCnt;
- uint8_t Get_GpsFlag;
- uint8_t Get_GpsBuff[Get_GpsBuffCnt];
- extern uint32_t GPS_Start_Cnt;
- void FunctionParseCompare(char *data); // 对比判断是否返回想要的数据
- int32_t string_to_u32(const char *str, uint8_t *num)
- {
- int value = 0;
- int sign = 1;
- int has_decimal = 0;
- // 处理符号(如负数)
- if (*str == '-')
- {
- sign = -1;
- str++;
- }
- // printf("a %d\n",*num);
- // 遍历字符
- while (*str != '\0')
- {
- if (*str >= '0' && *str <= '9')
- {
- if (!has_decimal)
- {
- value = value * 10 + (*str - '0');
- }
- else
- {
- value = value * 10 + (*str - '0');
- (*num)++;
- }
- }
- else if (*str == '.')
- {
- has_decimal = 1;
- }
- str++;
- }
- return value * sign;
- }
- // m^n函数
- // 返回值:m^n次方.
- uint32_t NMEA_Pow(uint8_t m, uint8_t n)
- {
- uint32_t result = 1;
- while (n--)
- result *= m;
- return result;
- }
- // 从buf里面得到第cx个逗号所在的位置
- // 返回值:0~0XFE,代表逗号所在位置的偏移.
- // 0XFF,代表不存在第cx个逗号
- uint8_t NMEA_Comma_Pos(uint8_t *buf, uint8_t cx)
- {
- uint8_t *p = buf;
- while (cx)
- {
- if (*buf == '*' || *buf < ' ' || *buf > 'z')
- return 0XFF; // 遇到'*'或者非法字符,则不存在第cx个逗号
- if (*buf == ',')
- cx--;
- buf++;
- }
- return buf - p;
- }
- // str转换为数字,以','或者'*'结束
- // buf:数字存储区
- // dx:小数点位数,返回给调用函数
- // 返回值:转换后的数值
- int NMEA_Str2num(uint8_t *buf, uint8_t *dx)
- {
- uint8_t *p = buf;
- uint32_t ires = 0, fres = 0;
- uint8_t ilen = 0, flen = 0, i;
- uint8_t mask = 0;
- int res;
- while (1) // 得到整数和小数的长度
- {
- if (*p == '-')
- {
- mask |= 0X02;
- p++;
- } // 是负数
- if (*p == ',' || (*p == '*'))
- break; // 遇到结束了
- if (*p == '.')
- {
- mask |= 0X01;
- p++;
- } // 遇到小数点了
- else if (*p > '9' || (*p < '0')) // 有非法字符
- {
- ilen = 0;
- flen = 0;
- break;
- }
- if (mask & 0X01)
- flen++;
- else
- ilen++;
- p++;
- }
- if (mask & 0X02)
- buf++; // 去掉负号
- for (i = 0; i < ilen; i++) // 得到整数部分数据
- {
- ires += NMEA_Pow(10, ilen - 1 - i) * (buf[i] - '0');
- }
- if (flen > 5)
- flen = 5; // 最多取5位小数
- *dx = flen; // 小数点位数
- for (i = 0; i < flen; i++) // 得到小数部分数据
- {
- fres += NMEA_Pow(10, flen - 1 - i) * (buf[ilen + 1 + i] - '0');
- }
- res = ires * NMEA_Pow(10, flen) + fres;
- if (mask & 0X02)
- res = -res;
- return res;
- }
- // 分析GPGGA信息
- // gpsx:nmea信息结构体
- // buf:接收到的GPS数据缓冲区首地址
- void NMEA_GPGGA_Analysis(Location *gpsx, uint8_t *buf)
- {
- uint8_t *p1, dx;
- uint8_t posx;
- p1 = (uint8_t *)strstr((const char *)buf, "$GNGGA");
- posx = NMEA_Comma_Pos(p1, 9); // 得到海拔高度
- if (posx != 0XFF)
- gpsx->HGT = NMEA_Str2num(p1 + posx, &dx);
- uint32_t temp;
- float rs;
- posx = NMEA_Comma_Pos(p1, 2); // 得到纬度
- if (posx != 0XFF)
- {
- temp = NMEA_Str2num(p1 + posx, &dx);
- gpsx->LAT = temp / NMEA_Pow(10, dx + 2); // 得到°
- rs = temp % NMEA_Pow(10, dx + 2); // 得到'
- gpsx->LAT = gpsx->LAT * NMEA_Pow(10, 5) + (rs * NMEA_Pow(10, 5 - dx)) / 60; // 转换为°
- }
- // posx=NMEA_Comma_Pos(p1,3); //南纬还是北纬
- // if(posx!=0XFF)gpsx->nshemi=*(p1+posx);
- posx = NMEA_Comma_Pos(p1, 4); // 得到经度
- if (posx != 0XFF)
- {
- temp = NMEA_Str2num(p1 + posx, &dx);
- gpsx->LON = temp / NMEA_Pow(10, dx + 2); // 得到°
- rs = temp % NMEA_Pow(10, dx + 2); // 得到'
- gpsx->LON = gpsx->LON * NMEA_Pow(10, 5) + (rs * NMEA_Pow(10, 5 - dx)) / 60; // 转换为°
- }
- // posx=NMEA_Comma_Pos(p1,5); //东经还是西经
- // if(posx!=0XFF)gpsx->ewhemi=*(p1+posx);
- }
- // 按 HGT(高度)从小到大排序
- void sortByHeight(Location arr[], int n, char mode)
- {
- switch (mode)
- {
- case 1:
- {
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = 0; j < n - i - 1; j++)
- {
- if (arr[j].LON > arr[j + 1].LON)
- {
- // 交换整个结构体
- uint32_t temp = arr[j].LON;
- arr[j].LON = arr[j + 1].LON;
- arr[j + 1].LON = temp;
- }
- }
- }
- }
- break;
- case 2:
- {
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = 0; j < n - i - 1; j++)
- {
- if (arr[j].LAT > arr[j + 1].LAT)
- {
- // 交换整个结构体
- uint32_t temp = arr[j].LAT;
- arr[j].LAT = arr[j + 1].LAT;
- arr[j + 1].LAT = temp;
- }
- }
- }
- }
- break;
- case 3:
- {
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = 0; j < n - i - 1; j++)
- {
- if (arr[j].HGT > arr[j + 1].HGT)
- {
- // 交换整个结构体
- uint32_t temp = arr[j].HGT;
- arr[j].HGT = arr[j + 1].HGT;
- arr[j + 1].HGT = temp;
- }
- }
- }
- }
- break;
- default:
- {
- }
- break;
- }
- }
- /* return
- * init = 10
- * 0 解析成功 mode 1 2
- * 1 没接收到串口数据 同
- * 2 接收到串口数据但不含有 "OK" 无
- * 3 接收到"OK" 但长度小于60-> 返回的字符无数据 长度小于500
- * 4 有数据但没匹配到"+CG" 无
- * 5 匹配到 但是没有对齐数据 -> [空格] 无
- * 6 无 数据全为0
- * 7 不够7次
- * 8 一次也没定上
- * mode 1:开启打印 变动接受数据 (解析较长数据) 2:关闭打印,主动获取并解析(解析较短)
- */
- uint8_t Accept_4GGPS(char mode_1)
- {
- char N_S_f[15] = {0}, E_W_f[15] = {0}, H_m_f[15] = {0};
- int N_S = 0, E_W = 0, H_m = 0;
- char N_S_num = 0, E_W_num = 0, H_m_num = 0;
- char a;
- uint8_t rut1 = 10, i;
-
- if (mode_1 == 2)
- {
- #if 0 // 手动屏蔽
- if(LTEReadFlag == 1)
- {
- if (rt_strstr((const char *)LTE_ReadBuff, "OK") != RT_NULL)
- {
- if (LTEReadCnt > 60)
- {
- rut1 = 4;
- for(i=0;i<LTEReadCnt;i++)
- {
- if((LTE_ReadBuff[i] == '+' )&& (LTE_ReadBuff[i+1] == 'C' ) && (LTE_ReadBuff[i+2] == 'G' ))
- {
- if((LTE_ReadBuff[i+10] == ' ' ))
- {
- const char *ptr = <E_ReadBuff[i+10];
- a = 0;
- while (*ptr != ',') {
- N_S_f[a++] = *ptr++;
- }
- N_S_f[a] = '\0';
- N_S = string_to_u32(N_S_f,&N_S_num);
- loc.LON = N_S * 10000;
- ptr++; // 跳过 ','
- while (*ptr != ',') ptr++;
- ptr++;
- // 3. 解析 纬度
- a = 0;
- while (*ptr != ',') {
- E_W_f[a++] = *ptr++;
- }
- E_W_f[a] = '\0';
- E_W = string_to_u32(E_W_f,&E_W_num);
- loc.LAT = E_W * 10000;
- ptr++; // 跳过 ','
- // 4. 跳过 "E,"
- while (*ptr != ',') ptr++;
- ptr++;
- // 5. 解析 ddmmyy
- while (*ptr != ',') ptr++;
- ptr++; // 跳过 ','
- // 6. 解析hhmmss.s
- while (*ptr != ',') ptr++;
- ptr++; // 跳过 ','
- // 7. 解析 H
- a = 0;
- while (*ptr != ',') {
- H_m_f[a++] = *ptr++;
- }
- H_m_f[a] = '\0';
- H_m = string_to_u32(H_m_f,&H_m_num);
- loc.HGT = H_m * 1000;
- rut1 = 0;
- loc.succ = 1;
- if (loc.succ == 1) // 定位成功 更新位置
- {
- ef_set_env_blob("loc", &loc, sizeof(loc));
- rt_kprintf("Upgrade LOC\n");
- }
- }
- else
- rut1 = 5;
- break;
- }
- }
- }
- else
- rut1 = 3;
- }
- else
- rut1 = 2;
- }
- else
- rut1 = 1;
- #endif
- }
- else if (mode_1 == 1) // 处理被动接受的
- {
- if (Get_GpsFlag == 1)
- {
- rt_kprintf("Accept_4GGPS \n\n");
- if (Gte_GpsCnt > 300)
- {
- #if 0
- NMEA_GPGGA_Analysis(&loc,Get_GpsBuff); //GPGGA解析
- if((loc.LON != 0) || (loc.LAT != 0) || (loc.HGT != 0)) // 解析成功
- loc.succ = 1;
- if (loc.succ == 1) // 定位成功 更新位置
- {
- ef_set_env_blob("loc", &loc, sizeof(loc));
- rt_kprintf("Upgrade LOC\n");
- rut1 = 0;
- }
- else
- rut1 = 6;
- #else
- if (flag_getGPS_ok == 1)
- {
- flag_pointer++;
- flag_getGPS_ok = 0;
- rt_kprintf("flag_pointer:%d\n", flag_pointer);
- }
- NMEA_GPGGA_Analysis(&loc_1[flag_pointer], Get_GpsBuff);
- 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);
- if ((loc_1[flag_pointer].LON != 0) || (loc_1[flag_pointer].LAT != 0) || (loc_1[flag_pointer].HGT != 0))
- {
- flag_getGPS_ok = 1;
- rt_kprintf("\nGPS_ALL!=0");
- }
- if (flag_pointer == 6)
- {
- flag_pointer = 0;
- sortByHeight(loc_1, 7, 1);
- sortByHeight(loc_1, 7, 2);
- sortByHeight(loc_1, 7, 3);
- loc.HGT = (loc_1[2].HGT + loc_1[3].HGT + loc_1[4].HGT) / 3;
- loc.LON = (loc_1[2].LON + loc_1[3].LON + loc_1[4].LON) / 3;
- loc.LAT = (loc_1[2].LAT + loc_1[3].LAT + loc_1[4].LAT) / 3;
- // if ((loc.LON != 0) || (loc.LAT != 0) || (loc.HGT != 0))
- // loc.succ = 1;
- if (loc.succ == 1) // 定位成功 更新位置
- {
- ef_set_env_blob("loc", &loc, sizeof(loc));
- rt_kprintf("Upgrade LOC\n");
- LED_buling(200, 2);
- rut1 = 0;
- }
- else
- rut1 = 6;
- }
- else
- {
- if (flag_pointer == 0)
- rut1 = 8;
- else
- {
- rut1 = 7;
- }
- }
- #endif
- }
- else
- rut1 = 3;
- }
- else
- rut1 = 1;
- }
- return rut1;
- }
- #define Get_GSP_number_times 60000
- void Get_Location(void)
- {
- char num_zx = 4;
- uint32_t i = 0;
- uint32_t Num_on_gps_data = 0;
- uint32_t Ste_num = 0;
- uint8_t rtreturn = 10;
- rt_thread_mdelay(2000);
- while (1)
- {
- data_process(); // 处理接受函数
- rt_thread_mdelay(2000);
- if (DataReturnFlage == 1)
- {
- DataReturnFlage = 0;
- switch (num_zx)
- {
- case 0:
- {
- num_zx = 1;
- };
- break;
- case 1:
- {
- num_zx = 2;
- };
- break;
- case 2:
- {
- num_zx = 3;
- };
- break;
- case 3:
- {
- num_zx = 4;
- };
- break;
- case 4:
- {
- num_zx = 5;
- };
- break;
- case 5:
- {
- num_zx = 6;
- };
- break;
- case 6:
- {
- num_zx = 7;
- };
- break;
- case 7:
- {
- num_zx = 8;
- };
- break;
- case 8:
- {
- num_zx = 9;
- };
- break; // 选择模式
- case 9:
- {
- num_zx = 10;
- };
- break; // mode 1
- case 10:
- {
- num_zx = 11;
- };
- break;
- case 11:
- {
- num_zx = 12;
- };
- break;
- case 12:
- {
- num_zx = 12;
- };
- break;
- case 13:
- {
- num_zx = 14;
- };
- break; // mode 2
- case 14:
- {
- num_zx = 15;
- };
- break;
- case 15:
- {
- num_zx = 16;
- };
- break;
- case 16:
- {
- num_zx = 16;
- };
- break;
- case 17:
- {
- num_zx = 19;
- };
- break;
- case 18:
- {
- num_zx = 19;
- };
- break;
- case 19:
- {
- num_zx = 20;
- };
- break; // 跳转
- case 20:
- {
- num_zx = 20;
- };
- break;
- case 21:
- {
- num_zx = 12;
- };
- break;
- case 22:
- {
- num_zx = 12;
- };
- break;
- case 23:
- {
- num_zx = 12;
- };
- break;
- };
- }
- switch (num_zx)
- {
- case 4:
- {
- SendConfigFunction("AT\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- }
- break;
- case 5:
- {
- SendConfigFunction("AT+RSTSET\r\n", NULL, NULL, NULL, FunctionParseCompare, CompareValue);
- GPS_Start_Cnt = 0;
- rt_thread_mdelay(8000);
- if (LTEReadFlag == 1)
- {
- DataReturnFlage = 1;
- }
- else
- {
- num_zx = 4;
- }
- }
- break;
- case 6:
- {
- SendConfigFunction("AT\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- }
- break;
- case 7:
- {
- SendConfigFunction("ATE0\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- }
- break;
- case 8:
- {
- while (GPS_Start_Cnt < 20000) // 20s开机20s后开启GPS
- {
- rt_thread_mdelay(2000);
- }
- Num_on_gps_data = GPS_Start_Cnt;
- DataReturnFlage = 1;
- format_uartbuf(4);
- }
- break;
- // 方法1
- case 9:
- { // 开启自动打印
- SendConfigFunction("AT+CGPSPORT=3\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- }
- break;
- case 10:
- {
- SendConfigFunction("AT+CGPSPORT?\r\n", NULL, "OK", "+CGPSPORT: 3", FunctionParseCompare, CompareValue);
- rt_thread_mdelay(100);
- }
- break;
- case 11:
- {
- SendConfigFunction("AT+CGPS=1\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- rt_thread_mdelay(400);
- }
- break;
- case 12:
- {
- if (Get_GpsFlag == 1)
- {
- // Get_GpsFlag = 0;
- rtreturn = Accept_4GGPS(1);
- if (rtreturn == 0) // 解析成功
- {
- num_zx = 18;
- }
- if ((rtreturn != 7) && (GPS_Start_Cnt - Num_on_gps_data > Get_GSP_number_times))
- {
- num_zx = 18;
- }
- Get_GpsFlag = 0;
- }
- else
- {
- rt_kprintf("new get err \n");
- }
- DataReturnFlage = 1;
- }
- break;
- // 方法2
- case 13:
- {
- SendConfigFunction("AT+CGPSPORT=0\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- }
- break;
- case 14:
- {
- SendConfigFunction("AT+CGPSPORT?\r\n", NULL, "OK", "+CGPSPORT: 0", FunctionParseCompare, CompareValue);
- rt_thread_mdelay(100);
- }
- break;
- case 15:
- {
- SendConfigFunction("AT+CGPS=1\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- rt_thread_mdelay(400);
- }
- break;
- case 16:
- {
- Usart4_SendString("AT+CGPSINFO\r\n");
- i = 1000;
- while (i > 0) // 总共延时1000ms 等待接受数据
- {
- rt_thread_mdelay(1);
- if (LTEReadFlag == 1)
- break;
- i--;
- }
- rtreturn = Accept_4GGPS(2);
- if (rtreturn == 0)
- {
- num_zx = 18;
- }
- if ((GPS_Start_Cnt - Num_on_gps_data > Get_GSP_number_times)) // 解析超时
- {
- num_zx = 18;
- }
- LTEReadFlag = 0;
- DataReturnFlage = 1;
- }
- break;
- case 17:
- {
- }
- break;
- case 18:
- {
- }
- break;
- case 19:
- {
- SendConfigFunction("AT+CGPS=0\r\n", NULL, "OK", NULL, FunctionParseCompare, CompareValue);
- rt_thread_mdelay(400);
- }
- break;
- case 20:
- {
- return;
- }
- break;
- default:
- {
- rt_kprintf("new GPS case err %d\n", num_zx);
- }
- break;
- }
- }
- }
- char *Send_Buff; // mqtt上报所有的字节 包含AT
- extern uint8_t Init_ok;
- extern int16_t READ_MODBUS_DATA_1(uint8_t times);
- extern uint8_t Start_LTE;
- extern uint8_t RTC_Check;
- extern RTC_DateTypeDef GetDate;
- extern uint8_t Tigger_RTC_Check;
- extern Trans_para Trans_p;
- extern void Get_Location(void);
- static void gprs_entry(void *param)
- {
- uint8_t count = 0;
- while (Start_LTE != One || RTC_Check != One)
- {
- rt_thread_mdelay(100);
- // LOG_I("wait start lte %d %d ",Start_LTE,RTC_Check);
- }
- Config_GPRS_State = -6;
- format_uartbuf(4);
- DataReturnFlage = 0;
- /**
- * 数据采集开始前 尝试读取定位
- */
- uint8_t Get_Loc_day = 0;
- uint8_t IAP_success = 0;
- ef_get_env_blob("IAP_success", &IAP_success, sizeof(IAP_success), NULL);
- ef_get_env_blob("Get_Loc_day", &Get_Loc_day, sizeof(Get_Loc_day), NULL);
- // n次前每次都上报数据
- int startUp_time = 0;
- ef_get_env_blob("startUp_time", &startUp_time, sizeof(startUp_time), NULL);
- if (startUp_time <= 0)
- {
- // 变量不存在,设置默认值并写入
- startUp_time = 1; // 你的默认值
- ef_set_env_blob("startUp_time", &startUp_time, sizeof(startUp_time));
- }
- else
- {
- startUp_time++;
- ef_set_env_blob("startUp_time", &startUp_time, sizeof(startUp_time));
- if (startUp_time < 20)
- {
- Get_Loc_day = 99;
- }
- }
- if (Get_Loc_day == GetDate.Date && (IAP_success != One) && (Tigger_RTC_Check != One)) // 每天执行一次定位(可下发指令触发) 升级成功时不执行
- {
- ef_get_env_blob("loc", &loc, sizeof(loc), NULL);
- rt_kprintf(" read loc \n ");
- }
- else if(IAP_success != One)
- {
- Get_Loc_day = GetDate.Date;
- ef_set_env_blob("Get_Loc_day", &Get_Loc_day, sizeof(Get_Loc_day));
- Get_Location(); // 定位采集器
- }
- Tigger_RTC_Check = Zero;
- /**
- * 数据采集开始
- */
- rt_kprintf("Sensor init %d ms\n", GPS_Start_Cnt);
- while (GPS_Start_Cnt < 5 * 1000)
- {
- rt_thread_mdelay(1000);
- rt_kprintf("Sensor init %dms\n", GPS_Start_Cnt);
- }
- while (sys_info.Sensor_num > 0) // 读取数据
- {
- READ_MODBUS_DATA_1(count); // 数据采集
- if (Init_ok >= sys_info.Sensor_num * 3 /* || 1*/) // 默认让他退出
- {
- // LOG_I("read data succ: %d", Init_ok);
- if (Node_Type == LTE && Trans_p.en == One) // 成功了 退出保护模式
- {
- Trans_p.en = Zero;
- Trans_p.na_cnt = 0;
- ef_set_env_blob("Trans_p", &Trans_p, sizeof(Trans_p));
- Recode_e_code(29, 200);
- LOG_I("exit Trans_p");
- }
- break;
- }
- count++;
- if (count >= 6) // 本来读3次就应该齐活 这读6次还不行 就不试了
- {
- if (Node_Type == LTE && Trans_p.en != One && Init_ok == 0) // 长期NA 进入保护模式
- {
- if (++Trans_p.na_cnt >= 10)
- {
- Trans_p.en = One;
- Recode_e_code(29, 100);
- }
- ef_set_env_blob("Trans_p", &Trans_p, sizeof(Trans_p));
- LOG_W("na_cnt %d en %d", Trans_p.na_cnt, Trans_p.en);
- }
- count = 0;
- break;
- }
- }
- rt_thread_mdelay(30);
- if (Send_Buff == RT_NULL)
- {
- Send_Buff = rt_malloc(send_buf_len); /*at指令发送缓存*/
- }
- if (Send_Buff == RT_NULL)
- {
- rt_kprintf("Send_Buff ×");
- return;
- }
- while (1)
- {
- attask();
- rt_thread_mdelay(10);
- }
- }
- uint8_t str_cmd(void)
- {
- uint8_t result = 0;
- 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))
- {
- cmd_list[0]++;
- result = 1;
- }
- memset(cmd1, 0, 30);
- memset(cmd2, 0, 30);
- return result;
- }
- #define THREAD_PRIORITY 12
- #define THREAD_STACK_SIZE 3072
- #define THREAD_TIMESLICE 5
- int gprs_thread(void)
- {
- rt_thread_t tid1 = RT_NULL;
- tid1 = rt_thread_create("gprs",
- gprs_entry, RT_NULL,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY, THREAD_TIMESLICE);
- /* 如果获得线程控制块,启动这个线程 */
- if (tid1 != RT_NULL)
- rt_thread_startup(tid1);
- return 0;
- }
- INIT_APP_EXPORT(gprs_thread);
- /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2021-09-15 19813 the first version
- */
- extern uint32_t GPS_Start_Cnt; // 开机计算时间
- void FunctionParseCompare(char *data); // 对比判断是否返回想要的数据
- static void gprs_entryzx(void *param)
- {
- // Get_Location();
- }
- int gprs_threadzx(void)
- {
- rt_thread_t tid1 = RT_NULL;
- tid1 = rt_thread_create("gprszx",
- gprs_entryzx, RT_NULL,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY, THREAD_TIMESLICE);
- /* 如果获得线程控制块,启动这个线程 */
- if (tid1 != RT_NULL)
- rt_thread_startup(tid1);
- return 0;
- }
- INIT_APP_EXPORT(gprs_threadzx);
|