Search code examples
plcst

How can I get current time from PLC (Beckhoff)


I am trying to get the current time from the PLC (Beckhoff-cx5130). I have tried "GETSYSTEMTIME", but I am not getting any output. I also tried "NT_GetTime" "TIMESTRUCT" and "T_FILETIME" but showing some compiling error (Could be a library issue). Any suggestion (Example code) regarding this issue will be highly appreciated. Thanks!


Solution

  • I forgot to add the TcUtilities.Lib. After adding it, everything worked. For more info see Beckhoff's InfoSys

    Example code

    PROGRAM MAIN
    VAR
        fbSystemTime : GETSYSTEMTIME;
        timeAsFileTime : T_FILETIME;
        timeAsDT : DT;
    END_VAR
    
    fbSystemTime(
        timeLoDW=>timeAsFileTime.dwLowDateTime,
        timeHiDW=>timeAsFileTime.dwHighDateTime 
    );
    
    timeAsDT := FILETIME_TO_DT(timeAsFileTime);