之前在用AT组件包 实现WIFI、蓝牙功能的时候,联网需要先进行地址获取,因用的EC200模组和FC41的指令函数不同,所以我在设备初始化成功之后,新增任务去自动获取模组地址,通过AT+QGEtiP指令,出现如下问题: at反馈回来的地址显示为:包含两行,0D 0A为一行,很奇怪的回显方式
我无法用换行识别获取,所以我利用关键字匹配来获取地址,函数如下
static void ec200x_get_netdev_info(struct rt_work *work, void *work_data)
{
#define AT_ADDR_LEN 32
#define EC200X_INFO_RESP_SIZE 128
at_response_t resp = RT_NULL;
char ip[AT_ADDR_LEN] = {0};
char gateway[AT_ADDR_LEN] = {0};
char netmask[AT_ADDR_LEN] = {0};
char dns[AT_ADDR_LEN] = {0};
char mac[AT_ADDR_LEN] = {0};
ip_addr_t ip_addr;
rt_uint32_t mac_addr[6] = {0};
rt_uint32_t num = 0;
// rt_uint32_t dhcp_stat = 0;
// char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
struct at_device *device = (struct at_device *)work_data;
struct netdev *netdev = device->netdev;
struct at_client *client = device->client;
rt_err_t result = RT_EOK;
int retry_num = 3;
if (work != RT_NULL)
{
rt_free(work);
}
resp = at_create_resp(EC200X_INFO_RESP_SIZE, 2, rt_tick_from_millisecond(1000));
if (resp == RT_NULL)
{
LOG_E("no memory for resp create.");
goto __exit;
}
while (retry_num--)
{
at_obj_exec_cmd(device->client, resp, "AT+QGETIP=station");
if (at_resp_parse_line_args_by_kw(resp, "+QGETIP:","+QGETIP:ip:%[^\"],gate:%[^\"],mask:%[^\"],dns:%[^\"],%*s",
ip, gateway, netmask, dns) <= 0)
{
at_resp_parse_line_args_by_kw(resp,"+QGETIP:","+QGETIP:gate:%[^\"],%*s",gateway);
at_resp_parse_line_args_by_kw(resp,"+QGETIP:","+QGETIP:ip:%[^\"],%*s",ip);
continue;
}
else
{
break;
}
}
if (retry_num < 0)
{
goto __exit;
}
LOG_D("%s device primary ip address: %s", device->name, ip);
LOG_D("%s device primary gate address: %s", device->name, gateway);
inet_aton(ip, &ip_addr);
netdev_low_level_set_ipaddr(netdev, &ip_addr);
at_netif_refresh(&ip_addr, RT_NULL, RT_NULL); // 网卡写入IP地址
/* set netdev info */
inet_aton(gateway, &ip_addr);
netdev_low_level_set_gw(netdev, &ip_addr);
at_netif_refresh(RT_NULL, RT_NULL, &ip_addr); // 网卡写入网关
inet_aton(netmask, &ip_addr);
netdev_low_level_set_netmask(netdev, &ip_addr);
at_netif_refresh(RT_NULL, &ip_addr, RT_NULL); // 网卡写入子网掩码
inet_aton(dns, &ip_addr);
netdev_low_level_set_netmask(netdev, &ip_addr);
at_netif_refresh(RT_NULL, &ip_addr, RT_NULL); // 网卡写入dns地址
netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
result = RT_EOK; // yuanyuan加入
__exit:
if (resp)
{
at_delete_resp(resp);
}
}
获取地址语句为:
if (at_resp_parse_line_args_by_kw(resp, "+QGETIP:","+QGETIP:ip:%[^\"],gate:%[^\"],mask:%[^\"],dns:%[^\"],%*s", ip, gateway, netmask, dns) <= 0)
但最终的结果为:用了%*s,但,后面的回显还是没有屏蔽掉,并且所有的地址全部赋值给了IP
我想问一下,这样的地址要怎么获取,只能通过区字符串吗?