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

add plug-device via the Python script

Rahied
2015-01-15
2019-02-08
1 2 > >> (Page 1 of 2)
  • Rahied - 2015-01-15

    Hello,

    I would like from a Python script.
    I want to add the plug-device of the EH-IOCA (Hitachi).
    there is someone who knows how to add plug-device via the Python script.

    Rahied

     
  • mkeller - 2015-01-19

    Hello Rahied.

    The following steps are required:
    - Get the slot object, e.g. projectObject.find(...)
    - Plug the device into slot with slotObject.plug(...)

    For details about the parameters see the documentation of the ScriptEngine, which you can find in the sub directory "OnlineHelp" of the CODESYS directory.

    BR
    Martin

     
  • Rahied - 2015-01-19

    Thanks for your adversely word .

    Only I 'm not from there at all . I send my script there. Maybe there is someone who got a example . Or can tell me what to do .

    thanks You

    Create CodeSys Project.py [1.31 KiB]

     
  • Rahied - 2015-01-19

    M.Keller hat geschrieben:
    Hello Rahied.
    The following steps are required:
    For details about the parameters see the documentation of the ScriptEngine, which you can find in the sub directory "OnlineHelp" of the CODESYS directory.
    BR
    Martin

    Create CodeSys Project.py [1.31 KiB]

     
  • Rahied - 2015-01-19

    Rahied hat geschrieben:
    Thanks for your adversely word .
    Only I 'm not from there at all . I send my script there. Maybe there is someone who got a example . Or can tell me what to do .
    thanks You

    Hello Rahied.

    The following steps are required:
    - Get the slot object, e.g. projectObject.find(...)
    - Plug the device into slot with slotObject.plug(...)

    For details about the parameters see the documentation of the ScriptEngine, which you can find in the sub directory "OnlineHelp" of the CODESYS directory.

    BR
    Martin

    Create CodeSys Project.py [1.31 KiB]

     
  • mkeller - 2015-01-19

    Hi Rahied.

    Please check the ID and the version of the device you want to plug! "51D_00000001_16 Digital Input_16 Digital Input" and "0" could be wrong.

    Also check the indentation of the "plug" line in your script because it was wrong when I opened it on my computer.

    I added a simple script as an example for you. It plugs all slots with the name "<empty>". The specified device required a module ID.</empty>

    BR
    Martin

    plug_device.py [329 Bytes]

     
  • Rahied - 2015-01-19

    M.Keller hat geschrieben:
    Hi Rahied.
    Please check the ID and the version of the device you want to plug! "51D_00000001_16 Digital Input_16 Digital Input" and "0" could be wrong.
    Also check the indentation of the "plug" line in your script because it was wrong when I opened it on my computer.
    I added a simple script as an example for you. It plugs all slots with the name "<empty>". The specified device required a module ID.
    BR
    Martin</empty>

    Tanks M.Keller,

    But the next error shows.

    IMG: Error.png

     
  • Rahied - 2015-01-19

    M.Keller hat geschrieben:
    Hi Rahied.
    Please check the ID and the version of the device you want to plug! "51D_00000001_16 Digital Input_16 Digital Input" and "0" could be wrong.
    Also check the indentation of the "plug" line in your script because it was wrong when I opened it on my computer.
    I added a simple script as an example for you. It plugs all slots with the name "<empty>". The specified device required a module ID.
    BR
    Martin</empty>

    I have a printscreen form the hitachi id code

    IMG: hitachi code.png

     
  • mkeller - 2015-01-19

    Hi Rahied.

    The ID and version are correct.

    Since I'm not sure what you want to do, I modified your last script for two different scenarios:
    - Plug all slots with the same device
    - Plug only the first one

    Please remove the code of the unused scenario.

    BR
    Martin

    Create CodeSys Project_modified.py [1.67 KiB]

     
  • Rahied - 2015-01-20

    M.Keller hat geschrieben:
    Hi Rahied.
    The ID and version are correct.
    Since I'm not sure what you want to do, I modified your last script for two different scenarios:
    Please remove the code of the unused scenario.
    BR
    Martin

    Thanks for your help.
    But it's not working.
    The next error is when a run the script.

    I hoop then you can help me

    IMG: next error.png

     
  • mkeller - 2015-01-20

    Hello

    Sorry, I forgot that find() returns a list. Please replace the code with the following:

    objects = master.find('EH_IOCA', True) # Recursive search
    assert(objects and len(objects) == 1, "no object or more than one object with the name EH_IOCA")
    subnodes = objects[0].get_children() # Get all children
    

    Is there more than one slave below the master? if yes than you have to find a way to find the correct slave.

    BR
    Martin

     
  • Rahied - 2015-01-20

    M.Keller hat geschrieben:
    Hello
    Sorry, I forgot that find() returns a list. Please replace the code with the following:

    objects = master.find('EH_IOCA', True) # Recursive search
    assert(objects and len(objects) == 1, "no object or more than one object with the name EH_IOCA")
    subnodes = objects[0].get_children() # Get all children
    

    Is there more than one slave below the master? if yes than you have to find a way to find the correct slave.
    BR
    Martin

    Hello a have put my script en Error code for u.
    It's stil not working

    IMG: Error.png

    Create CodeSys Project_modified (3).py [1.76 KiB]

     
  • Anonymous - 2015-01-20

    Originally created by: M.Schaber

    It seems you did mix tabs and spaces in the attached script. The error message actually tells you the location of the problem in the source file.

    Python generally interprets tabs according common standards with 8 characters, while your editor seems to use 4 characters of tab with.

    Generally, the python community strongly suggests to not mix tabs and spaces in source files:
    PEP 8 -- Style Guide for Python Code

    You should use an editor which can display tabs and spaces, so you can see the problem. Here is how it looks in my editor using 4 characters tab with:

    And here's how the Python interpreter "sees" it using 8 characters tab with:

    IMG: tabs_vs_spaces.png

    IMG: tabs_vs_spaces

     
  • Rahied - 2015-01-20

    M.Schaber hat geschrieben:
    It seems you did mix tabs and spaces in the attached script. The error message actually tells you the location of the problem in the source file.
    Python generally interprets tabs according common standards with 8 characters, while your editor seems to use 4 characters of tab with.
    Generally, the python community strongly suggests to not mix tabs and spaces in source files:
    PEP 8 -- Style Guide for Python Code
    You should use an editor which can display tabs and spaces, so you can see the problem. Here is how it looks in my editor using 4 characters tab with:
    And here's how the Python interpreter "sees" it using 8 characters tab with:

     
  • Rahied - 2015-01-20

    M.Schaber hat geschrieben:
    It seems you did mix tabs and spaces in the attached script. The error message actually tells you the location of the problem in the source file.
    Python generally interprets tabs according common standards with 8 characters, while your editor seems to use 4 characters of tab with.
    Generally, the python community strongly suggests to not mix tabs and spaces in source files:
    PEP 8 -- Style Guide for Python Code
    You should use an editor which can display tabs and spaces, so you can see the problem. Here is how it looks in my editor using 4 characters tab with:
    And here's how the Python interpreter "sees" it using 8 characters tab with:

    Hello here is my script.

    print "Current project."
    proj = projects.primary

    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("CODESYS Control RTE V3",4096,"0000 0002","3.5.5.40")
    print("--- PLC Device Insert. ---")

    # Create the IO

    for dev in proj.get_children():
    if dev.is_device:
    f = open("C:\Users\RO\Desktop\Import.txt", "r")
    for line in f:
    tok = line.split(' ')
    if "(Master)" in tok[1]:
    dev.add(tok[0], DeviceID(64, "0000 0001", "3.5.5.0"))
    subnodes = dev.get_children()
    master = subnodes[len(subnodes) - 1]
    elif "(Slave)" in tok[1]:
    master.add(tok[0], DeviceID(65, "51D_0201001000000001", "3.5.4.0"))
    elif "(IO)" in tok[1]:
    objects = master.find('EH_IOCA', True) # Recursive search
    assert(objects and len(objects) == 1, "no object or more than one object with the name EH_IOCA") # Stil have the Error
    subnodes = objects[0].get_children(True) # Get all children

        # Scenario 1: Plug all
        # i = 1
        # for node in subnodes:
               # name = "%s %d" % (tok[0], i) # Create name with index
               # node.plug(name, 68, "51D_00000001_16 Digital Input_16 Digital Input","0")
           # i += 1
    
        # Scenario 2: Plug only the first one
        if len(subnodes) > 0:
               subnodes[0].plug(tok[0], 68, "51D_00000001_16 Digital Input_16 Digital Input","0")
    

    # Save project
    proj.save()

    # Script finished
    print("--- Script finished. ---")

     

    Related

    Talk.ru: 1

  • Rahied - 2015-01-20

    M.Schaber hat geschrieben:
    It seems you did mix tabs and spaces in the attached script. The error message actually tells you the location of the problem in the source file.
    Python generally interprets tabs according common standards with 8 characters, while your editor seems to use 4 characters of tab with.
    Generally, the python community strongly suggests to not mix tabs and spaces in source files:
    PEP 8 -- Style Guide for Python Code
    You should use an editor which can display tabs and spaces, so you can see the problem. Here is how it looks in my editor using 4 characters tab with:
    And here's how the Python interpreter "sees" it using 8 characters tab with:

    attachment my scritpt

    Create CodeSys Project_modified (4).py [1.68 KiB]

     
  • Rahied - 2015-01-20

    M.Schaber hat geschrieben:
    It seems you did mix tabs and spaces in the attached script. The error message actually tells you the location of the problem in the source file.
    Python generally interprets tabs according common standards with 8 characters, while your editor seems to use 4 characters of tab with.
    Generally, the python community strongly suggests to not mix tabs and spaces in source files:
    PEP 8 -- Style Guide for Python Code
    You should use an editor which can display tabs and spaces, so you can see the problem. Here is how it looks in my editor using 4 characters tab with:
    And here's how the Python interpreter "sees" it using 8 characters tab with:

    Stil the same probleem

    Create CodeSys Project_modified (4).py [1.68 KiB]

    Create CodeSys Project_modified (4).py [1.68 KiB]

     
  • mkeller - 2015-01-20

    Hi.

    What error do you get when you execute your script? Is it the text of the assertion or something else?

    BR
    Martin

     
  • Rahied - 2015-01-20

    M.Keller hat geschrieben:
    Hi.
    What error do you get when you execute your script? Is it the text of the assertion or something else?
    BR
    Martin

    IMG: Error.png

     
  • mkeller - 2015-01-20

    Hi.

    The indentation of your script is still wrong. The indentation is important in python. Look at the screenshots.

    I modified your script to have the correct indentation and attached it. Please use a proper editor from now on.

    BR
    Martin

    IMG: correct indentation.png

    IMG: wrong_indentation.png

    Create CodeSys Project_7.py [1.85 KiB]

     
  • Rahied - 2015-01-20

    M.Keller hat geschrieben:
    Hi.
    The indentation of your script is still wrong. The indentation is important in python. Look at the screenshots.
    I modified your script to have the correct indentation and attached it. Please use a proper editor from now on.
    BR
    Martin

    Its stil not working
    Thanks voor you time

    IMG: Error.png

     
  • TimvH

    TimvH - 2015-01-20

    Hi Rahied,

    The Device is an EH_IOCA (with underscore).
    The name of the plugged devices cannot have a space. You should use e.g. "%s_%d" as device name to make them unique.
    Your import file (.txt) should have no empty lines at the end.

    Attached an example import file and script for plugging the modules in the Hitachi EH-IOCA EtherCAT slave.

    Maybe not perfect or complete, but for me this works.

    Import.txt [62 Bytes]

    Create CodeSys Project_8.py [4 KiB]

     
  • etamgul - 2015-09-04

    By plugging the device via a script a generated name is given to its structures and accessable variables (e.g. "%QX7.7", similarly when one has only plugs the device) but in GUI, I have the chance to map those addresses to variables.

    Is there a way to configure the plugged device configuration and I/O mapping ("xxx Configuration" and "xxx I/O Mapping" tabs)?

    I have some ADC/DIO/DAC "pins" configured and would like to map their variables and structures to one of the PRGs stored variables.

     
  • Anonymous - 2015-09-08

    Originally created by: M.Schaber

    Hi, etamgul,

    A scripting interface for the I/O Mapping is currently scheduled for the upcoming V3.5 SP8 release by the end of the year. (See CDS-37756).

    (Please note that the schedule may still change when other, more important issues arise.)

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.