我使用AT client来实现与esp8266之间的通信,开始esp8266连接服务器正常,一段时间后(时间不固定),esp8266会不断上线、下线,通过查看日志后发现是程序不断进行重连操作,具体日志如下:
[0m[D/at.clnt] execute command (AT+CIPSTART=0,"TCP","183.236.25.188",18135,60) timeout (5000 ticks)![0m[2021-11-01 10:24:36.197]
[0m[D/at.skt.esp] espo device socket (0) connect failed, the socket was not be closed and now will connect retry.myreslt=-2[0m[2021-11-01 10:24:36.197]
[0m[D/at.clnt] execute command (AT+CIPCLOSE=0) timeout (300 ticks)![0m[2021-11-01 10:24:36.514]
这似乎是因为esp8266没有回复指令的问题,于是我使用串口助手查看esp8266是否回应是数据,结果发现确实有数据回复,具体数据如下:
0,CLOSED[2021-11-01 11:07:52.759]
[2021-11-01 11:07:52.759]
OK[2021-11-01 11:07:52.759]
UNLINK[2021-11-01 11:07:53.173]
[2021-11-01 11:07:53.173]
ERROR[2021-11-01 11:07:53.173]
0,CONNECT[2021-11-01 11:07:59.485]
[2021-11-01 11:07:59.485]
OK[2021-11-01 11:07:59.485]
[2021-11-01 11:08:01.525]
于是我以为是串口程序出BUG了,于是进行仿真查看,好不容易等到出现这种情况了,马上在串口接收中断打了断点,结果发现,每次串口有数据,都会触发串口中断,于是进一步到at_client.c这个文件里面去查看,发现每次数据都会进入at_client_rx_ind这个函数,然后在这个函数打断点,每次也能运行到rt_sem_release(at_client_table[idx].rx_notice)这句,然后看了一下信号量的值一直在增大,因此肯定是接收这个信号量的地方出了问题,然后找到at_client_getchar这个函数,在里面打断点,结果一直没进入,然后往上反推回去,找到at_client_obj_recv、at_recv_readline这两个函数,发现这个函数也一直没进入,再往上找,at_recv_readline这个函数在client_parser调用,然后client_parser里面是一个死循环,结果在循环里面打断点,也一直未运行到。涉及到的函数如下:
static rt_err_t at_client_rx_ind(rt_device_t dev, rt_size_t size)
{
int idx = 0;
for (idx = 0; idx < AT_CLIENT_NUM_MAX; idx++)
{
if (at_client_table[idx].device == dev && size > 0)
{
rt_sem_release(at_client_table[idx].rx_notice);
}
}
return RT_EOK;
}
static rt_err_t at_client_getchar(at_client_t client, char *ch, rt_int32_t timeout)
{
rt_err_t result = RT_EOK;
__retry:
result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout));
if (result != RT_EOK)
{
return result;
}
if(rt_device_read(client->device, 0, ch, 1) == 1)
{
return RT_EOK;
}
else
{
goto __retry;
}
}
rt_size_t at_client_obj_recv(at_client_t client, char buf, rt_size_t size, rt_int32_t timeout)
{
rt_size_t read_idx = 0;
rt_err_t result = RT_EOK;
char ch;
RT_ASSERT(buf);
if (client == RT_NULL)
{
LOG_E("input AT Client object is NULL, please create or get AT Client object!");
return 0;
}
while (1)
{
if (read_idx < size)
{
result = at_client_getchar(client, &ch, timeout);
if (result != RT_EOK)
{
LOG_E("AT Client receive failed, uart device get data error(%d)", result);
return 0;
}
buf[read_idx++] = ch;
}
else
{
break;
}
}
#ifdef AT_PRINT_RAW_CMD
at_print_raw_cmd("urc_recv", buf, size);
#endif
return read_idx;
}
static int at_recv_readline(at_client_t client)
{
rt_size_t read_len = 0;
char ch = 0, last_ch = 0;
rt_bool_t is_full = RT_FALSE;
rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
client->recv_line_len = 0;
while (1)
{
at_client_getchar(client, &ch, RT_WAITING_FOREVER);
if (read_len < client->recv_bufsz)
{
client->recv_line_buf[read_len++] = ch;
client->recv_line_len = read_len;
}
else
{
is_full = RT_TRUE;
}
/ is newline or URC data /
if ((ch == '\n' && last_ch == '\r') || (client->end_sign != 0 && ch == client->end_sign)
|| get_urc_obj(client))
{
if (is_full)
{
LOG_E("read line failed. The line data length is out of buffer size(%d)!", client->recv_bufsz);
rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
client->recv_line_len = 0;
return -RT_EFULL;
}
break;
}
last_ch = ch;
}
#ifdef AT_PRINT_RAW_CMD
at_print_raw_cmd("recvline", client->recv_line_buf, read_len);
#endif
return read_len;
}
static void client_parser(at_client_t client)
{
const struct at_urc urc;
while(1)
{
if (at_recv_readline(client) > 0)
{
if ((urc = get_urc_obj(client)) != RT_NULL)
{
/ current receive is request, try to execute related operations /
if (urc->func != RT_NULL)
{
urc->func(client, client->recv_line_buf, client->recv_line_len);
}
}
else if (client->resp != RT_NULL)
{
at_response_t resp = client->resp;
/ current receive is response /
client->recv_line_buf[client->recv_line_len - 1] = '\0';
if (resp->buf_len + client->recv_line_len < resp->buf_size)
{
/ copy response lines, separated by '\0' /
rt_memcpy(resp->buf + resp->buf_len, client->recv_line_buf, client->recv_line_len);
/ update the current response information /
resp->buf_len += client->recv_line_len;
resp->line_counts++;
}
else
{
client->resp_status = AT_RESP_BUFF_FULL;
LOG_E("Read response buffer failed. The Response buffer size is out of buffer size(%d)!", resp->buf_size);
}
/ check response result /
if (rt_memcmp(client->recv_line_buf, AT_RESP_END_OK, rt_strlen(AT_RESP_END_OK)) == 0
&& resp->line_num == 0)
{
/ get the end data by response result, return response state END_OK. /
client->resp_status = AT_RESP_OK;
}
else if (rt_strstr(client->recv_line_buf, AT_RESP_END_ERROR)
|| (rt_memcmp(client->recv_line_buf, AT_RESP_END_FAIL, rt_strlen(AT_RESP_END_FAIL)) == 0))
{
client->resp_status = AT_RESP_ERROR;
}
else if (resp->line_counts == resp->line_num && resp->line_num)
{
/ get the end data by response line, return response state END_OK./
client->resp_status = AT_RESP_OK;
}
else
{
continue;
}
client->resp = RT_NULL;
rt_sem_release(client->resp_notice);
}
else
{
// log_d("unrecognized line: %.*s", client->recv_line_len, client->recv_line_buf);
}
}
}
}