FB_LocalSystemTime

FB_LocalSystemTime 1:

In some applications the local Windows system time is synchronized via the SNTP time server or a radio clock. In many cases the local Windows system time has to be used in the PLC (e.g. in the form of timestamp log messages to the HMI). The local Windows system time is displayed in the taskbar. For such applications the FB_LocalSystemTime function block can be useful.

This function block internally combines the functions of the following function blocks: RTC_EX2, NT_GetTime, FB_GetTimeZoneInformation and NT_SetTimeToRTCTime. The RTC_EX2 function block can be used for generating time stamps for log outputs, for example. However, this block has the disadvantage that its time is not synchronized with the local Windows system time and has to be resynchronized cyclically via the NT_GetTime function block (see RTC block examples in the documentation). Cyclic synchronization of the internal time (systemTime output) is already implemented in the function block. The cycle time can be configured via the dwCycle input. The function block also provides time zone information (summer time/winter time).

The FB_LocalSystemTime function block must be called cyclically (e.g. every second or during each PLC cycle). This is necessary to enable the time between synchronizations to be calculated.

FB_LocalSystemTime 2:

Jitter!

The local Windows system time is read with the aid of acyclic services (ADS function blocks). Due to the system characteristics the runtime of the ADS commands cannot be specified/estimated. Differences in command runtimes may lead to time jitter at the systemTime output, depending on the operating system, the synchronization interval and the PLC cycle time. For this reason, the time provided by the function block is only conditionally suitable for more precise measuring tasks. However, the accuracy is adequate, for example, for building automation applications.

Switching between daylight-saving time and standard time

The function block cannot be called exactly at the time when the switchover from daylight-saving time to standard time and vice versa takes place. In order to avoid complex calculations, the following implementation was chosen (explained in the example).

In our example the function block synchronises its own time with the local Windows system time (grey) every 60 seconds.
The PLC application needs and reads the time in the function block (e.g. every 30 seconds, blue). In our example the switchover between daylight-saving time and standard time is detected with a delay of 15 seconds. This behaviour should be unproblematic for most applications.

Switching from standard time to daylight-saving time

Switching from daylight-saving time to standard time

VAR_INPUT

VAR_INPUT
    sNetID    : T_AmsNetID := '';
    bEnable   : BOOL;
    dwCycle   : DWORD(1..86400) := 5;
    dwOpt     : DWORD := 1;
    tTimeout  : TIME := DEFAULT_ADS_TIMEOUT;
END_VAR

sNetID: A string containing the network address of the TwinCAT computer whose time is to be used for the synchronization can be given here (type: T_AmsNetID). If it is to be run on the local computer, an empty string can be entered.

bEnable: A rising edge at this input triggers immediate synchronization of the internal time with the local Windows system time. The output bValid remains set to FALSE until the synchronization is complete. The first rising edge activates the cyclic synchronization. The subsequent cyclic synchronizations are then executed automatically. In most cases the application only has to set this input to TRUE once.

dwCycle: Cycle time (in seconds) during which the function block resynchronizes its own time. Cyclic synchronization is activated after the first rising edge at the bEnable input. Default: Synchronization every 5 seconds.

dwOpt: Additional option parameters. The following parameters are currently available:

tTimeout: States the length of the timeout that may not be exceeded by execution of the internal ADS command.

VAR_OUTPUT

VAR_OUTPUT
    bValid     : BOOL;
    systemTime : TIMESTRUCT;
    tzID       : E_TimeZoneID := eTimeZoneID_Invalid;
END_VAR

bValid: The time at the systemTime output is invalid if this output is FALSE. The time is valid if TRUE (i.e. it was synchronized with the local Windows time at least once).

systemTime: Local Windows system time (type: TIMESTRUCT).

tzID: Time zone information (summer time, winter time) (type: E_TimeZoneID).

Example:

In the example the FB_LocalSystemTime function block is activated on program startup (rising edge at bEnable input). Once the time has been synchronized (bValid = TRUE), the PLC writes a message to the TwinCAT System Logview every 500 ms. The internal synchronization is carried out every second.

PROGRAM MAIN
VAR
    fbTime   : FB_LocalSystemTime := ( bEnable := TRUE, dwCycle := 1 );
    logTimer : TON := ( IN := TRUE, PT := T#500ms );
END_VAR
fbTime();

logTimer( IN := fbTime.bValid );
IF logTimer.Q THEN
    logTimer( IN := FALSE ); logTimer( IN := fbTime.bValid );
    ADSLOGSTR( ADSLOG_MSGTYPE_HINT OR ADSLOG_MSGTYPE_LOG, 'Local System Time:%s', SYSTEMTIME_TO_STRING(fbTime.systemTime));
END_IF

The written messages can be viewed in the TwinCAT Logview.

FB_LocalSystemTime 3:

Further functions and function blocks for time and time zone:

Requirements

Development environment

Target platform

PLC libraries to be integrated (category group)

TwinCAT v3.1.0

PC or CX (WES7/Win7/Win10: TC RT x86/x64, WEC6/7: TC RT x86, WEC7: TC CE7 ARMV7, TC/BSD: TC RT x64, TC OS ARMT2)

Tc2_Utilities (System)