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

How to manage multiple application in python script

Nicolas233
2017-03-14
2017-03-29
  • Nicolas233 - 2017-03-14

    Hello,

    I have a project with two applications which are not at the same level in project tree.

    How can I manage connexion in each of these two applications independently in Python script ?
    Can I connect and force variables on both applications or do I need to use something like the command menu "define active application" ?
    Can I set the active application using python script ? Which is the method to use ?
    Thank you

     
  • mkeller - 2017-03-14

    Hi Nicolas233.

    Nicolas233 hat geschrieben:
    How can I manage connexion in each of these two applications independently in Python script ?
    Can I connect and force variables on both applications or do I need to use something like the command menu "define active application" ?
    Can I set the active application using python script ? Which is the method to use ?

    The operations for connecting to an application and reading/forcing variables from it are part of the online application object which you get by using . You can call the method with the GUID of the application object or the application object itself to get the online application object for the specified application. See the attached python script.

    If you want the online application object for the active application or if your project only contains one application you can call the method without a parameter.

    BR
    Martin

    online_read_from_two_apps.py [776 Bytes]

     
  • Nicolas233 - 2017-03-15

    Hi Martin,

    Thank you for this example.It's helpful.
    However, my script encounters an error when i use such scheme.
    The error message is : >Zitat:
    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    It seems to happen on the following lines : ```

    app = found[0]

    ```

    Do you know where i can found inputs and outputs définitions for methods such as proj.find or projects.primary ? I don't find any description and, or examples in Script Engine API reference and in Python documentation. Where should I search ?

    I think the line : ```

    found = proj.find("Device", "PLC Logic", "Application")

    ``` is used to get the application GUID, isn't it ?

    How can i get GUID of an application from the application name in the Codesys project tree ?

    Thank you

     
  • mkeller - 2017-03-15

    Hi Nicolas233.

    Nicolas233 hat geschrieben:
    However, my script encounters an error when i use such scheme.
    The error message is :
    It seems to happen on the following lines : app = found[0]

    If creates that error than you didn't find any objects with the specified name or the specified path (array of names). I used this variant of the method so I can specify the complete path of the object.

    Nicolas233 hat geschrieben:
    Do you know where i can found inputs and outputs définitions for methods such as proj.find or projects.primary ? I don't find any description and, or examples in Script Engine API reference and in Python documentation. Where should I search ?

    The documentation of the Scripting API is in the file "ScriptEngine.chm" which is in the sub directory "Online Help" of the CODESYS directory. With CODESYS V3.5 SP11 we will add an introduction to the ScriptEngine/Scripting to our Online Help.

    Nicolas233 hat geschrieben:
    I think the line : found = proj.find("Device", "PLC Logic", "Application") is used to get the application GUID, isn't it ?

    No, it is used to specify the complete path of an object. If you used and you have more than object with the name "Application" you get an array with more than one entry.

    Nicolas233 hat geschrieben:
    How can i get GUID of an application from the application name in the Codesys project tree ?

    All script object of the project tree have a property which return the GUID of the object. But you don't need the GUID of the appliction to use because you can also use the script object of the application to get the corresponding online application object.

    BR
    Martin

     
  • Nicolas233 - 2017-03-29

    Thank you for these explanations. It helps me to correct my script.

     

Log in to post a comment.