CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

How to switch the active application via script
https://forum.codesys.com/viewtopic.php?f=18&t=5127
Page 1 of 1

Author:  aloeffler [ Wed Dec 12, 2012 1:29 pm ]
Post subject:  How to switch the active application via script

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?

Author:  M.Schaber [ Wed Dec 12, 2012 2:08 pm ]
Post subject:  Re: How to switch the active application via script

Hi,

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

Code:
proj = projects.primary

app = proj.find('Application', recursive = True)[0]

proj.active_application = app

Author:  aloeffler [ Wed Dec 12, 2012 2:54 pm ]
Post subject:  Re: How to switch the active application via script

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 ?
Code:
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...

Author:  M.Schaber [ Wed Dec 12, 2012 3:10 pm ]
Post subject:  Re: How to switch the active application via script

Hi, ALoeffler,

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

Code:
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:

Code:
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:

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


In your code, the problem was the following line:
Quote:
Code:
     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

Author:  aloeffler [ Wed Dec 12, 2012 3:50 pm ]
Post subject:  Re: How to switch the active application via script

Thank you Markus!
Your first solution runs perfect.
Now the next problem is knocking on the door :shock: 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

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/