Search code examples
interfacepropertiescodesys

Codesys interface properties in ST


I'm trying to make a interface with properties in st instead of having to add them as property with getter and setting by doing a lot of clicking.

How I am doing it now property

How I want to do it:

INTERFACE Door
VAR
  sensorBack : BOOL;
END_VAR

Is there a possibility to do something like this or do you always need to add them through the program structure?


Solution

  • Structured Text in codesys always felt like an after thought.

    The short answer is that you need to use the GUI to add them one by one.

    The long answer is, you can use the bultin (ScriptEngine) Python APIs to simplify the generation of interfaces with many properties:

    from __future__ import print_function
    import re
    import sys
    
    app = projects.primary.active_application
    
    # Fill in the following:
    interface_name = '' # Example 'IMyInterface'
    base_interface_type = None # Example 'IMyBaseInterface', leave as None if there's no base interface
    interface_properties = [
        {
            'name': '', # Example 'MyProperty'
            'return_type': '', # Example 'BOOL'
            'getter': True, # Set to False if you want no getter
            'setter': True # Set to False if you want no setter
        }# , # Include as many properties as you want in a comma (,) separated list
        # {
        #     'name': ''
        #     'return_type': ''
        #     'getter': True,
        #     'setter': True
        # }
    ]
    
    # Don't touch the code below
    if base_interface_type is None:
        interface = app.create_interface(name=interface_name)
    else:
        interface = app.create_interface(name=interface_name, baseInterfaces=base_interface_type)
    
    for property_data in interface_properties:
        property_name = property_data['name']
        return_type = property_data['return_type']
        include_getter = property_data['getter']
        include_setter = property_data['setter']
        
        property = interface.create_property(name=property_name, return_type=return_type)
        getter = property.find('Get')[0]
        setter = property.find('Set')[0]
        
        if not include_getter:
            getter.remove()
        
        if not include_setter:
            setter.remove()
    
    

    Save the above script as a *.py file, change the parameters according to your needs and run the script:

    View -> Scripting -> scripting Immediate:

    Scripting Immediate option in the View menu

    Press on the three dots (...):

    Scripting Immediate console window

    Navigate to the script (*.py) file and open it. A new Interface with the desired properties should be generated.

    Heres an example of me running the script with the following parameters:

    interface_name = 'IMyInterface'
    base_interface_type = None
    interface_properties = [
        {
            'name': 'MyProperty1',
            'return_type': 'BOOL',
            'getter': True,
            'setter': False
        },
        {
            'name': 'MyProperty2',
            'return_type': 'INT',
            'getter': False,
            'setter': True
        },
        {
            'name': 'MyProperty3',
            'return_type': 'DINT',
            'getter': True,
            'setter': True
        },
        {
            'name': 'MyProperty4',
            'return_type': 'WORD',
            'getter': False,
            'setter': False
        },
        {
            'name': 'MyProperty5',
            'return_type': 'BOOL',
            'getter': True,
            'setter': False
        }
    ]
    

    Results of running the script