Way to determine number of elements in UDT in Logix5000

radfahrer

Lifetime Supporting Member
Join Date
Nov 2006
Location
Boston
Posts
156
Does anyone know if there is a way to reference the number of elements in UDT programmatically in a COP statement so I don't have to update the Length field manually if I modify the size of a UDT by adding/removing parameters?

Kind of a parallel question is, what happens if I set a length parameter longer than the number of elements in the UDT? Does that do some funky stuff in runtime based on memory locations (ie inadvertenly copy stuff that is allocated next in line beyond the UDT) or does Logix just ignore it if the Length specified is longer than the number of elements in the UDT.

Thanks for any info...
 
If you are COPying into a tag based on a UDT then a length of 1 will copy enough to fill 1 of that tag. You don't have to know the actual word length of the UDT.
 
Other instruction instead of COP that is easier

Bernie,

Thanks for the info. Just curious, is there another instruction that is simpler (doesn't have a length param) or better programming practice to use to copy one UDT variable to another? I tried to use MOV, but Logix compiler didn't like that.

-Greg
 
The MOV is for the basic numerical types. A MOV reads the source number as a value then stores that value into the destination in the manner appropriate to the type of the destination. For example a float source will be truncated (I think) before being stored in a DINT destination. This operation has no bearing on block transferring the contents of a UDT.
 
MOV is usually a slower instruction than COP, even though it only works on one single data element.

MOV takes a copy of the source (noting its data-type), and then looks at the destination data-type to determine if data conversion is needed.

If no conversion needed, then MOV just places a copy of the Source element into the Destination element.

If conversion is needed, then the instruction has to do the maths to convert the source data-type to the data-type of the destination.

COP, on the other hand doesn't look at any data-types, it just copies byte-by-byte from the Source to the Destination tags, for the length specified (which incidentally is the length of the Destination tag).

For a length of 1......
If the Destination is a SINT, COP copies 1 bytes from the Source.
If the Destination is an INT, COP copies 2 bytes from the Source.
If the Destination is a DINT, COP copies 4 bytes from the Source.
If the Destination is a UDT, COP copies the destination UDT size in bytes from the Source.

Of course, if the source and destination data-types are mismatched, then COP (and CPS) will corrupt the data, since neither instruction cares about data-types.

If you specify the length incorrectly, i.e. you put 100 instead of 10 for the length parameter, COP will terminate at the end of the current tag, to guard against memory corruption. But be careful, if you are COPying into an array element of a UDT for example, a COP will destroy following elements of the UDT if you have the length set too long (the safety-net is employed at the end of the tag, not the end of any sub-elements within that tag).

Now, back to the MOV instruction, or indeed any instruction that stores data into a destination, (ADD, SUB, MUL, DIV, CPT, etc. etc.). Bernie was nearly right, but when storing a REAL (floating point) result into an integer-type (SINT, INT, DINT) destination, the actual operation performed is Rounding.

Don't leave just yet - it gets interesting.

When we apply rounding to a REAL, we generally think it does
xx.0 to xx.4999999 = round down
xx.5 to xx.9999999 = round up

The problem is what to should be done with the exact xx.5 values. It is exactly halfway between xx and xx+1, so to always round these .5 values up creates a statistical imbalance that is not favoured, especially by the banking fraternity.

So, Logix5000 systems (not PLC5, not SLC, just the Logix5000 family) have adopted a new method of rounding, called the "Round-to-Even" method. Any exact xx.5 values are rounded to the nearest even number (zero is considered an even number, sitting as it does between two odd numbers, 1 and -1).

You can see this easily online by setting up a simple MOV instruction from a REAL tag, to a DINT tag. The picture shows what you will get from the {unusual} rounding method. The third column is just an excel rounding function applied to the source, just to highlight the differences. What this means is that any "conversion" of code from PLC5/SLC platforms has to be audited carefully. The converted code can produce different, and sometimes surprising, results !!

2012-06-15_004158.jpg
 
Not just great: AWESOME! I knew about rounding issues but I never noticed that the Logix 5k systems round-to-even. I have some code floating around in SLCs that works around the rounding to do some calculations. It will NOT work properly in this case. Definitely have to keep this in mind.
 
Thanks Daba. I knew about the 'rounding bias' problem but didn't know what was done in this case (of course truncation is an even more drastic conversion.) Thanks for your notes.

If you specify the length incorrectly, i.e. you put 100 instead of 10 for the length parameter, COP will terminate at the end of the current tag, to guard against memory corruption.

I'd like to read more about this. Do you have an AB manual reference?
 
Thanks Daba. I knew about the 'rounding bias' problem but didn't know what was done in this case (of course truncation is an even more drastic conversion.) Thanks for your notes.



I'd like to read more about this. Do you have an AB manual reference?

Sorry Bernie, missed the response....

I'll see if I can dig out a technote or similar....

I just know that Logix5000 will not write anything past the memory allocated to the tag as a whole - a "self-preservation" policy, but will crash into subsequent elements of the same tag if .LEN is specified incorrectly.

I'm just trying to figure out if there's a reason why they didn't apply boundary checking at the element level - perhaps too costly in terms of execution time - i don't know.
 

Similar Topics

Hello, i am a beginner with a Siemens Logo 8 PLC. I would determine the direction of an object if it passes a whole cycle of 2 input sensors. See...
Replies
2
Views
182
Hi all, Just looking through the CIP_AXIS_DRIVE data type in a Logix controller to look for something that can tell me whether the current...
Replies
2
Views
1,069
I'm currently working on an MES interface PLC which passes around a whole bunch of strings. A lot of these strings are really just to allow for an...
Replies
0
Views
1,118
I don't use AB much these days, and any installs I've done in years past have been setup by myself from scratch, so I've always known what...
Replies
8
Views
2,082
Hello: I wonder if there is a way to find the CPU load of a Logix processor, and if it is not possible to determine this in an absolute way, I...
Replies
6
Views
1,996
Back
Top Bottom