This function block from run in task Back, gets the value of UTC from a time server, in this article it explains how to activate the server on a PC. activating Query the time value request is made from the server whose URL or IP address is passed in NTPServer. The FB queries the server and if it receives a correct answer it returns to UTCTime the UTC time value in Epoch. In Offset the difference in mS between the returned UTC value and the reference NTP clock is returned.
At the end of the execution, the output is activated Done, to carry out a new time reading, the input must be deactivated and then reactivated Query. In RTDelay the time necessary to send the request and to receive the response from the NTP server has returned.
NTP server list
An NTP server is able to estimate and compensate for systematic errors of the system hardware clock, there are several NTP servers that you can connect to to update the system clock. The project pool.ntp.org is one of them, it presents itself as a large virtual cluster of timeservers that provide a reliable and easy-to-use NTP service for millions of users. It is possible to configure any of these servers, the names 0, 1, 2 and 3.pool.ntp.org they point to a random server set that changes every hour.
- 0.pool.ntp.org
- 1.pool.ntp.org
- 2.pool.ntp.org
- 3.pool.ntp.org
Function lock
CODESYS: Not available
LogicLab: eLLabNetworkLib
Description
Query (BOOL) Request activation command. To re-execute the command, disable and then re-enable the input.
SpyOn (BOOL) If active, it allows you to spy on the operation of the FB.
NTPServer (@STRING) NTP server definition pointer. Both IP and URL can be defined.
Done (BOOL) It is activated at the end of the command execution.
Ok (BOOL) Active for a program loop if query is successful.
Fault (BOOL) Active for a loop if command execution error.
RTDelay (REAL) Round trip delay, time to send the request and receive the response from the server (mS).
UTCTime (UDINT) Date / Time in UTC expressed in Epoch time.
Offset (REAL) Difference between the returned UTC value and the reference NTP clock (mS).
Spy trigger
Se SpyOn active you can use the spy console to check the operation of the FB. There are various levels of triggers.
Trigger levels
trigger | Description |
---|---|
16 00000001 # | Tx: Sending request to the server. |
16 00000002 # | Rx: Receiving response from the server. |
16 20000000 # | Lg: Log message. |
16 40000000 # | Er: Execution error. |
Examples
How to use the examples.
ST_SNTPRequest: Send a request to the NTP server every 30 seconds 0.pool.ntp.org and if the answer is correct, the real time clock is updated.
ST_SystemClockSync: When the program starts, it updates the system clock. If the request is in error, the request is repeated quickly. Every hour it performs an update.
LogicLab (Ptp119, ST_SNTPRequest)
PROGRAM ST_SNTPRequest
VAR
i : UDINT; (* Auxiliary variable *)
TimeBf : UDINT; (* Time buffer (mS) *)
SNTPReq : SNTPRequest; (* NTP request *)
CheckDate : ARRAY[0..1] OF DATE_AND_TIME; (* To check Date/Time *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_SNTPRequest"
// *****************************************************************************
// At every 30 seconds it's a NTP request is sent and the real time clock is
// updated.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Program initializations.
IF (SysFirstLoop) THEN
SNTPReq.SpyOn:=TRUE; //Spy On
SNTPReq.NTPServer:=ADR('0.pool.ntp.org'); //NTP server
END_IF;
// -------------------------------------------------------------------------
// TIME SYNCRONIZATION
// -------------------------------------------------------------------------
// Check if time threshold to perform synchronization (Every 30 Sec).
// Program initializations.
IF ((SysTimeGetMs()-TimeBf) > TO_UDINT(T#30s)) THEN
TimeBf:=SysTimeGetMs(); //Time buffer (mS)
SNTPReq.Query:=TRUE; //Query On
END_IF;
// Manage the NTP request to the server.
SNTPReq(); //NTP request
// Execution done, if Ok update the real time clock.
IF (SNTPReq.Done) THEN
SNTPReq.Query:=FALSE; //Query On
CheckDate[0]:=TO_DATE_AND_TIME(SysDateGetS()); //System Date/Time
CheckDate[1]:=TO_DATE_AND_TIME(SNTPReq.UTCTime); //NTP Date/Time
IF (SNTPReq.Ok) THEN i:=SysDateSetS(SNTPReq.UTCTime); END_IF;
END_IF;
// [End of file]
LogicLab (Ptp181, ST_SystemClockSync)
PROGRAM ST_SystemClockSync
VAR
i : UDINT; (* Auxiliary variable *)
ClockSync : BOOL; (* Clock syncronized *)
TimeBf : UDINT; (* Time buffer (mS) *)
SNTPReq : SNTPRequest; (* NTP request *)
END_VAR
// *****************************************************************************
// PROGRAM "SystemClockSync"
// *****************************************************************************
// Sends a NTP request to synchronize the system clock
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Program initializations.
IF (SysFirstLoop) THEN
ClockSync:=FALSE; //Clock syncronized
SNTPReq.Query:=TRUE; //Query On
SNTPReq.SpyOn:=FALSE; //Spy On
SNTPReq.NTPServer:=ADR('0.pool.ntp.org'); //NTP server
END_IF;
// -------------------------------------------------------------------------
// TIME SYNCRONIZATION
// -------------------------------------------------------------------------
// If system clock not synchronized executes the request faster.
// If synchronized executes it slowly.
IF NOT(ClockSync) THEN
IF ((SysTimeGetMs()-TimeBf) > TO_UDINT(T#30s)) THEN TimeBf:=SysTimeGetMs(); SNTPReq.Query:=TRUE; END_IF;
ELSE
IF ((SysTimeGetMs()-TimeBf) > TO_UDINT(T#1h)) THEN TimeBf:=SysTimeGetMs(); SNTPReq.Query:=TRUE; END_IF;
END_IF;
// On execution done, if Ok update the real time clock.
SNTPReq(); //NTP request
IF (SNTPReq.Done) THEN
SNTPReq.Query:=FALSE; //Query On
IF (SNTPReq.Ok) THEN i:=SysDateSetS(SNTPReq.UTCTime); ClockSync:=TRUE; END_IF;
END_IF;
// [End of file]