How do Insert, edit and delete POUs

butzen
2016-10-19
2016-10-20
  • butzen - 2016-10-19

    Hello team.

    I need a script to insert a POU below of Application object.

    I need too a script to edit a specific POU (MainPrg, for example), i.e., add new variables and modify code of POU.

    Finally is necessary deletar a specific POU.

    Thanks.
    Émerson Butzen
    Software Developer
    Unisinos/Altus

     
  • mkeller - 2016-10-20

    Hi butzen.

    butzen hat geschrieben:
    I need a script to insert a POU below of Application object.

    The Scripting API was extended with the necessary features with CODESYS V3.5 SP9. Look for IScriptIecLanguageObjectContainer and IScriptIecLanguageMemberContainer in the Scripting documentation.

    butzen hat geschrieben:
    I need too a script to edit a specific POU (MainPrg, for example), i.e., add new variables and modify code of POU.

    We can only edit Structure Text directly. For the graphical languages you have to import the POU from PLCopenXML or our native format. See IScriptTextualObjectMarker, IScriptObjectWithTextualDeclaration, IScriptObjectWithTextualImplementation and IScriptTextDocument for the textual language and the methods import_xml() and import_native() from IScriptObject2 and IScriptProject2 for the import.

    butzen hat geschrieben:
    Finally is necessary deletar a specific POU.

    Find the Scripting object, for example with find(), and use the method remove() to delete the object.

    Here some example:

    proj = projects.primary
    found = proj.find("Application", True)
    app = found[0]
    \# Create FB
    mypou = app.create_pou("MyPou")
    \# Change declaration of the FB
    implementation = mypou.textual_declaration.replace("""FUNCTION_BLOCK MyPou
    VAR_INPUT
       iValue : INT;
    END_VAR
    VAR_OUTPUT
    END_VAR
    VAR
    END_VAR""")
    \# Change implementation of the FB
    mypou.textual_implementation.replace("""iValue := iValue + 1;""")
    \# Add method to FB
    dosomething = mypou.create_method("DoSomething", "INT")
    \# Change declaration of the method
    dosomething.textual_declaration.replace("""METHOD DoSomething : INT
    VAR_INPUT
       iVal1 : INT;
       iVal2 : INT;
    END_VAR""")
    \# Change implementation of the method
    dosomething.textual_implementation.replace("""DoSomething := iVal1 + iVal2;""")
    \# Find the pou and delete it
    found = app.find("MyPou")
    if found and len(found) == 1:
       found[0].remove()
    else:
       print("POU 'MyPou' was not found"
    

    BR
    Martin

     

Log in to post a comment.