Nonos 2.2.0 函数espconn_connect返回值的问题

Sam Shen
Posts: 10
Joined: Tue Jun 21, 2016 4:16 pm

Nonos 2.2.0 函数espconn_connect返回值的问题

Postby Sam Shen » Wed Mar 14, 2018 6:17 pm

1.中文环境:
SDK:ESP8266_NONOS_SDK-2.2.0
文档:ESP8266 Non-OS SDK API 参考 版本 2.2 版权 © 2018

2.文档原文:
-------------------------------------------------------------------------------------------------------------------------
功能
连接 TCP server(ESP8266 作为 TCP client)。
-------------------------------------------------------------------------------------------------------------------------
注意
• 如果 espconn_connect 失败,返回⾮非零值,连接未建⽴立,不不会进⼊入任何 espconn callback。
• 建议使⽤用 espconn_port 接⼝口,设置⼀一个可⽤用的端⼝口号。
-------------------------------------------------------------------------------------------------------------------------
函数定义
sint8 espconn_connect(struct espconn *espconn)
-------------------------------------------------------------------------------------------------------------------------
参数
struct espconn *espconn:对应⽹网络连接的结构体
-------------------------------------------------------------------------------------------------------------------------
返回
0:成功
其它:失败,返回错误码
• ESPCONN_ARG:未找到参数 espconn 对应的 TCP 连接
• ESPCONN_MEM:空间不不⾜足
• ESPCONN_ISCONN:连接已经建⽴立
• ESPCONN_RTE:路路由异常
-------------------------------------------------------------------------------------------------------------------------
3,问题:
请问,返回0[成功]做何解释。我用的TCP连接方式,这个TCP连接应该连接,还是不连接呢~?

4,现象:
wifi连接成功,并获取到了IP。 现在esp8266冲当TCPclient连接TCP SERVER。我的TCP SERVER并没有打开。调用espconn_connect时,为什么也返回0呢!!!???

5,代码片段:
//[TCP] tcp connect init
void ICACHE_FLASH_ATTR Tcp_Connect_Init()
{
uint8 server_ip [4] = DATA_SERVER_IP;

os_memset(&ser_tcp_cmd, 0, sizeof(ser_tcp_cmd));
os_memset(&tcp_conn, 0, sizeof(tcp_conn));

os_memcpy(ser_tcp_cmd.remote_ip, server_ip, 4);

ser_tcp_cmd.remote_port = DATA_SERVER_PROT;
ser_tcp_cmd.local_port = espconn_port();

tcp_conn.proto.tcp = &ser_tcp_cmd;
tcp_conn.type = ESPCONN_TCP;
tcp_conn.state = ESPCONN_NONE;

os_printf("[upgrade] tcp_cmd_func enter\n");
// regist connect call back!!!!
if (espconn_regist_connectcb(&tcp_conn, tcp_connect_callback)) {
os_printf("regist con_cb err\n");
return;
}

// connect
uint8 ret = espconn_connect(&tcp_conn);
if (ret != 0) {
os_printf("[upgrade]espconn_create fail, errcode=%d\n", ret);
espconn_disconnect(&tcp_conn);
Esp8266_Send_Connect_Status(CONNECT_SERVER_ERROR);
// os_timer_arm(&tcp_cmd_timer, 5000, 0);
return;
}

os_printf("[upgrade] tcp_cmd_func leave. conn status=%d\n", ret);

}

#define DEBUG 1

// [TCP] call back, connect success
void tcp_connect_callback(void *arg)
{

struct espconn *ptrespconn_tmp = (struct espconn *)arg;
uint8_t mac[6];
char upgrade_request[16];

#if DEBUG
os_printf("[upgrade]tcp_connect_callback enter, net state=%d\n", ptrespconn_tmp->state);
#endif

espconn_set_opt(ptrespconn_tmp, ESPCONN_KEEPALIVE);

espconn_set_keepalive(ptrespconn_tmp, ESPCONN_KEEPINTVL, NULL);

if (ptrespconn_tmp->state == ESPCONN_CONNECT)
{
if (espconn_regist_recvcb(&tcp_conn, esp_tcp_recv_entrance)) {
os_printf("regist recv_cb err\n");
// espconn_mesh_disable(NULL);
return;
}
}
#if DEBUG
else
{
os_printf("[upgrade] no ESPCONN_CONNECT \n");
}
#endif // DEBUG

Esp8266_Send_Connect_Status(CONNECT_OK);

#if DEBUG
os_printf("[upgrade] tcp_connect_callback leave\n");
#endif
}

