如何控制执行不同ST代码块的顺序?

时间:2015-12-01 20:16:04

标签: plc st

我有一个包含8个步骤的工艺配方,每个步骤都在ST中定义。但是,用户应该能够选择执行这8个步骤的顺序。我试图提出可用于此的标志或变量,但到目前为止还是留空了。有没有人对如何实施这一点有任何想法?

1 个答案:

答案 0 :(得分:2)

我的建议是让用户按顺序填写一个带有步骤编号的数组。然后将不同的ST代码块放在CASE语句中。对于CASE变量,使用数组。这样,执行顺序就完全灵活了。

这是一个三明治装饰器的例子。重要的部分从E_SandwichDecoratorStep.RecipeExecution

开始

<强>枚举:

TYPE E_SandwichDecoratorStep :
(
    UserSelecting := 1,
    RecipeExecution,
    Finished
);
END_TYPE
TYPE E_SandwichDecoratorUserRecipe :
(
    Pepperoni := 1,
    Ham,
    Cheese,
    Tomato,
    Salad,
    Sauce,
    Salt,
    Pepper
);
END_TYPE

<强>程序:

PROGRAM SANDWICHDECORATOR
VAR
    arrnStepOrder       : ARRAY[1..8] OF E_SandwichDecoratorStep;   (*User Recipe Configuration*)

    bRestart            : BOOL;
    bUserRecipeStart    : BOOL; (*Start the execution of the Recipe*)
    eCurStep            : E_SandwichDecoratorStep;
    nCurUserRecipeIndex : INT := 1;
END_VAR
CASE eCurStep OF
    E_SandwichDecoratorStep.UserSelecting:
        (*Example Order*)
        arrnStepOrder[1] := E_SandwichDecoratorUserRecipe.Ham;
        arrnStepOrder[2] := E_SandwichDecoratorUserRecipe.Tomato;
        arrnStepOrder[3] := E_SandwichDecoratorUserRecipe.Pepperoni;
        arrnStepOrder[4] := E_SandwichDecoratorUserRecipe.Cheese;
        arrnStepOrder[5] := E_SandwichDecoratorUserRecipe.Salt;
        arrnStepOrder[6] := E_SandwichDecoratorUserRecipe.Sauce;
        arrnStepOrder[7] := E_SandwichDecoratorUserRecipe.Pepper;
        arrnStepOrder[8] := E_SandwichDecoratorUserRecipe.Salad;

        IF bUserRecipeStart THEN
            bUserRecipeStart := FALSE;
            eCurStep := E_SandwichDecoratorStep.RecipeExecution;
        END_IF

    E_SandwichDecoratorStep.RecipeExecution:
        IF nCurUserRecipeIndex <= E_SandwichDecoratorUserRecipe.Pepper THEN
            CASE arrnStepOrder[nCurUserRecipeIndex] OF
                E_SandwichDecoratorUserRecipe.Pepperoni:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;

                E_SandwichDecoratorUserRecipe.Ham:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;

                E_SandwichDecoratorUserRecipe.Cheese:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;

                E_SandwichDecoratorUserRecipe.Tomato:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;

                E_SandwichDecoratorUserRecipe.Salad:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;

                E_SandwichDecoratorUserRecipe.Sauce:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;

                E_SandwichDecoratorUserRecipe.Salt:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;

                E_SandwichDecoratorUserRecipe.Pepper:
                    nCurUserRecipeIndex := nCurUserRecipeIndex + 1;
            END_CASE
        ELSE
            nCurUserRecipeIndex := 1;
            eCurStep := E_SandwichDecoratorStep.Finished;
        END_IF

    E_SandwichDecoratorStep.Finished:
        IF bRestart THEN
            bRestart := FALSE;
            eCurStep := E_SandwichDecoratorStep.UserSelecting;
        END_IF

END_CASE