Insert new POU

jbo
2011-06-16
2016-03-01
  • jbo - 2011-06-16

    Hi,
    is it possible to insert a new POU by Python script?
    Best regards,
    jbo

     
  • TimvH

    TimvH - 2011-06-16

    If you have already created it and exported it to .xml (manually), you can import it with a script.
    See my post about import XML.

     
  • Anonymous - 2011-06-16

    Originally created by: M.Schaber

    Hi, Jbo,

    jbo hat geschrieben:
    is it possible to insert a new POU by Python script?

    As TimvH wrote, it is possible to insert a POU via PLCOpenXML import (and in 3.4 SP4 also in the normal standard export format).

    PLCOpenXML can be imported from a file and from a string, that means that you can even "embed" the POU XML as a multiline string in your script.

    Markus

     
  • jbo - 2011-06-16

    Hi TimvH, Hi M.Schaber,

    many thanks for your answers.
    Background of my question:
    I created a new project by the following Python-Script:

    PROJECT = r"C:\CoDeSys-Projects\Script-Test.project"
    \# clean up any open project:
    if projects.primary:
    Β  Β projects.primary.close()
    print("--- CleanUp finished. ---")
    \# create a new project
    proj = projects.create(PROJECT)
    print("--- Project created. ---")
    \# add PLC
    proj.add("LMC_PacDrive",4096,"0000 0001","3.4.3.0")
    \# Save project
    proj.save()
    \# Script finished
    print("--- Script finished. ---") 
    

    The project created by the script contains the "LibraryManager", but not the standard POU 'PLC_PRG' and not the 'Taskkonfiguration'.
    Where have I made a mistake?

    CoDeSys V3.4 SP3 Hotfix 1

     
  • jbo - 2011-06-16

    I have just tried out with CoDeSys V3.4 SP3 Patch 2 ==> It works
    Was is a bug in CoDeSys V3.4 SP3 Hotfix 1?
    I only changed the device version (3.4.3.0 ==>3.4.3.20):```

    # add PLC
    proj.add("LMC_PacDrive",4096,"0000 0001","3.4.3.20

    ```

     
  • TimvH

    TimvH - 2011-06-16

    I am not 100% sure, but I believe it is related to the device description file of the PLC.
    If you haven't un-installed the previous version, you could choose the new device description in this older version and run the script again.

     
  • jbo - 2011-06-16

    jbo hat geschrieben:
    I have just tried out with CoDeSys V3.4 SP3 Patch 2 ==> It works
    Was is a bug in CoDeSys V3.4 SP3 Hotfix 1?
    I only changed the device version (3.4.3.0 ==>3.4.3.20):\# add PLC proj.add("LMC_PacDrive",4096,"0000 0001","3.4.3.20

    Sorry, that was a mistake.
    It does not work

    IMG: Geraete.png

     
  • TimvH

    TimvH - 2011-06-16

    Actually that is the normal behaviour, because you start a new empty project and then add the Device.
    With the (manual) CoDeSys File New "Wizard" you can choose a standard project which let you select the Device and POU language. Only then it will add the Task and PLC_PRG automatically.
    With the script I think the only solution is to add this yourself.

     
  • Anonymous - 2015-10-21

    Originally created by: Maximilian.Blase

    jbo hat geschrieben:
    Hi,
    is it possible to insert a new POU by Python script?
    Best regards,
    jbo

    Hi @ all,
    over four years have passed since the last entry. Therefore i wannted to ask again, if it is now possible to create a new POU or DTU with Python without xml im- and export.

    Best regards,
    M. Blase

     
  • mkeller - 2015-10-21

    Hi M. Blase.

    Maximilian.Blase hat geschrieben:
    Hi @ all,
    over four years have passed since the last entry. Therefore i wannted to ask again, if it is now possible to create a new POU or DTU with Python without xml im- and export.

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

    Since CODESYS V3.5 SP6 you can read and write the textual declaration and/or implementation part of POU objects which have one. Search for "IScriptTextDocument" and "IScriptTextualObjectMarker" in the ScriptEngine documentation.

    BR
    Martin

     
  • Anonymous - 2015-10-21

    Originally created by: Maximilian.Blase

    Hi Martin,

    let me first say thank you for your very quick response. Less than 30 minutes. I am impressed. Also it is a little bit funny because we are both from germany. Who cares.

    M.Keller hat geschrieben:
    ...
    Since CODESYS V3.5 SP6 you can read and write the textual declaration and/or implementation part of POU objects which have one. Search for "IScriptTextDocument" and "IScriptTextualObjectMarker" in the ScriptEngine documentation.
    ...

    I've already read about this possibility and so this was my intention at the second step. But at first i have to create new POUs and DTUs, or? Otherwise the objects have to be generated in the past manualy. This is the point i want to avoid.

    Best regards,
    Maximilian

     
  • Anonymous - 2015-10-22

    Originally created by: M.Schaber

    Hi,

    Yes, they have to be created - but for creating them, you could always use the same PLCOpenXML template of an empty POU, which you could even embed as a string into your Python script. Then use rename and text editing functions to populate it.

    HTH,
    Markus

     
  • PLKM - 2016-02-26

    M.Schaber hat geschrieben:
    Hi,
    Yes, they have to be created - but for creating them, you could always use the same PLCOpenXML template of an empty POU, which you could even embed as a string into your Python script. Then use rename and text editing functions to populate it.
    HTH,
    Markus

    Is it possible that only POUs from type Function_Block can be edited with the IScriptTextDocument Methods.

    Because a POU of type Program returns at request for has_textual_declaration and has_textual_implementation both with false (3.5 SP7), while returning true for type of Function_Block.

     
  • Anonymous - 2016-02-29

    Originally created by: M.Schaber

    Hi,

    PLKM hat geschrieben:
    Is it possible that only POUs from type Function_Block can be edited with the IScriptTextDocument Methods.
    Because a POU of type Program returns at request for has_textual_declaration and has_textual_implementation both with false (3.5 SP7), while returning true for type of Function_Block.

    In my test, it works with SP7.

    One common mistake is that your script might work on the Task Call object below the Task configuration which is calling your PROGRAM - it has the same name as the PROGRAM it calls, and thus the script might accidentally find it, instead of the PROGRAM itself.

    If that is not your problem, please send a stripped-down example project and script to e support@codesys.com e , so we can reproduce and fix the problem.

     
  • PLKM - 2016-03-01

    M.Schaber hat geschrieben:
    Hi,
    In my test, it works with SP7.
    One common mistake is that your script might work on the Task Call object below the Task configuration which is calling your PROGRAM - it has the same name as the PROGRAM it calls, and thus the script might accidentally find it, instead of the PROGRAM itself.
    If that is not your problem, please send a stripped-down example project and script to e support@codesys.com e , so we can reproduce and fix the problem.

    My fault. As you said i had chosen the PLC_PRG task instead of the PROGRAM itself.

     

Log in to post a comment.