EcoStruxure Machine Expert 1.1版本不支持M258、LMC058和LMC078控制器。

使用 Logic Builder Shell

启动 Logic Builder Shell

LogicBuilderShell.exe 位于 EcoStruxure Machine Expert 的安装目录中。

Logic Builder Shell 提供基于 IronPython ipy.exe 的 EcoStruxure Machine Expert REPL。这是安装在 PC 上时标准 IronPython 包的 REPL。因此,它提供相同的功能,如选项卡完成、Python 脚本调试支持等。此外,它还允许访问 EcoStruxure Machine Expert Python API。

双击 LogicBuilderShell.exe,在控制台中启动 Logic Builder Shell(不含参数)。

结果:显示输入提示 (>>>):

G-SE-0046243.1.gif-high.gif

 

 

命令

在输入提示 (>>>) 中,输入 Python 语句,然后按 Enter 来执行这些语句。

以下一系列命令举例说明了如何打开项目、搜索设备节点以及打印名称:

IronPython 2.7.4 (2.7.4.40) on .NET 4.0.30319.18444 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> projectName = "C:\\temp\\MyProject.project"
>>> proj = projects.open(projectName)
>>> sercosNode = proj.find("SERCOSIII", True)[0]
>>> print(sercosNode.get_name(False))
SERCOSIII
>>>

用法:

LogicBuilderShell.exe [options] [file.py|- [arguments]]

[options] 在下面的命令行参数列表中定义。

[arguments] 是传送到脚本文件的参数的占位符。

Logic Builder Shell 的命令行参数:

命令行参数

描述

示例

-h / --help

打印 IronPython 命令行帮助。

LogicBuilderShell.exe --help
LogicBuilderShell.exe -h

-m <module>

以脚本形式运行库模块。

LogicBuilderShell.exe -m pdb
MyScript.py

-i

脚本运行结束后,执行交互式检查。

LogicBuilderShell.exe -i
MyScript.py

<Script File>

运行指定的 Python 脚本文件。

LogicBuilderShell.exe
MyScript.py

--nologo

在启动期间,跳过对 Logic Builder 信息文本的显示。

在停机期间,跳过对 …terminated 消息的显示。

LogicBuilderShell.exe --nologo

您可以将不同的命令行参数组合到一起。比如,-i 参数可以与指定的 Python 脚本文件和 -nologo 组合。

列表中仅显示了 IronPython 命令行参数的一种选择。有关完整的列表,请参阅 IronPython 网站和文档。

Logic Builder Shell 用例

您可以在不同情形中使用 Logic Builder Shell。以下用例显示了它如何帮助改善 EcoStruxure Machine Expert 和 Python 脚本语言的可用性。

用例 1

在持续集成 (CI) 系统中执行脚本时 - 比如为了从 Subversion (SVN) 中签出 EcoStruxure Machine Expert 项目、编译项目并保存为库,您需要将脚本输出打印到控制台。由于 LogicBuilderShell.exe 是实际的控制台应用程序,您可以重定向输出或者在获取输出并将其添加到生成日志的 CI 系统(比如,Jenkins)中调用输出。

示例:

LogicBuilderShell.exe MyScript.py > output.txt

用例 2

您可以将 LogicBuilderShell.exe 集成作为 Python 工具用于 Visual Studio(请参阅调试章节)。在这种情况下,LogicBuilderShell.exe 用于调试 Visual Studio 与 EcoStruxure Machine Expert 之间的通讯并执行语句。

用例 3

LogicBuilderShell.exe 让您能够使用内置命令行调试功能调用的 pdb 模块。

示例:

LogicBuilderShell.exe -m pdb MyScript.py

用例 4

在无 UI 模式下测试您的 Python 脚本,确认其运行正常。在控制台模式中,可以通过控制台输入。请注意,只有在 LogicBuilderShell.exe 中,才能够从控制台读取输入。在 Logic Builder 用户界面中提供的 Scripting Immediate 视图中,无法执行控制台输入。否则会忽略语句 readline()

示例:

import sys
print("Please enter a text: ")
text = sys.stdin.readline()
print("Your entered text: " + text)