6,串口日志

没有打开TCPSERVER的日志
\0r\0l溫r運0?鈔?堗 ?傡p?巶焅0?挓菕茕?p  騨n??騨膾茕 ?b?l`$`黳騨嗝軃 鄠抢l€ € € b?n忏n滥帓?瀸p 岒nn?牧寧\0l`鼝?俷?帉l`涓`rn|拻n?庎馇鉲`箏睦 ??俷?帉 岒nn?娜l`nn嗝躙0 鄏?廛 € p p捾<泥 ?b滥緙騨?牧?\0l`鼝?俷€rnr帓?睦 ??rp騨嗝軃 鄏?廛??p p捾<拿 庈 p 岒nn?睦 r??俷??nr帓?睦 ?悓?r咣抧?庎偳躭`鼘廛\0?俷冷?r茇騨鉨 倢巐`臏膾苊呢b?r??鈔?芨挏\0 r屸茜I8倢?怱DK version:2.2.0(f28eaf2)
mode : sta(60:01:94:02:a4:06)
add if0
ret=1, ssid=them, pwd=qwert123456
scandone
event 2
mode: 0 -> 3
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt

connected with them, channel 11
dhcp client start...
event 0
connect to ssid them, channel 11
ip:192.168.0.215,mask:255.255.255.0,gw:192.168.0.1
event 3
ip:192.168.0.215,mask:255.255.255.0,gw:192.168.0.1[upgrade] tcp_cmd_func enter
[upgrade] tcp_cmd_func leave. conn status=0
pm open,type:2 0


打开了TCPSERVER的日志
\0r\0l溫r運0?鈔?堗 ?傡p?巶運0?掃菕茕?p  騨n??騨膾茕 ?b?塺rp騨嗝躙0 鄠抢l€ € € b?n忏n滥帓?瀸p 岒nn?牧寧\0l`鼝?俷?帉l`涓`rn|拻n?庎馇鉲`箏睦 ??俷??岒nn?娜l`nn嗝軃 鄏?廛 € p p捾<泥 ?b滥>~騨?牧?\0l`鼝?俷?rnr帓?睦 ??rp騨嗝躙0 鄏?廛??p p捾<拿 庈 p 岒nn?睦 r??俷??nr帓?睦 ?悓?r咣抧€鄠擒l`鼘廛\0?俷€?r茇騨鉨 倢巐`臏膾苊呢b?r??鈔郼?挏\0 r屸茜?倢?怱DK version:2.2.0(f28eaf2)
mode : sta(60:01:94:02:a4:06)
add if0
ret=1, ssid=them, pwd=qwert123456
scandone
event 2
mode: 0 -> 3
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt

connected with them, channel 11
dhcp client start...
event 0
connect to ssid them, channel 11
ip:192.168.0.215,mask:255.255.255.0,gw:192.168.0.1
event 3
ip:192.168.0.215,mask:255.255.255.0,gw:192.168.0.1[upgrade] tcp_cmd_func enter
[upgrade] tcp_cmd_func leave. conn status=0
[upgrade]tcp_connect_callback enter, net state=3
<[upgrade] tcp_connect_callback leave
pm open,type:2 0


明白情况的大侠,请说一下,怎么回事。

Her Majesty
Posts: 283
Joined: Mon Oct 27, 2014 11:09 am

Re: Nonos 2.2.0 函数espconn_connect返回值的问题

Postby Her Majesty » Tue Mar 27, 2018 5:04 pm

你可以注册一个 reconnect callback,它应该是进入 reconnect callback 中了,那是异常断开的回调函数。
正常断开是 disconnect callback。

Who is online

Users browsing this forum: Google [Bot] and 3 guests