Getting The System Time Using Ladder Logic (GSV)

Clock

To get the Time/Date from the PLC it is necessary to retrieve the data with a Get System Value (GSV) instruction.

Simple enough right? Good, let’s dive in. The first thing you need to do is drag a rung and GSV instruction into the ladder editor. I plan on updating my time structure  every processor scan so I will put the GSV instruction on an unconditional rung, which is a rung without any preceding instructions or logic. Your logic should look like the picture below.

GSV Rung

The next thing you will need to do is select the class. For this tutorial let’s will use the WallClockTime class . Double click the question mark next to the Class Name and select the “WallClockTime” class.

WallClockTime

Next, select the correct attribute, in this case the LocalDateTime attribute.  Double click the question mark next to the Attribute Name and select the “LocalDateTime” attribute from the drop down list.

LocalDateTime

Lastly the Destination tag array needs to be created and referenced. The destination tag should be a Dint array of 7 to hold all the data. The following is a list of all the data that will be retrieved:

                Year                                                       Minute
                Month                                                  Second
                Day                                                        Micro Second
                Hour

 For ease of use rather than creating an array of Dints 7 long we will create a User Defined Type with all of the above elements in them. I named the user defined type  DATE_TIME with a tag name of ControllerTime. I then place the element ControllerTime.Year in the Destination area of the GSV instruction.

GSV_Ladder_Complete

Let me explain further, the help files state that the Destination tag must be an array. Rather than loading the Date/Time into an array of Dints I am loading the Date/Time into a structure. Why? Take a look at the table below. The structure is far more readable than the array wouldn’t you agree? The software just needs 7 elements to place the data in. I also make the tag controller scope so I can use the tag anywhere in my project.

Array                                                                                     UDT______ 
ControllerTime[0]                                            ControllerTime.Year
ControllerTime[1]                                            CotnrollerTime.Month
ControllerTime[2]                                            ControllerTime.Day
ControllerTime[3]                                            Controller.Hour
ControllerTime[4]                                            Controller.Minute
ControllerTime[5]                                            Controller.Second
ControllerTime[6]                                            Controller.MicroSecond

 

Below is a screen shot of the DATE_TIME data type I created. Essentially I just created an array of 7 dints, but with actual names like ControllerTime.Hour and ControllerTime.Second, rather than just ControllerTime[3] or ControllerTime[5]. The structured path makes it much easier to get the system hour by addressing the hour rather than remembering which element of the array contains the hour. One could argue that you could just place a description on the array element, however why not just do it this way?

 
UDT

 

Now that you have the system Date/Time it can be used for time stamping or whatever your application demands. Below you can see a screenshot of the values that get loaded into the tag ContollerTime.

GSV_Monitor

 I am going to use the ContollerTime.Month element  to trigger a bit that will display a message on an HMI (Human Machine Interface or Touchscreen monitor) indicating that the machine is in need of scheduled maintenance. I will latch the bit on until the scheduled maintenance is performed. In this case when the maintenance is performed the technician will press an acknowledge button on the HMI which will unlatch the bit in the ladder logic.

If you would like to copy the neutral code and paste it into one of your applications feel free. If you don’t know how to paste it into your application check out this walk through. Be aware that if you copy and paste the code you still have to create the tags and write the descriptions, great practice right?

The ControllerTime tag is a user defined type with the name of DATE_TIME as described above. The Last_Month tag is type DINT. The PM_Request and HMI_Acknowledge bits are type BOOL.

Code:

GSV(WallClockTime,,LocalDateTime,ControllerTime.Year);
NEQ(ControllerTime.Month,Last_Month)[OTL(PM_Request),MOV(ControllerTime.Month,Last_Month)];
XIC(HMI_PM_Acknowledge)OTU(PM_Request);

 

GSV_Complete