用例 5

浏览 Python 和 EcoStruxure Machine Expert API 及文档。

o使用内置的 help() 功能来通过 IronPython shell 访问 Python 帮助。为此,应确保互联网连接可用。

示例:

help()      # start the built-in help module
topics      # list all available topics
##### here you'll get the list of available topics #####
LIST        # dumps the help to work with lists.
##### here you'll get the help working with lists #####
quit        # leaf build-in help feature

o使用内置的 dir() 功能来打印可用的 api 功能。

示例:

dir()       # prints the list of defined variables and functions in current script scope
##### here you'll get the list of defined variables or functions in current script scope #####
import sys
dir(sys)    # prints the available functions defined in sys module
##### here you'll get the list of defined variables or functions in sys module #####

o使用 inspectapi 模块来浏览 EcoStruxure Machine Expert Python API。由于技术限制,Python 的内置 dir() 功能无法为 EcoStruxure Machine Expert Python API 打印 API 帮助。为此,请使用 inspectapi

示例:

inspectapi.dir(projects)  # prints the available API functions of "projects" which provides access e.g. to open a SoMachine project
##### here you'll get the SoMachine API provided via "projects" variable #####

用例 6

-i 开关用作参数,连同指定的脚本一起传送到 LogicBuilderShell.exe,以在脚本运行结束时获得 shell 提示。这能够有助于在脚本运行结束后开展交互式检查,比如以便查看变量的内容。

Logic Builder Shell 的优点

建议使用 LogicBuilderShell.exe 来执行脚本,而不是使用 LogicBuilder.exe 在无 UI 模式中运行脚本。这两种方法依然都可行,但 LogicBuilderShell.exe 的命令行具有更好的可用性,Logic Builder Shell 则将输出打印至控制台。

使用 LogicBuilder.exe 在无 UI 模式中运行脚本的实例:

LogicBuilder.exe -noui --runscript="<script file>"

LogicBuilderShell.exe 使用示例:

LogicBuilderShell.exe "<script file>"

用命令行调试器模块 pdb 调试 EcoStruxure Machine Expert Python 脚本

