2016-08-21 2 views
0

나는 esp-open-rtos를 사용하여 lwip netconn API를 사용하여 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 repo를 찾을 수 있습니다. github 또는 기타 장소에서 미러를 찾을 수도 있습니다 (업그레이드 될 수 있음).

+0

이 구현을 시도했지만 동일한 방법으로 실패했습니다. lwip_recvfrom은 -1을 반환했습니다. – Anton

+0

그것은 내 프로젝트에서 작동합니다. 포트/드라이버를 확인하십시오. 그리고'lwipopts.h' – kyb

+0

TCP 통신은 제 프로젝트에서 잘 작동합니다. 이것이 UPD에 문제가 될 수 있습니까? – Anton

관련 문제