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

Python and CoDeSys

aoj
2012-01-17
2012-01-18
  • aoj - 2012-01-17

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

    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:

    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

     
  • Anonymous - 2012-01-17

    Originally created by: M.Schaber

    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

     
  • aoj - 2012-01-17

    Hi M.Schaber,

    I am trying to call the two functions with the following 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?

     
  • Anonymous - 2012-01-17

    Originally created by: M.Schaber

    Hi,

    In your first post, you had the following 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:

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

    HTH,
    Markus

     
  • aoj - 2012-01-18

    Markus you are my hero
    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

     
  • aoj - 2012-01-18

    I have one more question Is it possible to run CoDeSys without giving the directory of CoDeSys.exe in my method?

     
  • Anonymous - 2012-01-18

    Originally created by: M.Schaber

    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:

    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:

    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

     
  • aoj - 2012-01-18
    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 is saved, is it possible to run it without giving the directory in the method?

     
  • Anonymous - 2012-01-18

    Originally created by: M.Schaber

    Hi, aoj,

    aoj hat geschrieben:
    What should I change to make fail-proof? And for the case if I dont know in which directory 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:

    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:

    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

     
  • aoj - 2012-01-18

    Hi Markus,
    thanks a lot for your help.
    One more question
    The following code is a part of my ExportScript:```

    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

     
  • Anonymous - 2012-01-18

    Originally created by: M.Schaber

    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:

    proj.export_native(proj.get_children(), "d:\testination.export", true)
    
     
  • aoj - 2012-01-18

    That is what i was searching for. Endless THANKS

     

Log in to post a comment.