Codesys ST instructions

damian177
2022-04-07
2022-04-10
  • damian177 - 2022-04-07

    Please explain to me what is the difference between the following instructions :?
    Can I use both interchangeably?

    IF [conditional_expresion] THEN
        <instructions>;
    ELSIF [conditional_expresion] THEN
        <instructions>;
    ELSE
        <instructions>;
    END IF;
    
    IF [conditional_expresion] THEN
        <instructions>;
    ELSE IF [conditional_expresion] THEN
        <instructions>;
    ELSE
        <instructions>;
    END IF
    END IF
    
     
  • sgronchi - 2022-04-07

    Yes. You'll suffer for multiple END_IFs in the second case.

     
  • tvm - 2022-04-07

    No real difference when there's only one elsif statement, but consider what your code would look like if there was more than one

    this:

    IF [conditional_expresion] THEN
        <instructions>;
    ELSIF [conditional_expresion] THEN
        <instructions>;
    ELSIF [conditional_expresion] THEN
        <instructions>;
    ELSIF [conditional_expresion] THEN
        <instructions>;    
    ELSE
        <instructions>;
    END IF;
    

    is easier to understand and troubleshoot than this:

    IF [conditional_expresion] THEN
           <instructions>;
    ELSE 
        IF [conditional_expresion] THEN
           <instructions>;
        ELSE
            IF [conditional_expresion] THEN
               <instructions>;
            ELSE
               IF [conditional_expresion] THEN
                   <instructions>;
                ELSE
                    <instructions>;
                END IF
            END IF
        END IF
    END IF
    
     
    • hermsen

      hermsen - 2022-04-08

      IF branching should be avoided by replacing it with a CASE statement if that is a possibility. But you probably know that 😜

       

      Last edit: hermsen 2022-04-08
  • ludecus

    ludecus - 2022-04-08

    Just as hint: For reference/pointer/interface checks you could also use AND_THEN

    IF itfInterface<>0 AND_THEN itfInterface.Method THEN <instructions>; END_IF
    IF pointer<>0 AND_THEN pointer.value THEN <instructions>; END_IF
    
     
    πŸ‘
    1
    • hermsen

      hermsen - 2022-04-08

      This is only useful when the method returns some boolean.
      To be honest I am personally not a huge fan of the AND_THEN extension as I dont need the AND_THEN that much. But this said, it available in the box of tricks

       
      πŸ‘
      1

      Last edit: hermsen 2022-04-08
      • ludecus

        ludecus - 2022-04-08

        To be honest, i love to pass a boolean as the return value for methods. Therefore i could simple call the method as a condition in IF statements.

        IF  iDevice <> 0 AND_THEN iDevice.mReadValues()
        THEN
            <instrcutions>;
        END_IF
        
         
  • damian177 - 2022-04-09

    the semicolon after END_IF has any meaning?

     
  • ludecus

    ludecus - 2022-04-10

    In CODESYS the IF is a statement. So the semicolon after the END_IF is needless. Only for assignment or calls is a semicolon required.

     

Log in to post a comment.