Define multiple variables in a condition in ST

时间:2018-09-18 20:22:10

标签: conditional-statements st codesys

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 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点(如果我理解正确的话)。

例如,有两种方法:

1。。创建具有条件结果的变量并使用该变量。您必须在开始时分配变量,然后可以使用该变量代替长代码。

humanize()

2。。创建一个函数,例如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 ,其中包含检查并将状态返回为F_VerifyEngines。注意:在此示例中,A,B,C和D需要为全局变量。您也可以将它们作为函数的参数传递。

BOOL

然后您可以在代码中使用该功能:

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); 

第二种方法可能是您正在考虑的方法。

顺便说一句,我认为不需要写IF F_VerifyEngines() THEN //Do something END_IF ,而当您使用A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE时,读起来更清楚。