开发环境:野火的stm32f407,rt-thread studio版本是版本: 2.2.6,stm32f4的资源包为0.2.2。以RT-Thread中Lan8720和lwip协议栈的使用文章创建的工程为基础。
ntp代码参考网上的例程,参考1;参考2。
新建ntptest.c测试文件,移植以上参考的ntp代码,整理后如下:
#include <rtthread.h>
#include <lwip/sockets.h>
#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的时间差
#define SEC_TIME_ZONE + (86060) //Beijing,GMT+8, 时区差
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字节的报文
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
//后面分别发送了4个不同版本,都可以收到数据。
for(i=1;i<48;i++)NTP_Data[i]=0;//剩余的47字节为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 err\n");
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];//由于本文的时间精度要求不高,故自己用服务器返回的时间作为对时的时间,并未用公式:时间差offset=((T2-T1)+(T3-T4))/2。而是用T3作为对时基准时间。
local_timestamp = packet.txTm_s - NTP_TIMESTAMP_DELTA;//减去1970和1900的差值
rt_kprintf( "unix_timestamp=%ld\n",local_timestamp);
local_timestamp +=SEC_TIME_ZONE; //加上北京的时间差,GMT+8
Net_time = localtime(&local_timestamp); //秒数转换位标准时间
rt_kprintf("%d-%d-%d %d:%d:%d\r\n",(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); //打印出时间
}
}
return 0;
}
主函数调用,代码如下:
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;
}
编译无报错,下载测试,测试结果如下:
选择其中一个时间戳,利用时间戳转换工具比对,结果如下:
结果正常。
原作者:YZRD