LogicBuilderShell.exe 内置有带命令行调试功能的 pdb 模块(有关更多文档,请参阅 https://docs.python.org/2/library/pdb.html)。这个功能专为专家设计,用于调试 Python 脚本或查找问题。IronPython 本身是一个能够加载到 shell 中的模块。在通过 pdb 功能 run(…) 启动自己的脚本时,您可以在控制台中调试 Python 代码。控制台应用程序仅提供基于文本的 UI。

注意: 命令行调试器的使用可能较为复杂。如要获得较为简便的调试体验,可以考虑使用 Microsoft Visual Studio 和 PTVS(有关更多信息,请参阅将 Logic Builder Shell 与 Microsoft Visual Studio 和 PTVS 一起使用)。

模块 pdb 为 Python 程序定义交互式源代码调试器。它提供下列功能:

o在源行层级设置(有条件)断点和单步运行

o检查栈帧

o列出源代码

o在任意栈帧的上下文中对任意 Python 代码求值

如要直接在 pdb 中启动 Python 脚本,请使用以下命令行语法:

LogicBuilderShell.exe -m pdb MyScript.py

选择 pdb 调试器命令(有关更多详细信息,请参阅网上的 Python 帮助):

调试器命令

命令描述

h(elp) [command]

在没有参数的情况下,打印可用命令列表。

在有命令作为参数的情况下,打印有关该命令的帮助。

help pdb 显示文档文件。

r(un)

重启调试后的 Python 程序。

b(reak)

利用行编号参数,在文件中的此位置设置中断。

利用功能参数,在该功能内第一个可执行语句处设置中断。行编号可作为前缀添加到文件名,可使用冒号来指定另一个文件(可能是尚未加载的文件)中的断点。文件可通过 sys.path 搜索。每个断点被分配有一个供其他断点命令引用的编号。

如果存在第二个参数,则断点为一个表达式,在执行断点前,此表达式的求值必须为 TRUE。

在没有参数的情况下,会列出所有中断。对于所列出的每个断点,同时还包含遇到断点的次数、忽略次数和相关条件(如有)。

l(ist) [first[, last]]

列出文件的源代码。

在没有参数的情况下,围绕当前行列出 11 行,或者继续先前的列出操作。

在有一个参数的情况下,围绕该行列出 11 行。

在有两个参数的情况下,列出给定范围;如果第二个参数小于第一个参数,则理解为计数。

c(ontinue)

继续执行,仅在遇到断点时停止。

n(ext)

继续执行,直至到达当前功能中的下一行,或者直至返回。(next 与 step 之间的区别在于,step 在所调用的功能内部停止,而 next 以(几乎)全速执行所调用的功能,仅在到达当前功能中的下一行时才停止。)

s(tep)

执行当前行,在第一个可能的情形(在被调用的功能中,或者在到达当前功能中的下一行时)下停止。

u(ntil)

继续执行,直至到达行编号比当前行大的行或者直至从当前帧返回。

r(eturn)

继续执行,直到当前功能返回。

w(here)

打印堆栈轨迹,最新的帧位于最底部。箭头指示当前帧,它确定大多数命令的上下文。

q(uit)

退出调试器。中止正被执行的程序。

利用 pdb 进行调试的示例

要调试的 Python 示例脚本:

print("Demonstration how to use Visual Studio + PTVS to debug SoMachine Python scripts")

# close open project
if projects.primary:
    print("Close project")
    projects.primary.close()

print("Open project")
projects.open("c:\\Temp\\MyProject.project")

sercosDevice = projects.primary.find("SERCOSIII", True)[0]

sercosDeviceName = sercosDevice.get_name(False)

print("Sercos device name: " + sercosDeviceName)

print("Close project")
projects.primary.close()

在下面所列出的以 (pdb) 开头的每行中,执行调试器命令,如 list (l) 或 help 或 next (n) 或 quit (q)。在其之间,您可以看到语句或其被执行的输出。

LogicBuilderShell.exe -m pdb SoMachinePythonDemonstration.py
SoMachine Logic Builder Shell version 1.53.16.0
Copyright (C) Schneider Electric Automation GmbH 2014-2015

Demonstration how to use Visual Studio + PTVS to debug SoMachine Python scripts
> C:\temp\somachinepythondemonstration.py(5)<module>()
-> if projects.primary:
(Pdb) help

Documented commands (type help <topic>):
========================================
EOF    bt         cont      enable  jump  pp       run      unt
a      c          continue  exit    l     q        s        until
alias  cl         d         h       list  quit     step     up
args   clear      debug     help    n     r        tbreak   w
b      commands   disable   ignore  next  restart  u        whatis
break  condition  down      j       p     return   unalias  where

Miscellaneous help topics:
==========================
exec  pdb

Undocumented commands:
======================
retval  rv
(Pdb) l
  1
  2     print("Demonstration how to use Visual Studio + PTVS to debug  SoMachine Python scripts")
  3
  4     # close open project
  5  -> if projects.primary:
  6         print("Close project")
  7         projects.primary.close()
  8
  9     print("Open project")
 10     projects.open("c:\\Temp\\MyProject.project")
 11
(Pdb) n
> C:\temp\somachinepythondemonstration.py(9)<module>()
-> print("Open project")
(Pdb) n
Open project
> C:\temp\somachinepythondemonstration.py(10)<module>()
-> projects.open("c:\\Temp\\MyProject.project")
(Pdb) l
  5     if projects.primary:
  6         print("Close project")
  7         projects.primary.close()
  8
  9     print("Open project")
 10  -> projects.open("c:\\Temp\\MyProject.project")
 11
 12     sercosDevice = projects.primary.find("SERCOSIII", True)[0]
 13
 14     sercosDeviceName = sercosDevice.get_name(False)
 15
(Pdb) n
IOError: IOError(...oject'.")
> C:\temp\somachinepythondemonstration.py(10)<module>()
-> projects.open("c:\\Temp\\MyProject.project")
(Pdb) q
SoMachine Logic Builder Shell terminated.