Structured Text Expression question....

timryder

Member
Join Date
Feb 2007
Location
Macomb MI
Posts
176
Hey all,

Experimenting with Structured Text more.

Does anyone know a way where you can set an int or any numerical variable type to multiple values in a single line based on an expression?

EX:
Code:
Var_a = 0 := X>Y;
Var_a = 1 := Y>X;
Var_a = 2 := X=Y;

Is there a structure which would let me do all that in a single line?

Thanks!
 
timryder,

I believe what you are wanting to do is an IF THEN (IF ELSE) statement where you compare X to Y and set Var_a depending on if the IF statement is True or not.

-Nathan


Edit: If would look something like this (note: this logic has not been tested):
If X > Y Then
Var_a := 0;
ElsIf Y < X Then
Var_a := 1;
ElsIf X = Y Then
Var_a := 2;
End_If;
 
Last edited:
Yes,

I am aware of the IF THEN ElsIf.

What I was wondering is if you can do the same expression evaluation with other operators in a single line. Some other OOP languages allow you to do exactly that with a single line and some operators.
 
timryder, what you describe in posts #1 and #3 seem to be two different things.

To set a variable to different values depending on differeing formulas (as described with the pseudocode in post #1) definitely would have to be made with several lines of code. How is that done in other languages ?

To set multiple variables to the same value in a single code line (as described in post #3) is possible in Siemens TIA.

Example:
"MyDB".Intvar_1 := "MyDB".Intvar_2 := "MyDB".Intvar_3 := 10 ;

Many consider it to be bad style though.
 
I'm guessing you are looking for something equivalent to an IIF as in other languages. I don't know of any equivalent in ST for that. One option is to create a Function and then you could use it like this:

Result:=FB1(0,1);


That's on a Codesys based system. If you're on an AB system, it seems to lack the capabilities a CodeSys system has, so I'm not sure if it is possible on an AB system.
 
Jesper

Thanks for the reply!

C# for example you can set the value of a variable with multiple conditions by doing something like this...


var NewVariable = ValueA if(X>Y) | ValueB if (Y>X);

the | being an alternate condition.

I can just program it in multiple lines, I just wanted to make sure I wasn't missing out on some clever syntax I could utilize.
 
Beckhoff/Codesys has the SEL operator which seems like it would handle your initial example but only for two conditions (true/false).

sel.JPG
 
Codesys is what we're using,

so that single line statement, i would like to expound upon that to give an alternate value with an alternate condition.

So in plain English it would read...

Alarm is equal to ONE if Temp is greater than 100 or equal to TWO if Temp is less than 100.

something like that but all in one line.
 
C# for example you can set the value of a variable with multiple conditions by doing something like this...

var NewVariable = ValueA if(X>Y) | ValueB if (Y>X);

the | being an alternate condition.
That raises several questions to me.
What happens if both IF conditions resolves to true ? That is not possible in your example but I can easily imagine how that can happen.

What happens if neither IF conditions resolves to true ?
Is there no equivalent to ELSE in that syntax ?


IMO it is not desirable to write as dense as possible. One should comment the heck out of every line, and having too much going on in a single line makes that more difficult.
 
The SEL Is the winner!

Thank you guys... you can nest the select statements and get the result to be what I'm looking for in 1 line of code.

very nice!

Thanks
 

Similar Topics

I'm writing some structured text that's handling a data structure that comes from a PC. The PC structure is in the "new" LREAL 64-bit floating...
Replies
3
Views
488
I have an expression in a structured text routine of a Logix controller that looks more or less like the following: ResultInteger := Integer1 *...
Replies
13
Views
389
Good evening. I display the step number of a SFC on a display. Sometimes, on a trip, it goes quickly through many steps and I need to prove to...
Replies
1
Views
127
I am trying to set up a piece of equipment with a Horner HE-X4R. I'd like to use structured text and so far I'm just trying to get a basic On/off...
Replies
0
Views
70
Good morning. I'm doing a rehab and I need to recycle some part of the old code that won't change and that I need. This is a calculation that...
Replies
22
Views
1,359
Back
Top Bottom