硬件平台为:STM32F103RCT6 + ESP8266软件版本为:RTT -- 4.02 AT version:1.2.0.0
软件流程:上电后自动连接到指定wifi --->> 开启服务端多链接模式 -->> PC端通过TCP调试助手连接到ESP8266 -->> TCP助手发送字符串到 ESP8266 -->> STM32解析数据后返回数据到客户端。
问题描述:STM32解析数据后返回数据到客户端步骤出错,在第一次连接成功后发送数据是可以正常返回

但是接下来就再也无法解析+IPD开头的数据了

以下为我判断可能出错的代码段(不调用此函数+IPD数据头的数据都可以正常接收):
rt_err_t send_data_to_client(unsigned char client_num, unsigned char *data, unsigned short len)
{
at_response_t resp = RT_NULL;
rt_err_t result = RT_EOK;
char cmd_buf[128] = {0};
rt_uint16_t wait_times = 500;
rt_uint8_t *test_buf = "Hello";
/* 创建响应结构体,设置最大支持响应数据长度为 512 字节,响应数据行数无限制,超时时间为 5 秒 */
resp = at_create_resp(512, 0, 5 * RT_TICK_PER_SECOND);
if (!resp)
{
LOG_E("No memory for response structure!");
return -RT_ENOMEM;
}
// 请求发送
sprintf(cmd_buf,"AT+CIPSEND=%d,%d\r\n", client_num, len);
result = at_exec_cmd(resp, cmd_buf);
at_set_end_sign('\n');
if (result != RT_EOK)
{
LOG_E("AT client send commands failed or return response error!");
LOG_E("Error commands:%s", cmd_buf);
goto __exit;
}
/* Print response line buffer */
// show_recv_msg(resp);
// rt_device_write(device, 0, test_buf, 5);
at_exec_cmd(RT_NULL, test_buf);
// at_exec_cmd(RT_NULL, (char*)data);
// show_recv_msg(resp);
while (wait_times--) {
if (WifiInfo.recv_send_data_len == len) {
WifiInfo.recv_send_data_len = 0;
result = RT_EOK;
break;
}
rt_thread_mdelay(5);
}
if (wait_times == 0) {
result = RT_ETIMEOUT;
}
__exit:
if(resp)
{
at_delete_resp(resp);
}
return result;
}
这是我的ipd数据处理urc函数:
static void urc_func_ipd_data(struct at_client *client, const char *data, rt_size_t size)
{
rt_kprintf("in in in\r\n"); // 发送控制指令
int device_socket = 0;
rt_int32_t timeout = 0;
rt_uint16_t bfsz = 0, temp_size = 0;
char *recv_buf = RT_NULL, temp[8] = {0};
RT_ASSERT(data && size);
/* get the at deveice socket and receive buffer size by receive data */
sscanf(data, "+IPD,%d,%d:", &device_socket, (int *) &bfsz);
/* set receive timeout by receive buffer length, not less than 10ms */
timeout = bfsz > 3 ? bfsz : 3;
if (device_socket < 0 || bfsz == 0)
return;
recv_buf = (char *) rt_calloc(1, bfsz);
if (recv_buf == RT_NULL)
{
LOG_E("no memory receive buffer(%d).", bfsz);
/* read and clean the coming data */
while (temp_size < bfsz)
{
if (bfsz - temp_size > sizeof(temp))
{
at_client_obj_recv(client, temp, sizeof(temp), timeout);
}
else
{
at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
}
temp_size += sizeof(temp);
}
return;
}
/* sync receive data */
if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
{
LOG_E("device receive size(%d) data failed.", bfsz);
rt_free(recv_buf);
return;
}
rt_kprintf("WiFi RECV Command[%d][%d]:", device_socket,bfsz); // 发送控制指令
for (int i = 0; i < bfsz; i++) {
rt_kprintf("%02X ", *(recv_buf + i)); // 发送控制指令
}
rt_kprintf("\r\n"); // 发送控制指令
dealServerMessage((rt_uint8_t*)recv_buf, device_socket, bfsz);
rt_free(recv_buf);
}
static struct at_urc urc_table[] = {
{"+IPD,", ":", urc_func_ipd_data},
// {"", ">", urc_func_clear},
{"SEND OK", "\r\n", urc_send_func},
{"SEND FAIL", "\r\n", urc_send_func},
{"Recv", "bytes\r\n", urc_send_bfsz_func},
{"", ",CLOSED\r\n", urc_func_close},
{"", ",CONNECT\r\n", urc_func_connect},
{"WIFI GOT IP", "\r\n", urc_conn_func},
};
求指点!!
查看更多