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 switch the active application via script

aloeffler
2012-12-12
2012-12-12
  • aloeffler - 2012-12-12

    I have one given project, which keeps 3 applications for 3 different plc-types. ( I have no influence to this project structure, so I can not change it)

    For downloading them by script I am guessing I have to change the active application. How can I get to the application to set active?
    Or how can I change the active application at all?

     
  • Anonymous - 2012-12-12

    Originally created by: M.Schaber

    Hi,

    Projects have a property active_application, which you can use to query or set the active application.

    proj = projects.primary
    app = proj.find('Application', recursive = True)[0]
    proj.active_application = app
    
     
  • aloeffler - 2012-12-12

    Hi, thanks for this quick answer!
    Unfortunatelly I found 3 appplication objects. The given project structure:

    Project-PLC_A--SPS-Logik---Application-PLC_B--SPS-Logik---Application-PLC_C--SPS-Logik---Application

    Is there an easy way to detect the right application via parents property? Something like app.parent.parent ?

    print( "findApplicationResult.Count ", findApplicationResult.Count) 
    for far in findApplicationResult:
        appPlcFound = far.parent.parent.find( "PLC_A", False )
        print( "appPlcFound.Count ", appPlcFound.Count ) 
        if appPlcFound.Count > 0:
            print( "appPlcFound" ) 
            proj.active_application = appPlcFound[1]
            break
    

    Does not work, appPlcFound.Count = 0 three times. I am also not so much into Python so I am just guessing and trying...

     
  • Anonymous - 2012-12-12

    Originally created by: M.Schaber

    Hi, ALoeffler,

    There are several possibilities to solve your problem: The first one is to search from the device upwards:

    proj = projects.primary
    dev = proj.find('PLC_A')[0]
    app = dev.find('Application', recursive = True)[0]
    proj.active_application = app
    

    The second one is to search the application by path:

    proj = projects.primary
    dev = proj.find('PLC_A', 'SPS-Logik', 'Application')[0]
    proj.active_application = app
    

    A third way is to check the name of the parent:

    for far in findApplicationResult:
        if far.parent.parent.get_name() == 'PLC_A':
            # do something here...
    

    In your code, the problem was the following line:

    Zitat:

         appPlcFound = far.parent.parent.find( "PLC_A", False )
    

    Here, you're going down 2 steps from the application object (far.parent.parent points to the device in your project), and then you search for a child of that device with the name "PLC_A" non-recursively, which none exists.

    Note that the find() methods always return a list of objects, because each search could return more than one result.

    HTH,
    Markus

     
  • aloeffler - 2012-12-12

    Thank you Markus!
    Your first solution runs perfect.
    Now the next problem is knocking on the door To create the gateway and set the device active.
    But if I need help I'll open a new thread with a fitting topic.
    Alfred

     

Log in to post a comment.