開發(fā)環(huán)境:野火的stm32f407,rt-thread studio版本是版本: 2.2.6,stm32f4的資源包為0.2.2。以RT-Thread中Lan8720和lwip協(xié)議棧的使用文章創(chuàng)建的工程為基礎(chǔ)。
新建ntptest.c測試文件,移植以上參考的ntp代碼,整理后如下:
#include
#include
#define SERVER_IP "114.67.237.130"
#define SERVER_PORT 123
#define TIMEOUT 10
//宏定義
#define NTP_TIMESTAMP_DELTA 2208988800ull //number of seconds between 1900 and 1970,1900-1970的時(shí)間差
#define SEC_TIME_ZONE + (86060) //Beijing,GMT+8, 時(shí)區(qū)差
typedef struct
{
uint8_t li_vn_mode; // Eight bits. li, vn, and mode.
// li. Two bits. Leap indicator.
// vn. Three bits. Version number of the protocol.
// mode. Three bits. Client will pick mode 3 for client.
uint8_t stratum; // Eight bits. Stratum level of the local clock.
uint8_t poll; // Eight bits. Maximum interval between successive messages.
uint8_t precision; // Eight bits. Precision of the local clock.
uint32_t rootDelay; // 32 bits. Total round trip delay time.
uint32_t rootDispersion; // 32 bits. Max error aloud from primary clock source.
uint32_t refId; // 32 bits. Reference clock identifier.
uint32_t refTm_s; // 32 bits. Reference time-stamp seconds.
uint32_t refTm_f; // 32 bits. Reference time-stamp fraction of a second.
uint32_t origTm_s; // 32 bits. Originate time-stamp seconds.
uint32_t origTm_f; // 32 bits. Originate time-stamp fraction of a second.
uint32_t rxTm_s; // 32 bits. Received time-stamp seconds.
uint32_t rxTm_f; // 32 bits. Received time-stamp fraction of a second.
uint32_t txTm_s; // 32 bits and the most important field the client cares about. Transmit time-stamp seconds.
uint32_t txTm_f; // 32 bits. Transmit time-stamp fraction of a second.
} ntp_packet; // Total: 48 bytes.
int sockfd=-1;
int maxfd1;
fd_set readfds;
struct timeval timeout;
struct sockaddr_in servaddr;
int len;
int i;
uint8_t buf[48];
long local_timestamp;//uint32_t local_timestamp;
ntp_packet packet ;
struct tm * Net_time;
uint8_t NTP_Data[48]; //48字節(jié)的報(bào)文
int bsp_ntp_init(void)
{
NTP_Data[0]=0xa3;
//00100011, 0xa3,100 版本4
//00011011, 0x1b,011 版本3
//00010011, 0x13,010 版本2
//00001011, 0x0b,001 版本1
//后面分別發(fā)送了4個(gè)不同版本,都可以收到數(shù)據(jù)。
for(i=1;i<48;i++)NTP_Data[i]=0;//剩余的47字節(jié)為0
// servaddr.sin_family = AF_INET;
// servaddr.sin_port = htons(SERVER_PORT);
// servaddr.sin_addr.s_addr = inet_host(SERVER_IP);
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, SERVER_IP, &servaddr.sin_addr.s_addr);
servaddr.sin_port = htons(SERVER_PORT);
if(sockfd>0)
{
//close(sockfd);
closesocket(sockfd);
sockfd=-1;
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
return -1;
}
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) != 0)
{
//close(sockfd);
closesocket(sockfd);
sockfd=-1;
return -1;
}
return 0;
}
int bsp_read_ntp(void)
{
if(sockfd<0)
{
if(bsp_ntp_init()<0)
{
rt_kprintf("sockfd is errn");
return -1;
}
}
send(sockfd, NTP_Data, sizeof(NTP_Data), 0);
rt_kprintf("send n");
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
maxfd1 = sockfd + 1;
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
if (select(maxfd1, &readfds, NULL, NULL, &timeout) > 0)
{
if (FD_ISSET(sockfd, &readfds))
{
if ((len = recv(sockfd, buf, sizeof(buf), 0)) < 0)
{
//close(sockfd);
closesocket(sockfd);
sockfd=-1;
return -1;
}
packet.txTm_s = buf[40]<<24 | buf[40+1]<<16|buf[40+2]<<8 |buf[40+3];//由于本文的時(shí)間精度要求不高,故自己用服務(wù)器返回的時(shí)間作為對(duì)時(shí)的時(shí)間,并未用公式:時(shí)間差offset=((T2-T1)+(T3-T4))/2。而是用T3作為對(duì)時(shí)基準(zhǔn)時(shí)間。
local_timestamp = packet.txTm_s - NTP_TIMESTAMP_DELTA;//減去1970和1900的差值
rt_kprintf( "unix_timestamp=%ldn",local_timestamp);
local_timestamp +=SEC_TIME_ZONE; //加上北京的時(shí)間差,GMT+8
Net_time = localtime(&local_timestamp); //秒數(shù)轉(zhuǎn)換位標(biāo)準(zhǔn)時(shí)間
rt_kprintf("%d-%d-%d %d:%d:%drn",(Net_time->tm_year)+1900, (Net_time->tm_mon)+1, Net_time->tm_mday, Net_time->tm_hour,Net_time->tm_min,Net_time->tm_sec); //打印出時(shí)間
}
}
return 0;
}
主函數(shù)調(diào)用,代碼如下:
extern int bsp_ntp_init(void);
extern int bsp_read_ntp(void);
int main(void)
{
int count = 1;
rt_thread_mdelay(5000);
bsp_ntp_init();
while (count++)
{
LOG_D("Hello RT-Thread!");
rt_thread_mdelay(1000);
bsp_read_ntp();
}
return RT_EOK;
}
編譯無報(bào)錯(cuò),下載測試,測試結(jié)果如下:
選擇其中一個(gè)時(shí)間戳,利用時(shí)間戳轉(zhuǎn)換工具比對(duì),結(jié)果如下:
結(jié)果正常。
-
NTP
+關(guān)注
關(guān)注
1文章
141瀏覽量
13847 -
LwIP協(xié)議棧
+關(guān)注
關(guān)注
0文章
19瀏覽量
7307 -
LAN8720
+關(guān)注
關(guān)注
0文章
4瀏覽量
10996 -
STM32F407
+關(guān)注
關(guān)注
15文章
187瀏覽量
29205 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1239瀏覽量
39435
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論