SNTPRequest, sends a SNTP request

List

Questa pagina fa parte del Manuale Programmazione IEC 61131-3. Vai all indice.

Questo blocco funzione da eseguire in task Back, acquisisce da un server di tempo il valore di UTC, in questo articolo è spiegato come attivare il server su di un PC. Attivando Query viene eseguita la richiesta del valore di tempo dal server il cui URL o indirizzo IP è passato in NTPServer. L’FB interroga il server e se riceve risposta corretta ritorna in UTCTime il valore di tempo UTC in Epoch. In Offset viene ritornata la differenza in mS tra il valore UTC ritornato e l’orologio NTP di riferimento.

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.

Lista server NTP

Un server NTP è in grado di stimare e compensare gli errori sistematici dell’orologio hardware di sistema, esistono diversi server NTP a cui è possibile connettersi per aggiornare l’orologio di sistema. Il progetto pool.ntp.org è uno di questi, si presenta come un grande cluster virtuale di timeserver che forniscono un servizio NTP affidabile e semplice da usare per milioni di utenti. E’ possibile configuare uno qualsiasi di questi servers, i nomi 0, 1, 2 e 3.pool.ntp.org puntano ad un set di server casuale che cambia ogni ora.

  • 0.pool.ntp.org
  • 1.pool.ntp.org
  • 2.pool.ntp.org
  • 3.pool.ntp.org
Information Circle

Blocco funzione

CODESYS: Non disponibile

LogicLab: eLLabNetworkLib

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.
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 (REAL) Round trip delay, tempo per inviare la richiesta e ricevere la risposta dal server (mS).
UTCTime (UDINT) Data/Ora in UTC espressa in Epoch time.
Offset (REAL) Differenza tra il valore UTC ritornato e l’orologio NTP di riferimento (mS).

Immagine FB SNTPRequest

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
TriggerDescrizione
16#00000001Tx: Invio richiesta al server.
16#00000002Rx: Ricezione risposta dal server.
16#20000000Lg: Messaggio di log.
16#40000000Er: Errore esecuzione.

Esempi

Come utilizzare gli esempi.
ST_SNTPRequest: Invia ogni 30 secondi una richiesta al server NTP 0.pool.ntp.org e se risposta corretta viene eseguito l’aggiornamento del real time clock.

ST_SystemClockSync: All’avvio programma esegue aggiornamento clock di sistema. Se richiesta in errore la richiesta viene ripetuta velocemente. Ogni ora esegue un aggiornamento.

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]
Was this article helpful?