我想使用脚本引擎自动化 Codesys 项目。我尝试使用 site 来理解文档。 PrintDeviceTree.py 脚本正在运行,但我无法让它在 Codesys 中执行简单的任务,例如构建应用程序并以与 UI 中相同的方式在控制台中提供有效输出。最终,我想建立一个 Jenkins 管道来自动实现这一点,并可能对项目运行测试。如果有人有这方面的经验,我很乐意得到帮助或指出正确的方向。
我尝试运行这个示例程序,它输出一个设备列表,但现在我无法尝试构建一个应用程序。
# encoding:utf-8
# We enable the new python 3 print syntax
from __future__ import print_function
# Prints out all devices in the currently open project.
print("--- Printing the devices of the project: ---")
# Define the printing function. This function starts with the
# so called "docstring" which is the recommended way to document
# functions in python.
def print_tree(treeobj, depth=0):
""" Print a device and all its children
Arguments:
treeobj -- the object to print
depth -- The current depth within the tree (default 0).
The argument 'depth' is used by recursive call and
should not be supplied by the user.
"""
# if the current object is a device, we print the name and device identification.
if treeobj.is_device:
name = treeobj.get_name(False)
deviceid = treeobj.get_device_identification()
print("{0}- {1} {2}".format("--"*depth, name, deviceid))
# we recursively call the print_tree function for the child objects.
for child in treeobj.get_children(False):
print_tree(child, depth+1)
# We iterate over all top level objects and call the print_tree function for them.
for obj in projects.primary.get_children():
print_tree(obj)
print("--- Script finished. ---")
在您链接的文档的工具栏中,有一个关于 Scripting API 的完整部分。任何时候你想找东西,我都会先在那里搜索。
其中,可以在
build()
类中找到ScriptApplication
方法:
class ScriptApplication.ScriptApplication
...
build()
Builds the application.
...
在
ScriptProject
类中,您可以找到 active_application
属性:
class ScriptProject.ScriptProject
...
property active_application
Gets or sets the active application.
This is a property. You can read or assign a ScriptObject for the application you want to be the active application.
Return type:
ScriptObject
因此,您可以使用以下内容来构建项目的活动应用程序:
projects.primary.active_application.build()
如果您有超过1个应用程序,您可以获取所有应用程序的列表:
[app for app in projects.primary.active_application.parent.get_children() if app.is_application]
最后,由于您似乎想要自动化,我假设您想从外部源(例如,从 shell)执行这些脚本,在这种情况下,您可以在脚本顶部使用
projects.open(project_path)
,并通过 shell 将脚本路径传递给 codesys 来执行脚本C:\\Path\\to\\CODESYS\\exe C:\\Path\\to\\script
。然后,Codesys 将在没有 GUI 的情况下运行并执行脚本并将输出写入标准输出。 注意,项目不得已被某人/某事打开,否则脚本将失败。