Questo blocco funzione da eseguire in task Back, acquisisce da un server di tempo il valore di UTC. Attivando Query viene eseguita la richiesta del valore di tempo dal server il cui URL o indirizzo IP è passato in NTPServer sulla porta definita in Port. L’FB interroga il server e se riceve risposta corretta si attiva per un loop Ok ed in in UTCTime è ritornato il valore di tempo UTC in Epoch.
Terminata l’esecuzione si attiva l’uscita Done, per effettuare una nuova lettura di tempo occorre disattivare e poi riattivare l’ingresso Query. In RTDelay è ritornato il tempo necessario all’invio della richiesta ed alla ricezione della risposta dal server NTP.
Approfondimenti
- In questo articolo come attivare un server NTP su PC.
- In questo articolo informazioni sulla sincronizzazione orario tra sistemi.
Descrizione
Query (BOOL) Comando attivazione richiesta. Per rieseguire il comando disabilitare e poi riabilitare l’ingresso.
SpyOn (BOOL) Se attivo permette di spiare il funzionamento della FB.
Port (UINT) Definizione porta a cui inviare la richiesta su server, default 123.
NTPServer (@STRING) Pointer definizione server NTP. Può essere definito sia l’IP che l’URL.
Done (BOOL) Si attiva al termine della esecuzione comando.
Ok (BOOL) Attivo per un loop di programma se query eseguita correttamente.
Fault (BOOL) Attivo per un loop se errore esecuzione comando.
RTDelay (TIME) Round trip delay, tempo per inviare la richiesta e ricevere la risposta dal server.
NTPPacket (NTPv4_PACKET) Messaggio NTP ricevuto da server (Descrizione).
UTCTime (LDATE_AND_TIME) Data/Ora in UTC espressa in Epoch time.
Requests (UDINT) Numero di richieste eseguite.
Errors (UDINT) Numero di errori esecuzione.

Trigger di spy
Se SpyOn attivo è possibile utilizzare utilizzare la console di spionaggio per verificare il funzionamento della FB. Sono previsti vari livelli di triggers.
Livelli di trigger
Trigger | Descrizione |
---|---|
16#00000001 | Tx: Invio richiesta al server. |
16#00000002 | Rx: Ricezione risposta dal server. |
16#10000000 | Lg: Messaggio di log. |
16#40000000 | Er: Errore esecuzione. |
Esempi
ST_SNTPClient: Esegue aggiornamento clock di sistema, se clock non aggiornato la richiesta viene ripetuta frequentemente. Se clock aggiornato la richiesta viene eseguita con meno frequenza.
ST_SNTPClientNTPTimestamp: Ad ogni tempo definito esegue invio richiesta NTP al server, se risposta corretta esegue il calcolo del tempo do root delay.
LogicLab (Ptp192, ST_SNTPRequest)
PROGRAM ST_SNTPClient
VAR
ClockSync : BOOL; (* Clock syncronized *)
TimeBf : UDINT; (* Time buffer (mS) *)
SNTPReq : SNTPClient; (* SNTP request *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_SNTPClient"
// *****************************************************************************
// At every defined time sends a NTP request and updates system clock.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Program initializations.
IF (SysFirstLoop) THEN
SNTPReq.SpyOn:=TRUE; //Spy On
SNTPReq.Port:=123; //NTP server port
SNTPReq.NTPServer:=ADR('ntp1.inrim.it'); //NTP server
SNTPReq.NTPServer:=ADR('0.it.pool.ntp.org'); //NTP server
SNTPReq.NTPServer:=ADR('0.pool.ntp.org'); //NTP server
TimeBf:=SysTimeGetMs(); //Time buffer (mS)
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 eTO_JUNK(SysDateSetS(TO_ULINT(SNTPReq.UTCTime)/1000000000)); ClockSync:=TRUE; END_IF;
END_IF;
// [End of file]
LogicLab (Ptp192, ST_SNTPClientNTPTimestamp)
PROGRAM ST_SNTPClientNTPTimestamp
VAR
TimeBf : UDINT; (* Time buffer (mS) *)
RootDelay : REAL; (* Root delay (uS) *)
SNTPReq : SNTPClient; (* SNTP request *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_SNTPClientNTPTimestamp"
// *****************************************************************************
// At every defined sends a NTP request and makes a NTP timestamp calculations.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Program initializations.
IF (SysFirstLoop) THEN
SNTPReq.SpyOn:=TRUE; //Spy On
SNTPReq.Port:=123; //NTP server port
SNTPReq.NTPServer:=ADR('ntp1.inrim.it'); //NTP server
SNTPReq.NTPServer:=ADR('0.it.pool.ntp.org'); //NTP server
SNTPReq.NTPServer:=ADR('0.pool.ntp.org'); //NTP server
TimeBf:=SysTimeGetMs(); //Time buffer (mS)
END_IF;
// -------------------------------------------------------------------------
// TIME SYNCRONIZATION
// -------------------------------------------------------------------------
// Waits time before to send request.
IF ((SysTimeGetMs()-TimeBf) > TO_UDINT(T#10s)) THEN
TimeBf:=SysTimeGetMs(); //Time buffer (mS)
SNTPReq.Query:=TRUE; //Query On
END_IF;
// On execution done, if Ok calculates the root delay.
SNTPReq(); //NTP request
IF NOT(SNTPReq.Done) THEN RETURN; END_IF;
SNTPReq.Query:=FALSE; //Query On
IF NOT(SNTPReq.Ok) THEN RETURN; END_IF;
// Calculate the root delay, value is in NTP short format.
// Upper 16 Bits (Integer Part): Represents the whole seconds.
// Lower 16 Bits (Fractional Part): Represents the fraction of a second
RootDelay:=TO_REAL(SNTPReq.NTPPacket.RootDelay/16#10000)*1000.0;
RootDelay:=RootDelay+(TO_REAL(SNTPReq.NTPPacket.RootDelay AND 16#FFFF)/65536);
// [End of file]