Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Simple Code with Script

MKeo
2018-02-05
2018-02-06
  • MKeo - 2018-02-05

    Hello!

    Is it possible to write a simple Codefragment, something like "count := count + 1; etc.. " including creating the variable count over a Python Script?

    I already have written a script which creates a POU (ST), but i can not find any topics about writing an application over a script.

    Regards!

     
  • XopHeT - 2018-02-06

    You have to use textual_implementation and textual_declaration properties of IScriptTextDocument.

    There are few methods like replace, insert, append and so on (see ScriptEngine.chm for more information).

    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"
    
     

Log in to post a comment.