CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Sat Feb 16, 2019 10:21 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Python and CoDeSys
PostPosted: Tue Jan 17, 2012 4:12 pm 
Offline

Joined: Tue Oct 04, 2011 9:21 am
Posts: 27
Hi,

I have two Python-Sctipts. The first one is to start CoDeSys and commanding it to execute a second Python-Script. The second script is to open one CoDeSys-Project and to exporting it to XML. The first script:
Code:
subprocess.call(r'"C:\Program Files (x86)\3S CoDeSys\CoDeSys\Common\CoDeSys.exe" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="D:\-----Python32\Export_New.py"')
My script should work on all computers without changing the directory, thats why i changed the first script. I have now two functions; the first one searchs for CoDeSys.exe and the second for my second script:
Code:
def runProcess(CoDeSys):
    for root, dirs, files in os.walk('C:\\'):
        if CoDeSys in files:
            return (os.path.abspath(os.path.join(root, CoDeSys)))

def PythonScript(ExportScript):
    for root, dirs, files in os.walk('D:\\'):
        if ExportScript in files:
            os.system((os.path.abspath(os.path.join(root, ExportScript))))
Problem: how can i tell CoDeSys to start the second script from inside of CoDeSys??
I hope my problem is clear :)


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Tue Jan 17, 2012 4:25 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, aoj,

I understand what you try to achieve, but I don't understand what your problem is, sorry. :?

You've got the call to start codesys, and you've got methods to find the path of the script and CoDeSys.exe itsself, so everything seems to be there... :?:

Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Tue Jan 17, 2012 4:30 pm 
Offline

Joined: Tue Oct 04, 2011 9:21 am
Posts: 27
Hi M.Schaber,

I am trying to call the two functions with the following code:
Code:
codesys = "CoDeSys.exe"
subprocess.call(runProcess(r"%s" % codesys))

exportscript = "Export.py"
os.system(PythonScript(exportscript))


The project doesnt get exported because the second script deosnt get run from inside of the CoDeSys environment. Is it now more clear?


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Tue Jan 17, 2012 4:42 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi,

In your first post, you had the following code:
Code:
subprocess.call(r'"C:\Program Files (x86)\3S CoDeSys\CoDeSys\Common\CoDeSys.exe" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="D:\-----Python32\Export_New.py"')

This code shows how the command line to start CoDeSys has to be built together. Then, you should build the CoDeSys command line using this pattern.

Why do you to call CoDeSys without arguments, and afterwards try to start the script outside of CoDeSys with os.system instead?

Try something like the following:
Code:
codesys = runProcess("CoDeSys.exe")
exportscript = PythonScript("Export.py")
subprocess.call('"%s" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="%s"' % (codesys, exportscript))

HTH,
Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 8:37 am 
Offline

Joined: Tue Oct 04, 2011 9:21 am
Posts: 27
Markus you are my hero :D
I didnt know how to build the CoDeSys command line using the subprocess-Modul. Now it is working. Thank you again and wish you a nice day.

AOJ


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 8:48 am 
Offline

Joined: Tue Oct 04, 2011 9:21 am
Posts: 27
I have one more question :) Is it possible to run CoDeSys without giving the directory of CoDeSys.exe in my method?


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 8:50 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, AOJ,

The building of the command line is not related to the subprocess-module, it is just a normal string operation. You could do the following:

Code:
codesys = runProcess("CoDeSys.exe")
exportscript = PythonScript("Export.py")
commandline = '"%s" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="%s"' % (codesys, exportscript)
subprocess.call(commandline)


The % operator for string formatting is documented in the python documentation: http://docs.python.org/library/stdtypes.html#string-formatting-operations

Another way would be to use string concatenation:
Code:
commandline = '"' + codesys + '" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="' + exportscript + '"'


Btw, your methods of searching the CoDeSys.exe is not fail-proof: If several CoDeSys versions are installed in different directories in "C:\Program Files", your script will match the first CoDeSys.exe it can find, and this is not necessarily the one matching the profile you give.

HTH,
Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 9:23 am 
Offline

Joined: Tue Oct 04, 2011 9:21 am
Posts: 27
Code:
your methods of searching the CoDeSys.exe is not fail-proof: If several CoDeSys versions are installed in different directories in "C:\Program Files", your script will match the first CoDeSys.exe it can find, and this is not necessarily the one matching the profile you give.


What should I change to make fail-proof? And for the case if I dont know in which directory CoDeSys.exe is saved, is it possible to run it without giving the directory in the method?


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 9:46 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, aoj,

aoj wrote:
What should I change to make fail-proof? And for the case if I dont know in which directory CoDeSys.exe is saved, is it possible to run it without giving the directory in the method?


AFAICS, there's no perfect approach for this, as there can be several CoDeSys version installed in parallel, and the script cannot guess which one you wanted to start.

I know the following approaches which all are being used in the field:

  • The most common approach I saw is to use a "configuration" module which is specific to each machine. In that module, you set variables like the path for the CoDeSys.exe, the profile, and the path for your own script directory. The configuration module is in the same directory as the script. So each user has to change only this single module for all scripts.
  • The configuration module as described above could also be installed in the site packages directory of your cPython installation.
  • Another way is to set environment variables with the CoDeSys path and profile, and query them in the script.
  • Another way might be the registry, search for the Keys pointing to the CoDeSys installation.

Note that the directory containing a script is usually contained in the search path. Additionally, in most cases, a python script or module can detect its own directory using a construct like the following:
Code:
scriptdir = os.path.abspath(os.path.dirname(__file__))

You can use this to find the directory of the Export_New.py script if it is installed in the same directory (or relative path) to the first script.

HTH,
Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 4:01 pm 
Offline

Joined: Tue Oct 04, 2011 9:21 am
Posts: 27
Hi Markus,
thanks a lot for your help.
One more question :oops:
The following code is a part of my ExportScript:
Code:
proj = projects.open(pfad)
device = proj.find('Device', recursive = False)[0]
device.export_native(destination=utils.temp(File), recursive = True)
print ("exporting finished")
As you can see the script exports the object Device. What about if I have in my project 50 objects to export? Is it possible to export the whole project?

Thanks :)


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 4:10 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, aoj,

The Project itsself also has an export_native method, which allows to pass a list of objects to export, as well as an recursive parameter to include child objects.

Simply exporting the whole project could be done with a command like:

Code:
proj.export_native(proj.get_children(), "d:\testination.export", true)

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
 Post subject: Re: Python and CoDeSys
PostPosted: Wed Jan 18, 2012 4:22 pm 
Offline

Joined: Tue Oct 04, 2011 9:21 am
Posts: 27
That is what i was searching for. Endless THANKS :D


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 12 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited