英文:
Codesys interface properties in ST
问题
我正在尝试创建一个具有属性的界面,而不必通过大量点击来添加它们作为带有getter和setter的属性。
我想要这样做:
INTERFACE Door
VAR
sensorBack : BOOL;
END_VAR
是否有可能像这样做,还是总是需要通过程序结构来添加它们?
英文:
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 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?
答案1
得分: 2
在codesys中,结构化文本总感觉是事后的考虑。
简短的回答是你需要使用GUI逐个添加它们。
长答案是,你可以使用内置的(ScriptEngine)Python API简化具有许多属性的接口的生成:
英文:
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
:
Press on the three dots (...
):
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
}
]
答案2
得分: 1
你需要按照我所知道的程序结构通过代码添加它们。
英文:
You need to add them via the program structure as far as I know.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论