PLC Structured Text Programming 語法整理

參考文獻: MELSEC-Q/L/F Structured Programming Manual (Fundamentals).pdf
  • The ST language is a text language with a similar grammatical structure to the C language.
  • Controls such as conditional judgement and repetition process written in syntax can be described.
  • This language is suitable for programming complicated processes that cannot be easily described by a graphic language (structured ladder/FBD language).

Operators & Data Types

  • Operators

  • Data Types


Syntax


  • Assignment
    • 格式:<Left side> := <Right side>;
    • 範例
      • intV1 := 0;
      • intV2 := 2;
    • Array type labels
      • The data type and the number of elements need to be the same for left side and right side.
      • intAry1 := intAry2;
    • Structure labels
      • The data type (structured data type) needs to be the same for left side and right side.
      • dutVar1 := dutVar2;

  • IF THEN Conditional Syntax
    • 格式:
      IF <Boolean expression> THEN
          <Syntax ...>;
      END_IF;
    • 範例
      IF bool1 THEN
          intV1 := intV1 + 1;
      END_IF

  • IF ...ELSE Conditional Syntax
    • 格式:
      IF <Boolean expression> THEN
          <Syntax 1 ...>;
      ELSE
          <Syntax 2 ...>;
      END_IF;
    • 範例
      IF bool1 THEN
          intV3 := intV3 + 1;
      ELSE
          intV4 := intV4 + 1;
      END_IF;

  • IF ...ELSEIF Conditional Syntax
    • 格式:
      IF <Boolean expression 1> THEN
          <Syntax 1 ...>;
      ELSIF <Boolean expression 2> THEN
          <Syntax 2 ...>;
      ELSIF <Boolean expression 3> THEN
          <Syntax 3 ...>;
      END_IF;
    • 範例
      IF bool1 THEN
          intV1 := intV1 + 1;
      ELSIF bool2 THEN
          intV2 := intV2 + 1;
      ELSIF bool3 THEN
          intV3 := intV3 + 1;
      END_IF;

  • CASE Conditional Syntax
    • 格式:
      CASE <Integer expression> OF
          <Integer selection 1> : <Syntax 1 ...>;
          <Integer selection 2> : <Syntax 2 ...>;
          ·
          ·
          ·
          <Integer selection n> : <Syntax n ...>;
      ELSE
          <Syntax n+1 ...>;
      END_CASE;
    • 範例
      CASE intV1 OF
          1: bool1 := TRUE;
          2: bool2 := TRUE;
      ELSE
          intv1 := intv1 + 1;
      END_CASE;

  • FOR ...DO Syntax
    • 格式:
      FOR <Repeat variable initialization>
          TO <Last value>
          BY <Incremental expression> DO
              <Syntax ...>;
      END_FOR;
    • 範例
      FOR intV1 := 0
          TO 30
          BY 1 DO
              intV3 := intV1 + 1;
      END_FOR;

  • WHILE...DO Syntax
    • 格式:
      WHILE <Boolean expression> DO
          <Syntax ...>;
      END_WHILE;
    • 範例
      WHILE intV1 = 30 DO
          intV1 := intV1 + 1;
      END_WHILE;

  • REPEAT...UNTIL Syntax
    • 格式:
      REPEAT
              <Syntax ...>;
          UNTIL <Boolean expression>
      END_REPEAT;
    • 範例
      REPEAT
              intV1 := intV1 + 1;
          UNTIL intV1 = 30
      END_REPEAT;

  • RETURN Syntax
    • 格式:
      RETRUN;
    • 範例
      IF bool1 THEN
          RETURN;
      END_IF;

  • EXIT Syntax
    • 格式:
      EXIT;
    • 範例
      FOR intV1 := 0
          TO 10
          BY 1 DO
              IF intV1 > 10 THEN
                  EXIT;
              END_IF;
      END_FOR;

Calling Functions

  • 格式
    function_name(Var1, Var2, ...);
  • 範例
    Output1 := ABS(Input1);
    Output1 := MAX(Input1, Input2, Input3);
    boolENO := MOV(boolEN, Input1, Output1);

Calling Function Blocks

  • 格式
    instance_name(Input var1 := Var1, ... Output var1 := Var2, ...);
  • 範例
    FBADD1(IN1 := Input1);
    Output1 := FBADD1.OUT1;
    FBADD2(IN1 := Input1, IN2 := Input2, IN3 := Input3);
    Output1 := FBADD2.OUT1;
    Output2 := FBADD2.OUT2;
simen

An enthusiastic engineer with a passion for learning. After completing my academic journey, I worked as an engineer in Hsinchu Science Park. Later, I ventured into academia to teach at a university. However, I have now returned to the industry as an engineer, again.

Post a Comment (0)
Previous Post Next Post