2016-08-21 153 views
0

我正在尝试使用lwip netconn API(使用esp-open-rtos)为ESP8266编写简单的SNTP客户端。问题是我无法从服务器接收答案。lwip netconn api - 无法接收来自SNTP服务器的答案

代码(没有错误检查和调试messgaes):

#include <string.h> 

#include <lwip/api.h> 
#include <lwip/err.h> 

struct sntp_message 
{ 
    u8_t li : 2; 
    u8_t vn : 3; 
    u8_t mode : 3; 
    u8_t stratum; 
    u8_t poll; 
    u8_t precision; 
    u32_t root_delay; 
    u32_t root_dispersion; 
    u32_t reference_identifier; 
    u32_t reference_timestamp[2]; 
    u32_t originate_timestamp[2]; 
    u32_t receive_timestamp[2]; 
    u32_t transmit_timestamp[2]; 
} __attribute__((packed)); 

#define SNTP_MSG_LEN (sizeof(struct sntp_message)) 

int16_t sntp_sync(char* server) 
{ 
    err_t err; 
    int16_t result = ERR_OK; 
    ip_addr_t sntp_server_address; 
    struct netconn* connection = NULL; 
    struct netbuf* send_buffer = NULL; 
    struct netbuf* receive_buffer = NULL; 
    struct sntp_message* send_buffer_data = NULL; 

    err = netconn_gethostbyname(server, &sntp_server_address); 
    connection = netconn_new(NETCONN_UDP); 
    err = netconn_connect(connection, &sntp_server_address, 123); 
    send_buffer = netbuf_new(); 
    send_buffer_data = netbuf_alloc(send_buffer, SNTP_MSG_LEN); 
    memset(send_buffer_data, 0, SNTP_MSG_LEN); 
    send_buffer_data->vn = 4; 
    send_buffer_data->mode = 3; // Mode client. 
    err = netconn_send(connection, send_buffer); 
    err = netconn_recv(connection, &receive_buffer); // Here netconn_recv block my thread, and no data received. If i set timeout, i have timeout error. 
    return result; 
} 

什么都可以错在我的代码?在接收数据(通过netconn_bind)之前,我需要绑定我的连接吗?或者我忘了其他的东西?

回答

0

不要发明自行车。 Simon Goldschmidt已经为LwIP编写了SNTP。 Link。在那里你可以找到lwip-contrib描述和git回购。你也可以在github上或任何地方寻找镜像(可能会升级)。

+0

我试过这个实现,但是它失败了 - lwip_recvfrom返回-1。 – Anton

+0

它在我的项目中工作。检查你的端口/驱动程序。而'lwipopts.h' – kyb

+0

TCP通信正常工作我的项目。这会对UPD造成麻烦吗? – Anton

相关问题