Reading out system time / clock in PLC

Hello,

I'd like to read out system time in PLC.

Later he time should be split in hour, minute, second. There are several different libraries but I didn't get it to work.

Any hint?

With WebVisu it's easy with %t[HH:mm.ss] so the system time has to be available with standard library but which to use?

Thanks in advance

Best reply by Open

Hello Alink,

I happened to have done it in my recent project.

I used SysTimeRtc (System) library.

Declaration:

Date_and_time_in_seconds : UDINT;
Local_date_time_seconds : UDINT;
Date_and_Time_result : UDINT;
Local_date_time_from_secs : SYSTIMEDATE;
Date_and_time_format : DATE_AND_TIME;
Date_and_time_string : STRING;

 Implementation: 

// Get time in seconds since 1970 :
Date_and_time_in_seconds := SysTimeRtcGet(Date_and_Time_result);
IF Date_and_Time_result <> 0 THEN
RETURN;
END_IF

// Convert UTC seconds to local time seconds, regarding Timezone and Summer time :
Date_and_Time_result := SysTimeRtcConvertUtcToLocal(Date_and_time_in_seconds, Local_date_time_seconds);
IF Date_and_Time_result <> 0 THEN
RETURN;
END_IF

// Convert the UTC seconds to a DATE_AND_TIME variable dt#yyyy-mm-dd-hh:mm:ss :
Date_and_time_format := UDINT_TO_DT(Local_date_time_seconds);

// Convert DATE_AND_TIME to a String :
Date_and_time_string := DT_TO_STRING(Date_and_time_format);

 

View original
4 replies