How to use DTStructureGetTime in ST?

For a project I need to find out if time is before or after 11 AM.
I tried to read the time with DTStructureGetTime and compare the hour element of the DTStructure with 11.

The variables:

VAR
    dpgIst11Uhr : BOOL; (*Einspeisen darf ab 11 Uhr beginnen*)
	dpgDTStructureGetTime_1 : DTStructureGetTime; (*Reads the local time from the target system in DTStructure format.*)
	dpgCurrentDateTime : DTStructure; (*Aktuelle Uhrzeit der SPS als DTStructure*)
    status : UDINT;
END_VAR

The code in an cyclic module:

dpgDTStructureGetTime_1.enable := TRUE; 
status := dpgDTStructureGetTime_1.pDTStructure := ADR(dpgCurrentDateTime); // aktuelles Data & Time ist in CurrentDateTime Data type DTStructure

IF dpgCurrentDateTime.hour >= 11 THEN
	dpgIst11Uhr := TRUE;
END_IF;

Unfortunately, the structure dpgCurrentDateTime is empty, all elements are 0.
Additional, status must be as UDINT declared not as UINT as described in AS Help.

PLC: X20CP0482
AS 4.11

Any ideas what I am doing wrong?
Walter

Hi,

I can’t see that the function block instance is called in the cyclic code.
I think you have to call “dpgDTStructureGetTime_1();” before using the outputs.

For example:

	dpgDTStructureGetTime_1.enable := TRUE;
	dpgDTStructureGetTime_1.pDTStructure := ADR(myDtStruct);
	dpgDTStructureGetTime_1();
	IF dpgDTStructureGetTime_1.status = 0 THEN
		IF myDtStruct.hour > 11 THEN
			// do something
		END_IF
	END_IF

Best regards!
1 Like

Alexander,

thank you, I missed this line in the code. Now the program works.

Walter

1 Like