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

Scripting - examples and receipes

Anonymous
2011-05-05
2020-02-24
<< < 1 2 (Page 2 of 2)
  • Anonymous - 2011-10-13

    Originally created by: M.Schaber

    Hi,

    As the documentation states, the export_native function for projects gets a list of objects as mandatory first parameter, and the destination file as mandatory second parameter, and then the optional (keyword) parameters.

    So the error message tells you that passing the "recursive" parameter as second parameter to export_native is not allowed, as there need to be the two mandatory parameters first, before the keyword parameters are allowed.

    HTH,
    Markus

     
  • aoj - 2011-10-13

    so what could I write as list of objects (first parameter) if I want to export the complete project?

     
  • Anonymous - 2011-10-13

    Originally created by: M.Schaber

    You should be able to use the result of the get_children() method exposed by the project object.

     
  • aoj - 2011-10-13

    sorry for taking a lot of your time and effort but can you please explain what do you mean in other words

     
  • aoj - 2011-10-17

    Hi M. Schaber,

    many thanks for your help. I got it. It is working now
    Thank you

     
  • Anonymous - 2011-10-17

    Originally created by: M.Schaber

    Hi, Aoj,

    Sorry for the delay in answering, but I was busy with high priority issues, and had no time to check here in the forum.

    I'm clad that you could finish the issue yourself.

     
  • aoj - 2011-10-19

    Hi M. Schaber,

    is there any command which close CoDeSys after exporting the project?

     
  • Anonymous - 2011-10-19

    Originally created by: M.Schaber

    Hi,

    You can use system.exit(exitcode). The exitcode is optional, 0 (which means success) is the default.

    If you just want to terminate the script execution without exitting the platform, call sys.exit() or raise a SystemExitException.

    HTH,
    Markus

     
  • Anonymous - 2011-10-19

    Originally created by: M.Schaber

    One of our customers works with "prototype projects". They want to slice the prototype projects into single objects, and then use those pieces to re-assemble different projects using scripts.

    While assisting our customer, the following example script was written. It exports all POUs of the project - each of them into a single file containing just that POU including its children (Actions, Properties etc.), and the parent folders to preserve the folder structure.

    Other scripts can then use the native import to import those objects under an Application, and the folder structure is recreated.

    (There is a Bug with native import that can lead to duplicated folders, the fix is scheduled for 3.4 SP4 Patch 2.)

    \# Tests CoDeSys native import/export functionality.
    from __future__ import print_function
    proj = projects.primary
    \# We're interested in POU nodes:
    POUGuid = Guid("6f9dac99-8de1-4efc-8465-68ac443b7d08")
    \# We collect all POU nodes in that list.
    pous = []
    \# From the parent node on, we recursively add POU nodes:
    def CollectPous(node):
        if node.type == POUGuid:
            pous.append(node)
        else:
            for child in node.get_children():
                CollectPous(child)
    \# Now we collect all the leaf nodes.
    for node in proj.get_children():
        CollectPous(node)
    \# We print everything just to know what's going on.
    for i in pous:
        print("found: ", i.type, i.guid, i.get_name())
    \# And now we export the files.
    for candidate in pous:
        # We create a list of objects to export:
        # The object itsself
        objects = [candidate]
        
        # And sub-objects (POUs can have actions, properties, ...)
        objects.extend(candidate.get_children())
        
        # And the parent folders.
        parent = candidate.parent
        while ((not parent.is_root) and parent.is_folder):
            objects.append(parent)
            parent = parent.parent
        
        # Create an unique file name:
        filename = "d:\\test\\%s__%s.export" % (candidate.get_name(), candidate.guid)
        
        # print some user information
        print("exporting ", len(objects), " objects to: ", filename)
        
        # and actually export the project.
        proj.export_native(objects, filename)
    print ("script finished.")
    
     
  • aoj - 2011-10-19

    Hi Hi,

    one more request
    After exporting the project as XML-File, CoDeSys schows usually a message, that the exporting was successfull (Exportieren erfolgreich abgeschlossen) and waits for the user to press the "OK" button.

    Is it possible to tell CoDeSys to continue executing the script without waiting for the "OK" button?

     
  • Anonymous - 2011-10-19

    Originally created by: M.Schaber

    Hi,

    First, we should stop discussing questions in this thread - it was only meant to present examples. We should create a new thread in this forum.

    Currently, it is not possible to suppress this dialog. We have system.prompt_answers which allows catching and auto-answering of dialogs - however, this currently works only for dialogs which have a so-defined "MessageKey", and it seems that the export dialogs don't offer such MessageKeys.

    I created issue CDS-25228 in our tracker so this will be fixed eventually.

     
  • etamgul - 2015-07-14

    Hi

    I'm currently trying to make this script execution work (using the "ScriptEnginePluginAPIReference" and "CoDeSys" help)
    with the examples given in help files, and seems to work but ran into some unexpected trouble.

    I wanted to store my project settings and code in PLCopenXML so I made a project, exported everything,
    using the example script I imported the XML to an empty project (so far it worked) but when I tried to upload
    and login I got an error: gateway is not configured.

    So I wrote a simple script:

        def _setGateway(self):
            objects = self.proj.get_children(recursive=True)        
            for dev in objects:
                if dev.get_name() == 'Device':
                    proj_device = dev
            
            gateway_guid = proj_device.get_gateway()
            #gateway_guid = '3261a15d-434d-4868-9e1f-1bfa38e92408'
            gateway_dev_addr = 'some-hardcoded-value'
            proj_device.set_gateway_and_address(gateway_guid, gateway_dev_addr)
    

    The script had been checked and its working under a preset project file.
    Problems:
    1st) when I execute it on a newly (script) created empty project file (after XML import)
    the gateway GUID is NULL (00000-000-0000-0000), if I set the gateway (manually) in that project
    and save the project and than re-execute this script I get a valid GUID.

    2nd) Even if I hardcode the correct GUID and the correct address (I can see the script setting itt correctly,
    because I set the addr to a wrong value before) the login command throws me an error that the device
    could not be reached because of connection problems. Again if I set it manually and save the project,
    login works perfectly.

    Another major problem that I came across with is the following (either my python knowledge is limited here
    or just a simple explanation can solve this), using a python script through command line will only grant
    that and only that python script the visibility to codesys supplemented interfaces, once another script is
    used from that script (using sys.path.append) that script will no longer have the ability to resolve these interface names.

    • I have a few python script under (let say) C:\python_scripts, these are mostly helpers and classes (e.g. ImporterReporter)
    • I have another python script under C:\codesys_python\execute.py that should do some testing (or loggin in, etc)

    execute.py:

     sys.path.append('C:\\python_scripts')
     from python_importer import *
    

    This line gives me an error/exception that undefined name is
    -> execute.py line 2 "Unknown name"
    -> python_scripts\codesys_importer.py line 16 "ImporterReporter"

    Is there any way to grant access/visibility to script outside the runtime frame?
    If I copy these scripts near my "execute.py" it works, but I have a reason for separating them
    so that is not an option.

    Thank you very much for your help!

     
  • Mystic - 2015-10-06

    Zitat:
    "projects" is an object imported into the namespace for scripts running in CoDeSys, so it is only available if your script is being started in CoDeSys directly (either via the "Execute Script" menu command or button, or via the "--runscript" command line parameter.)

    Is there any documents or manuals including these 'namespace' in CoDeSys?

    I can't find it on my PC and internet.

    Thanks a lot.

     
  • etamgul - 2015-12-17

    Mystic hat geschrieben:
    Is there any documents or manuals including these 'namespace' in CoDeSys?
    I can't find it on my PC and internet.
    Thanks a lot.

    Yes it is documented in the online manual and also 3S CODESYS\CODESYS\Online Help\ScriptEngine.chm

     
  • etamgul - 2015-12-17

    Is there a way to read the device log using the scripting interface?
    E.g. when an exception happens and I retreive that the application has been stopped but would like to know the reason.

     
  • mkeller - 2015-12-17

    Hi.

    etamgul hat geschrieben:
    Is there a way to read the device log using the scripting interface?
    E.g. when an exception happens and I retreive that the application has been stopped but would like to know the reason.

    Right now, this is not yet possible, but this improvement is already listed in our Jira tracker CDS-31928, so it will be implemented eventually.

    BR
    Martin

     
  • etamgul - 2016-01-22

    In my case onlineapp.application_state does not work properly when script is executed with --noUI switch.

    I wanted to test that in case of an exception (thrown because the runtime got a wrong UDP packet) the application is stopped,
    thus I needed a python script to send the proper UDP packets when I realized that using the --noUI switch influences the result of the
    execution and this was because the onlineapp.application always returned me 'run'.

    Could somebody confirm this?

     
  • jvm68 - 2020-02-24

    Hi

    is it possible just to use just one script instead od two , if always execute codesys from the PC.?
    other question, is it possible to introduce the variables from the scrip, with informtion from other program?

    Thanks for your help.

     
<< < 1 2 (Page 2 of 2)

Log in to post a comment.