Define multiple variables in a condition in ST

1.2k views Asked by At

I am currently programming/simulating a small plant in CODESYS. I have several outputs (that correspond to engines) that I need to test several times, so I want to create a condition that incorporates this test so I dont need to write the entire condition. For instance, i have the condition that verifies if

A=TRUE AND B=TRUE AND C=TRUE AND D=TRUE

Can I create a condition like "verify engine" to use each time ?

Thank you

1

There are 1 answers

0
Quirzo On

There are many ways to do this (if I understood you correctly).

Here are two ways for example:

1. Create a variable that has the condition result and use the variable. You have to assign the variable at beginning, and then you can use the variable instead of that long code.

VAR
    EnginesOK   : BOOL;
END_VAR

//Check engines
EnginesOK := (A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE); 

//.. Later ..

IF EnginesOK THEN
    //Do something
END_IF

2. Create a function, for example F_VerifyEngines that contains checks and returns the state as BOOL. Note: In this example A,B,C and D need to be global variables. You could also pass them as parameters for the function.

FUNCTION F_VerifyEngines : BOOL
VAR_INPUT
    //Add A,B,C,D here if needed
END_VAR
VAR
END_VAR

//Return the result
F_VerifyEngines := (A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE); 

Then you can use the function in code:

IF F_VerifyEngines() THEN
    //Do something
END_IF

The 2nd way is probably the one you were thinking.

By the way, there is no need to write A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE, in my opinion, it's more clear to read when you use A AND B AND C AND D instead.