了解 EcoStruxure Machine Expert Python API(带 dir()inspectapi

概述

EcoStruxure Machine Expert 提供了能够用在 EcoStruxure Machine Expert 脚本中的 Python API。当编写 Python 脚本时,大多数时候,您只需要用到函数的名称或者所需参数及其名称的列表。您通常还需要使用由所调用的 API 函数返回的对象的可用函数。以下函数让您能够深入利用 EcoStruxure Machine Expert Python API 来帮助您开发 Python 脚本。

Python 的内置 dir(…) 功能

在不使用参数的情况下,此函数在当前本地范围内返回名称列表。在使用参数的情况下,此函数尝试为该对象返回有效属性的列表。

dir() 的缺省 Python 机制在使用不同类型的对象时有不同的行为表现,因为它会试图生成最相关的而不是全面的信息:

  • 如果对象是模块对象,则列表包含模块的属性名称。

  • 如果对象是类型或类对象,则列表包含属性名称,并且递归地包含其基础的属性名称。

  • 否则,列表包含对象的属性名称以及其类的属性名称,并且递归地包含基础类的属性名称。

示例(LogicBuilderShell.exe 中):

>>> import struct
>>> dir()   # show the names in the module namespace
['__builtins__', '__doc__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module
['Struct', '__builtins__', '__doc__', '__file__', '__name__',
 '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape(object):
        def __dir__(self):
            return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'perimeter', 'location']

inspectapi 函数

由于 dir() 函数不列出所有 EcoStruxure Machine Expert Python API 元素,因此会提供额外的函数以用于为 Python 脚本开发人员进行显示。

inspectapi 是一个 Python 模块,它在启动 LogicBuilderShell.exeScripting Immediate 视图时自动加载。因此,当前范围中有变量/模块 inspectapi 可用。此变量还通过执行 dir() 函数来列出。

inspectapi 变量/模块提供以下函数:

函数名称

参数

描述

inspectapi.dir

obj:指定要为其获取更多信息的对象(比如,变量)。

(可选)verbose:缺省值为 FALSE。将参数设置为 TRUE,获取详细的输出。

此函数提供可被调用的可用 EcoStruxure Machine Expert Python API 成员的列表。列出字段、属性、事件和方法。

inspectapi.dir_events

此函数提供可被调用的可用 EcoStruxure Machine Expert Python API 事件的列表。

inspectapi.dir_fields

inspectapi.dir_methods

inspectapi.dir_properties

inspectapi.help

()

此功能可用于打开 EcoStruxure Machine Expert 在线帮助的软件 > 编程和配置 > CODESYS 脚本 API部分中的 CODESYS 脚本引擎

有关更多信息,另请参阅 Schneider Electric 脚本引擎用户指南

obj:指定将显示在线帮助的对象(例如,变量)。

此函数可打开对象的在线帮助。

  • 如果对象没有文档,则会打开脚本引擎的在线帮助。

  • 如果对象有多个帮助主题(因为对象有扩展名或对象有多个文档接口),则会打开一个列出了相关帮助主题的对话框。双击条目,可打开相应的帮助主题。

  • 如果对象只有一个帮助主题,则直接打开帮助主题。

示例:

# To see the API of the inspect api itself
inspectapi.dir(inspectapi)

输出:

Members of type 'InspectAPI'
Methods:
  dir (obj, verbose = False)
  dir_events (obj, verbose = False)
  dir_fields (obj, verbose = False)
  dir_methods (obj, verbose = False)
  dir_properties (obj, verbose = False)
  help (obj = null)
# To see the API of projects variable
inspectapi.dir(projects)

输出:

Members of type 'ScriptProjects'
Properties:
  [get] all
  [get] primary
Methods:
  convert (stPath, stOutputPath, converter, bPrimary)
  convert (stPath, stOutputPath, converterGuid, bPrimary)
  create (stPath, bPrimary)
  get_all ()
  get_by_path (stPath)
  get_primary ()
  open (stPath, stPassword, bPrimary)
  open (stPath, encryption_password, session_user, session_password, bPrimary)
  open_archive (stArchiveFile, stProjectPath, bOverwrite, stPassword)
  open_archive (stArchiveFile, stProjectPath, bOverwrite, encryption_password, session_user, session_password)
# open a project
proj = projects.open("c:\\temp\\MyScript.py ")
# To see API of primary project
inspectapi.dir(projects.primary)

输出(截断):

Members of type 'ScriptProject'
Properties:
  [set] active_application
  [get] dirty
  [get] handle
  [get] has_library_manager
  [get] has_project_info
  [get] is_root
  [get] library
  [get] path
  [get] primary
  [get] project
  [get] svn
  [get] user_management
Methods:
  add (stName, id, stModuleId)
  add (stName, iType, stId, stVersion, stModuleId)
  check_all_pool_objects ()
  clean_all ()
  close ()
  compare_to (projectFile)
  compare_to (projectFile, ignoreWhiteSpace, ignoreComments, ignoreProperties)
  ...
  logout ()
  save ()
  save_archive (stArchiveFile)
  save_archive (stPath, additional_categories)
  save_archive (stPath, comment, additional_categories)
  save_archive (stPath, additional_files, additional_categories)
  ....
  save_as (stPath, stPassword)
  save_as_compiled_library (destination_name)
  set_active_application (value)
  update ()
# search objects by name (e.g. a device with name SERCOSIII)
objs = projects.primary.find("SERCOSIII", True)
# extract object at index 0 from returned list (if found).
theObj = obj[0]
# see the available API of the found object SERCOSIII
inspectapi.dir(theObj)

输出(截断):

Members of type 'ScriptObject'
Properties:
  [get] connectors
  [get] device_parameters
  [get] embedded_object_types
  [get] guid
  [get] handle
  [get] has_textual_declaration
  [get] has_textual_implementation
  [get] index
  [get] is_application
  [get] is_device
  [get] is_explicit_connector
  [get] is_folder
  [get] is_libman
  [get] is_project_info
  [get] is_root
  [get] is_task
  [get] parent
  [get] project
  [get] svn
  [get] type
Methods:
  add (stName, id, stModuleId)
  add (stName, iType, stId, stVersion, stModuleId)
  allowed_interfaces_at (index)
  can_convert (targetDeviceId, targetModuleId = null)
  can_convert (targetType, targetId, targetVersion, targetModuleId = null)
  convert (targetDeviceId, targetModuleId = null)
  convert (targetType, targetId, targetVersion, targetModuleId = null)
  create_folder (stFolderName)
  disable ()
  enable ()
  export_native (destination, includeChildren, profileName, reporter)
  export_xml (reporter, stPath, bRecursive)
  export_xml (reporter, stPath, bRecursive, bExportFolderStructure)
  export_xml (reporter, stPath, bRecursive, bExportFolderStructure, bPlainText)
  export_xml (stPath, bRecursive, bExportFolderStructure, bPlainText)
  find (namePath)
  find (stName, recursive)
  get_address ()
  get_all_parameters ()
  ...
  move (newParent, nNewIndex)
  plug (stName, id, stModuleId)
  plug (stName, iType, stId, stVersion, stModuleId)
  reboot_plc ()
  remove ()
  rename (stNewName)
  reset_diagnosis_messages ()
  set_communication_address (address)
  set_gateway_and_address (stGateway, stAddress)
  set_gateway_and_address (gateway, stAddress)
  set_parameter (parameter, value)
  set_parameter_iec_address (identifier, connectorId, iecAddress)
  set_parameter_io_variable_mapping (identifier, connectorId, variable, createVariable = False)
  set_parameter_io_variable_mapping (identifier, connectorId, subElementIndex, variable, createVariable = False)
  set_simulation_mode (bSimulation)
  unplug ()
  update (id, stModuleId)
  update (iType, stId, stVersion, stModuleId)