Logix Designer Structured Text help

ASF

Lifetime Supporting Member
Join Date
Jun 2012
Location
Australia
Posts
3,917
Hi all, this is a long one, so there's a TL;DR at the bottom :)

I have a situation where I'm dragging data from an external database into a UDT in a Control Logix PLC. The only problem is, the only way I can get the data to come in is by arraying the UDT elements, instead of arraying the UDT itself. By which I mean, the normal way I would do it is to create a UDT as follows:

ID, DINT
Name, STRING
Speed, DINT
Temperature, REAL
...
...

and then I would create an array of this UDT, so that I have:

Recipe[0].ID
Recipe[0].Name
Recipe[0].Speed
Recipe[0].Temperature
Recipe[1].ID
Recipe[1].Name
Recipe[1].Speed
Recipe[1].Temperature
Recipe[2].ID
Recipe[2].Name
...
...
Recipe[99].Speed
Recipe[99].Temperature

But I can't do that. I have to create the UDT like this:

ID, DINT[100]
Name, STRING[100]
Speed, DINT[100]
Temperature, REAL[100]
...
...

Which means I then create a single instance of this UDT, which is populated from my DB, and looks like this:

Recipe.ID[0]
Recipe.ID[1]
...
...
Recipe.ID[99]
Recipe.Name[0]
Recipe.Name[1]
...
...
Recipe.Name[99]

Obviously this is a pain for a lot of reasons, but I can live with it for now. However, what I'd like to do is create a UDT the "proper" way, and when the operator calls for a recipe, copy the data from the relevant database fields into that one UDT for loading into the line parameters. So if they call for recipe 12, I copy Recipe.ID[12] into RecipeUDT.ID, Recipe.Name[12] into RecipeUDT.Name, and so on.

I'm fine with the concept of indirect addressing, but what I'd like to do is use structured text to iterate through each element of the array instead of having to manually code a copy for each element of the array. I'm a complete novice at ST, so I'm not sure how to approach it, but I would imagine that it would look something like:

Code:
For each element in Recipe (
 COP element[pointer] RecipeUDT.element 1
)

Am I close to the mark? Is there anyone who's a more familiar with ST that can give me a nudge in the right dance floor?

Thanks!

TL;DR - from a UDT where each element is arrayed, can I copy the data from one array element into a UDT with an identical structure but with single (not arrayed) elements?
 
As far I know there's no method to iterate through the elements of a UDT. I think your best option is to use an index and copy each element belonging to a recipe:

Code:
RecipeUDT.ID := Recipe.ID[index];
RecipeUDT.Name := Recipe.Name[index]; 
RecipeUDT.Speed := Recipe.Speed[index];
RecipeUDT.Temperature := Recipe.Temperature[index];
You only will need to provide the correct index to load a recipe.
 
Last edited:
Damn, that's what I was afraid of. If I have to do it all manually I'll just do it in ladder for consistency - I had just hoped there might be a creative way to do it with ST.

Anyone else know any quirky tricks to make this work?
 
Damn, that's what I was afraid of. If I have to do it all manually I'll just do it in ladder for consistency - I had just hoped there might be a creative way to do it with ST.

Anyone else know any quirky tricks to make this work?

I don't see why this is a hassle, create an AOI based on ST, input your DB arrays into it, and output the Recipe[x] UDT tag.

Code:
// Just declaring the input/output tags in text for forum purpose
INPUTS
ID AS DINT[100]
Name AS STRING[100]
Speed AS REAL[100]
Temp AS REAL[100]

OUTPUT
Recipe AS RecipeUDT[100]

for loopIndex := 0 to 100 DO
    Recipe[loopIndex].ID := ID[loopIndex];
    Recipe[loopIndex].Name:= Name[loopIndex];
    Recipe[loopIndex].Speed := Speed[loopIndex];
    Recipe[loopIndex].Temp := Temp[loopIndex];
end_for;
Should be all you need:confused:
 
Yep - but that's still doing it all manually. You've got one line for each element of the recipe, and in my example there are significantly more than the four I've thrown up there as an example.

The loop you're using is to loop through each recipe and put ALL of them into a correctly-shaped UDT - I only need to get out one at a time, but I just want to try and do it without having to have 40 lines for the 40 different parameters.
 
Yep - but that's still doing it all manually. You've got one line for each element of the recipe, and in my example there are significantly more than the four I've thrown up there as an example.

The loop you're using is to loop through each recipe and put ALL of them into a correctly-shaped UDT - I only need to get out one at a time, but I just want to try and do it without having to have 40 lines for the 40 different parameters.

Ok I see the point, but still 40 lines in ST is easier than 40 lines of ladder. Plus you could simply pass the recipe number into the AOI and get rid of the loop for the specific data you need to transfer.

When you said manual I wasn't sure if that was for each child of the UDT or for each element + each child.
 
Paully's5.0 said:
Ok I see the point, but still 40 lines in ST is easier than 40 lines of ladder.
Agreed - if it comes to doing it manually, it'll be ST regardless. I'll probably just make it a subroutine, pass it the recipe ID I'm after, and have it populate my "normal" UDT.

Was just hoping that there might be a trick to iterate through all elements of a UDT automatically :)
 
One more question on the ST structure - how do I define an input parameter? As in, I will call this subroutine from a LAD routine with an input parameter defining which array element I want to copy into the individual UDT.

So, my code will look something like...
Code:
Recipe[Pointer].ID := SelectedRecipe.ID;
    Recipe[Pointer].Name:= SelectedRecipe.Name;
    Recipe[Pointer].Speed := SelectedRecipe.Speed;
    Recipe[Pointer].Temp := SelectedRecipe.Temp;
...but somewhere, I have to tell the routine that "Pointer" is the tag that was defined as the first input parameter.

Is it just as simple as Paully's example above?
Code:
INPUTS
Pointer as DINT
 
Nothing wrong with ladder! LOL ST causes a few problems when trouble shooting. Not my favourite bag.
 
I generally stick to ladder too - means I get less phone calls in the middle of the night :) but for something this simple I think I'll be safe, surely even Bubba can look at something as simple as the above and have an idea of what's going on ;)
 

Similar Topics

Hey! I'm looking for a way to reset a tag when the routine that sets the tag is disabled. For example: Routine 1 has the following If Tag1...
Replies
6
Views
1,803
Hello, Im building project with 1756-L82ES controller and 1756-IB16S card but i cant find it when trying to add the card to IO configuration...
Replies
3
Views
100
Does anyone know how to set the background colors of instuction blocks (TON, MOV, etc)?
Replies
1
Views
107
Hello all, I'm trying to use the Logix Designer SDK to upload and download a single ACD to many controllers. I got this to work fine copying...
Replies
0
Views
56
Hello, I am trying to connect a Stride Analog Output module to a 1769 CompactLogix via Ethernet. I added a new Generic Ethernet Module for the...
Replies
4
Views
198
Back
Top Bottom