利来囯际W66

关注官方微信

中文.

ZLAN NTP Time Module Usage

Published Time:2019-08-30 Author:Shanghai ZLAN Views:17238
1. Summary

Time synchronization is widely used in engineering application, scientific research, people's livelihood and other fields. Time is one of the most important parameters in life and engineering. Sometimes different electronic devices several meters apart need to work together and need to synchronize the clock. At some point two devices thousands of kilometers apart need to make scientific measurements and need to synchronize their clocks; Sometimes the clocks in different classrooms need to be accurate to less than 100 milliseconds in order to be consistent. At present, time synchronization can be achieved through radio waves, satellites and networks. Although network time synchronization accuracy is limited, network time synchronization is the simplest and most economical method.

NTP时间同步示意图

Network time synchronization is generally achieved through the Network Timer Protocol. There are multiple NTP time servers on the Internet, and one server can be selected. The NTP module synchronizes its time with the time server by obtaining the time from the NTP time server. The protocol of NTP is not so simple. In addition to the complexity of the network protocol itself, it also needs to calculate the time difference of path transmission. ZLSN2043S module has been able to automatically realize NTP protocol and synchronize the clock. If the user needs to obtain the internal time of NTP module, he/she can get it by sending serial port instructions through the single-chip serial port.

With this scheme, the time relative error between different MCU (MCU 1, MCU 2, and MCU 3) can be controlled within 2MS, which can basically meet the requirements of ordinary time synchronization.

ZLAN provides a wide range of time synchronization products, from NTP chips to finished equipment.

NTP module type
Type Model
NTP single chip (integration Phy) ZLAN1043 ↴
Ethernet NTP time module ZLSN2043S ↴
Wifi NTP time module ZLSN7046T ↴
NTP time equipment (RS232/485 interface) ZLAN5143 ↴

Note that ZLAN1043 requires firmware version 1.578 and above, and ZLAN7144 requires firmware version 1.490 and above.

2. Usage

The above has introduced the network and hardware structure diagram of NTP time synchronization, the following mainly introduces how to configure the NTP module in software, and how to obtain the current time through the single chip microcomputer serial port.

2.1. Configuration files

The configuration file is used to set which time server to use on the Internet. Create an empty directory ntp_web directory on your computer, and then create an empty ntp.txt file. The contents are as follows:

[NTP]

NTP_SERVER1=a1.a2.a3.a4

NTP_SERVER2=b1.b2.b3.b4

NTP_SERVER3=c1.c2.c3.c4

RE_ARUIRE_TIME=0

Name Meaning
NTP_SERVER1 The IP or domain name of the first time server (up to 29 characters). Up to three servers can be set, but you must start writing NTP_SERVER1 if you have only one, NTP_SERVER1 and NTP_SERVER2 if you have only two.
RE_ARUIRE_TIME If the time has been successfully gained, it is regained every millisecond. The range is 0 to 2147483647. If set to 0, it means that the time will never be regained. When time is not regained, the system relies on the clock inside the CPU to calculate the time. It is recommended to re-let NTP get the time in 10 minutes where the precision is relatively high, that is, RE_ARUIRE_TIME=60000 in NTp.txt. RE_ARUIRE_TIME can be set to 0 if using the use of sending instructions over a serial port to get time.

After saving the configuration file, download the directory through the device edit → firmware and configuration interface, and restart the device after downloading.

配置文件下载

NTP function is enabled only for modules that have downloaded NTP.txt, otherwise NTP function is not available.

2.2. Obtain time through serial port

A fixed instruction is sent to the serial port of the module at the corresponding baud rate (the device baud rate can be set through the network port through Zlvircom and the default is 115200bps) : Ed F2 a3 56 Ca dB 91 84 B0 D7 92 02 0A 51 4A. The module will return 10 bytes of data. If no data is returned or all data returned is 0, it indicates that the time has not been obtained or other errors are made.

串口发送和接收

The return time format is: year - month - day - hour - minute - second - millisecond - week. Of these, year and ms take 2 bytes, and ms above V1.580 is meaningful. For example, the returned data: 07 E3 05 04 0F 24 29 00 00 06 represents 15:35 41 seconds on May 4, 2019, and 00 00 represents the number of milliseconds. 06 means Saturday. After sending the instruction, the instruction will be returned to the serial port within 100ms on return.

Note that the time obtained through serial port has a higher accuracy than the time sent to serial port actively. The time error of different modules is within 2ms. In addition, if you want to be precise to the absolute time, you need to subtract the time for the serial port to send the above 10 bytes at the specified baud rate.

2.3. Sending time to serial port actively

Create a new httpd.txt file in the above NTP.txt directory. Please consult ZLAN engineer for the contents of httpd.txt. After saving, use the download tool to download the file with ntp.txt into the device. This will allow the device to actively output time to the serial port every 1 second. The output date format is the string "2019-05-13 22:23:31" and does not contain the 0 at the end of the string. Among them, the number is 4, and the others are 2. The date and time are separated by Spaces.

The output format for ms is also supported: "2019-05-13 22:23:31.123". Where 123 is milliseconds. Please try to use high baud rate output time.

利来囯际W66