Logix Compare DateTime UDT?

FCS_Chris

Member
Join Date
Nov 2017
Location
Perth
Posts
5
Hi all,

Is there an efficient way to compare the UDT elements below in ladder logic, I need to know when they're not equal?

Compare PLC_DateTime_Set.Previous to PLC_DateTime_Set.Current

Code:
PLC_DateTime_Set				DateTime_Set
  PLC_DateTime_Set.Previous			DateTime
    PLC_DateTime_Set.Previous.Year		DINT
    PLC_DateTime_Set.Previous.Month		DINT
    PLC_DateTime_Set.Previous.Day		DINT
    PLC_DateTime_Set.Previous.Hour		DINT
    PLC_DateTime_Set.Previous.Minute		DINT
    PLC_DateTime_Set.Previous.Second		DINT
    PLC_DateTime_Set.Previous.uSec		DINT
  PLC_DateTime_Set.Current			DateTime
    PLC_DateTime_Set.Current.Year		DINT
    PLC_DateTime_Set.Current.Month		DINT
    PLC_DateTime_Set.Current.Day		DINT
    PLC_DateTime_Set.Current.Hour		DINT
    PLC_DateTime_Set.Current.Minute		DINT
    PLC_DateTime_Set.Current.Second		DINT
    PLC_DateTime_Set.Current.uSec		DINT

Cheers,
Chris.
 
You can use an index on both arrays, then increment index and compare element by element until last element is achieved then repeat, this way is easier to handle.
Or you compare them individually using brute force.
 
Thanks widelto,

I tried accessing the elements via an index but couldn't get it to work. Can anyone elaborate on the correct approach?

For now I've got a bunch of parallel NEQs which do the job rather untidily an inefficiently. Surely there's a better way?

Cheers,
Chris.
 
Efficient to execute... but not to code. Especially if dealing with a bigger UDT.

Surely there's a better way?
 
PLC_DateTime_Set.Previous DateTime = Array0
PLC_DateTime_Set.Previous.Year DINT =Array0[0]
PLC_DateTime_Set.Previous.Month DINT =Array0[1]
PLC_DateTime_Set.Previous.Day DINT
PLC_DateTime_Set.Previous.Hour DINT
PLC_DateTime_Set.Previous.Minute DINT
PLC_DateTime_Set.Previous.Second DINT
PLC_DateTime_Set.Previous.uSec DINT =Array0[7]
PLC_DateTime_Set.Current DateTime = Array1
PLC_DateTime_Set.Current.Year DINT =Array1[0]
PLC_DateTime_Set.Current.Month DINT =Array1[1]
PLC_DateTime_Set.Current.Day DINT
PLC_DateTime_Set.Current.Hour DINT
PLC_DateTime_Set.Current.Minute DINT
PLC_DateTime_Set.Current.Second DINT
PLC_DateTime_Set.Current.uSec DINT =Array1[7]

Index in this case is from 0 up to 7
compare array0[index] to array1[index]
the increment index and compares gain repeat until index = 7 then go back.
 
Thanks jstolaruk, structured text would be a nice way to do it.

Unfortunately we're running Studio5000 mini/cheap, only $1377 +GST
 
Change them to Arrays and use the FSC instruction. Then FSC Expression would be:

PLC_DateTime_Set.Previous[FSC_Control.POS] <> PLC_DateTime_Set.Current[FSC_Control.POS]

PLC_DateTime_Set.Previous[7] DINT

PLC_DateTime_Set.Previous.[0] = Year
PLC_DateTime_Set.Previous.[1] = Month
PLC_DateTime_Set.Previous.[2] = Day
PLC_DateTime_Set.Previous.[3] = Hour
PLC_DateTime_Set.Previous.[4] = Minute
PLC_DateTime_Set.Previous.[5] = Second
PLC_DateTime_Set.Previous.[6] = uSec

PLC_DateTime_Set.Current[7] DINT

PLC_DateTime_Set.Current.[0] = Year
PLC_DateTime_Set.Current.[1] = Month
PLC_DateTime_Set.Current.[2] = Day
PLC_DateTime_Set.Current.[3] = Hour
PLC_DateTime_Set.Current.[4] = Minute
PLC_DateTime_Set.Current.[5] = Second
PLC_DateTime_Set.Current.[6] = uSec
 
This is not answering the OP's question about comparing the seven elements of the DateTime/LocalDateTime objects. Instead, consider using the CurrentValue of the WALLCLOCKTIME, which can be read into a two-element DINT array for a slightly faster comparison. This may not be appropriate if the DateTime elements are used for other purposes such as on HMI displays.

I am not seeing a way to get DateTime and CurrentValue simultaneously, which implies you may not be able to store both, and guarantee them to be consistent. There could be an AOI to convert CurrentValue to a DateTime object, in which case you could GSV and store the 64-bit CurrentValue, and then convert to DateTime elements as needed.
 
Hi and Welcome to the Forum!

FCS_Chris said:
...Is there an efficient way to compare the UDT elements below in ladder logic, I need to know when they're not equal?...

If you just need to know when the UDTs, as a whole, are not equal, then perhaps this simple method might suffice?...

45384 - Compare a whole UDT's data with another UDT
Access Level: Everyone

Regards,
George
 
Thanks everyone for your input!

To summarise, UDTs can be compared with:
- Parallel NEQ or other instructions.
- Instead of UDT use an array, elements can then be compared via their index using FSC or other instruction
- Structured text

There was a suggestion that elements of UDTs can be access via an index like an array. This would be nice but I haven't been able to get it to work. Is this really possible?

Cheers,
Chris.
 
I would go "brute-force" on this, just for the sake of clarity...

A string of 7 EQU instructions in series on a rung would evaluate as true only if all elements were the same.

Obviously XIO "Same" would mean "Different"
 
There was a suggestion that elements of UDTs can be access via an index like an array. This would be nice but I haven't been able to get it to work. Is this really possible?

The literal answer to this question is, to my knowledge, no -- unless the UDT elements are defined as arrays to begin with.

What you can do, as pointed out by Geospark in post #11, is to effectively memory-copy UDT tags into a one-dimensional arrays. Then you can FSC or loop through those arrays for comparison with each other. This technique operates on copies of the UDT tags, and will not identify which UDT element(s) are different.

If it were possible to alias an array tag to a UDT tag, comparing the actual UDT contents would be possible. However, when defining an alias, Logix forces the data type of the alias to that of the base tag. I have not found a way around this behavior.
 

Similar Topics

Hi everyone! Accidentally closed it and it wont appear again. Checked and unchecked the option and seem to make no difference. Running v7.10.00...
Replies
16
Views
1,814
Howdy Everyone, We are working on a project in rslogix 5000 where we are using an rfid reader to read the barcodes then if they are on a list of...
Replies
10
Views
2,519
Hi While running the compare tool for two Projects, I am getting the attached difference in Program and Run. See attached. The difference seems...
Replies
3
Views
1,542
The font size and row height after doing a compare function are so small I can barely read it. I am using version 6.30.01 on a Win 10 Pro...
Replies
2
Views
1,349
is there a way to open a compact logix plc program and then compare that to the program that is actually running in the plc Thanks
Replies
1
Views
1,055
Back
Top Bottom