简介

EcoStruxure Machine Expert 中的 Python 脚本

EcoStruxure Machine Expert 包括 Python 解译器,可作为一款强大的工具来实现开发环境的自动化。

您可以通过 EcoStruxure Machine Expert Logic Builder 主菜单中的菜单命令工具 > 脚本 > 执行脚本文件... 来执行 Python 脚本文件。

EcoStruxure Machine Expert 中 Python 脚本的文档包含若干部分:

访问 EcoStruxure Machine Expert 中的 Python 解译器章节中介绍了如何打开 EcoStruxure Machine Expert 的 Python 解译器、如何用它来执行脚本文件以及如何与它开展交互。使用 Logic Builder Shell使用 Logic Builder Scripting Immediate 视图Logic Builder Shell 和 Scripting Immediate 视图中的键盘命令章节更详细地介绍了各个部分。

Schneider Electric 脚本引擎示例CoDeSys 脚本引擎示例章节就如何用 Python 脚本实现 EcoStruxure Machine Expert 自动化提供了多种示例。这些示例的主旨都是为了显示 Python 脚本可用的 EcoStruxure Machine Expert 特有 API(应用程序编程接口)的功能。

深入利用 EcoStruxure Machine Expert Python API(使用 dir() 和 inspectapi)最佳做法EcoStruxure Machine Expert 脚本 - Python API 章节概述了 Python 脚本的入门以及如何深入利用所提供的 API。

EcoStruxure Machine Expert 在线帮助的“ EcoStruxure Machine Expert编程”部分中章节Schneider Electric - 脚本引擎类库脚本引擎插件 API 引用介绍了 Python 脚本可以的 EcoStruxure Machine Expert 特有 API。您可以按照上述示例中所述的相同方式来使用这些成员。

为 Python 选择开发环境

可用的 Python 文件编辑方案有多种,如 Notepad++、搭载 PTVS (Python Tools for Visual Studio) 的 Microsoft Visual Studio 或 PyCharm。选择符合您需求的编辑器。

另请参阅以下章节:

o将 Logic Builder Shell 与 Microsoft Visual Studio 和 PTVS 一起使用

o将 Logic Builder Shell 与 JetBrains PyCharm 一起使用

o将 Logic Builder Shell 与 Microsoft Visual Studio Code 和 Python Extension 一起使用

Python V2.x 与 Python  V3.x 之间的兼容性

EcoStruxure Machine Expert 随附的 Python 解译器是基于 Python 版本 2.7 的 IronPython 实现。

注意: 版本 2.x 与版本 3.x 之间的 Python 语言定义有一些重大修改。虽然 EcoStruxure Machine Expert 随附的 Python 解译器是基于 Python V2.x 的,但您也可以编写与版本 3.x 实现相兼容的代码。例如,这会涉及到使用 from __future__ import print_function 语句。

有关详细信息,请访问网站

ohttp://wiki.python.org/

ohttp://docs.python.org/

新功能的示例:

from __future__ import print_function
from __future__ import division

# New Python print syntax
print('Hello World!')
# Division
# Python 2 return an integer and rounds off
# Python 3 returns a float
print(17/3)

编码约定

为了协调和促进同一编程项目中不同编程人员的工作,有必要就采用一种共同编程风格达成一致意见。EcoStruxure Machine Expert 遵循 Python 代码的样式指南。EcoStruxure Machine Expert 随附的脚本和示例也遵循这些惯例。如要提高代码的可读性,建议您的代码页遵循这些惯例。

有关详细信息,请参阅 http://www.python.org/dev/peps/pep-0008/ 上的 Python 代码风格指南

有用的链接

有关详细信息,请访问以下网址:

o提供有教程与语言参考的官方 Python 网页,地址为:http://docs.python.org/

o官方 Python 博客,地址为:http://blog.python.org/

oPython 初学者指南,地址为:http://wiki.python.org/moin/BeginnersGuide

o提供有示例和有用信息的官方 CoDeSys 论坛,地址为:http://forum.codesys.com/

oIronPython 解译器,地址为:http://ironpython.codeplex.com/

oPyTools(Visual Studio 插件),地址为:http://pytools.codeplex.com/

oPyCharm(Visual Studio 插件),地址为:http://www.jetbrains.com/pycharm

o如何使用 PyCharm Debugging,地址为:http://www.jetbrains.com/help/pycharm/2017.1/debugging.html

oVisual Studio Code,地址为:http://code.visualstudio.com

oPython extension,地址为:http://marketplace.visualstudio.com/items?itemName=donjayamanne.python

oIronPython Cookbook,地址为:http://www.ironpython.info/index.php/Contents

o免费的 Galileo Openbook(仅以德语提供),地址为:http://openbook.galileocomputing.de/python/