admin 发表于 2020-6-17 10:55:40

在CODESYS软件中使用Python

Python是一种动态语言。您可以从简单的线性编程样式(批处理文件)开始,然后再添加必要和更强大的功能,例如条件,循环,函数,异常,类和模块。Python在运行时模式下更为典型,它使用自动垃圾收集器来保护程序员免受意外损坏整个系统。IronPython是.NET的Python实现,并允许完全访问.NET框架和类。IronPython解释器的实现基于Python版本2.7。在CODESYS软件中使用Python入门
[*]使用文本编辑器,创建一个hello.py文件,在其中输入以下内容:print("Hello, automation!")
[*]启动CODESYS,然后单击工具‣脚本‣执行脚本文件,在文件系统中选择文件hello.py。

[*]在消息视图中,会显示如下:
Python的基本语法(带有示例)Python与C语言相似,但是有一些明显的区别和独特的属性。Python与C和ST等语言之间最明显的语法区别是Python解析器通过缩进识别块结构。没有BEGIN/END或大括号{}来识别的块IF/ELSE的条件下,FOR和WHILE循环,或函数。注释使用#开头并延伸到该行的末尾。在源代码的第一行和第二行中,您可以设置一个特殊的标记来声明文件的编码。如果不需要ASCII字符,我们建议您使用UTF-8作为编码。出于调试目的,您可以使用print来简化输出。使用%运算符,您可以实现类似于C函数的功能printf()。输出显示在CODESYS的消息视图中。例程:# encoding:utf-8

# 定义一个带有参数i的函数
def do_something(i):
    # if 分支
    if i>0:
      print("The value is: %i" % i)
      sum += i
      print("The new sum is: %i" % sum)

    # else if (可选,可以没有,也可以是多个elif分支)
    elif i==0:
      print("The sum did not change: %i" % sum)

    # 最后的else分支(可选)。
    else:
      handle_error()

# 死循环
while True:
    print("I got stuck forever!")属于同一个块的所有内容都必须缩进相同的距离。括号和花括号等元素的优先级高于缩进。以下代码段即使是以不良的编程风格编写的,但却是完全正确的。# 警告:下面的样式不好。 孩子们,不要在家尝试!
foo = 0
bar = 1
if foo >= bar:
               print("foobar")
else:
    print(
          "barfoo"
)为避免歧义,需要避免在文件中混用制表符和空格。Python区分大小写,与ST相似且相反。关键字,例如def,if,else,和while,必须是小写(相较于ST规则:关键字是大写)。两个标识符(例如“ i”和“ I”)表示两个不同的变量。以下关键字在Python中是保留字,不允许用作变量,函数等的标识符:and | as | assert | break | class | continue | def | del | elif | else | except | exec | finally | for | from | global | if | import | in | is | lambda | not | or | pass | print | raise | return | try | while | with | yield变量和数据类型Python是一种功能强大的动态类型化语言-所有类型信息都在运行时进行评估。 变量只保存对象的引用,以及对象知道它的类型,而不是变量。 当程序员尝试执行不可能的操作(例如,添加整数和字符串)时,Python在运行时引发异常。因此,没有变量及其类型的声明。在Python中,仅创建变量以为其分配值。这在类型强且静态的C和ST中完全不同。每个变量都用一种类型声明,并且在编译时,编译器检查该类型和运算符是否被允许。参考以下示例来处理变量:# 将整数1分配给变量i(同时“创建”变量”)
i = 1

# 将字符串“foobar”分配给变量s
s = "foobar"

# 将5加到整数i,这等效于i = i + 5
i += 5
# 现在i的值为整数6

# 尝试对i和s相加,执行时会抛出异常
# TypeError: unsupported operand type(s) for +: 'int' and 'str'
result = i + s

# 删除变量同时可以“取消声明”
# 进一步访问变量i将引发NameError异常,
# 由于该变量已不存在
del i

i += 5 # 现在抛出异常: NameError: name 'i' is not defined所有现有变量仅引用一个值。在Python中,没有未分配或未初始化的变量。为了表示没有值,Python提供了一个特殊的对象:None。在C或ST中,您将使用空指针。它的唯一目的是表达“这里没有值”,尽管None实际上是该类的现有实例NoneType。数值类型和浮点数与IEC或C中的数十种整数类型相比,Python中只有一种整数类型。Python中的整数类型没有固定大小。相反,它们会根据需要增长,并且仅受可用内存的限制。from __future__ import print_function

i = 1
print(i)

j = 0x1234   # 十六进制, IEC的16#1234,十进制的4660
k = 0o123    # 八进制, IEC的8#123,十进制的83
l = 0b101010 # 二进制, IEC的2#101010,十进制的42
print(j, k, l)

m = (2 + 3)*10 # 现在k的值为50
print(m)

n = 10 ** 100 # 10的100次方
print(n)输出结果:

Python中只有一种浮点类型,类似于IEC数据类型LREAL。它提供64位IEEE浮点运算。
语法在大多数情况下类似于基于C的语言:# 一个简单的浮点数...
a = 123.456

#包含整数值2
b = 2的浮点数

#可以忽略前导零
c = .3#与0.3相同

# 指数/科学表示形式
d = -123e-5两个特殊情况是True和False,两个常量定义了布尔值。它们的行为与整数值0和相似1,不同之处在于将它们转换为字符串并返回其名称时。# 内置函数“type”可用于查询值的类型。
print("True:", True, type(True))
print("False: ", False, type(False))
print("1:   ", 1, type(1))
print("False + 0: ", False + 0, type(False + 0))
print("True * 5: ", True * 5, type(True * 5))
输出结果:字符串在IronPython中,字符串始终采用Unicode,且为任意长度。如果将它们包含在'或“中,没有任何区别。字符串也可以使用三引号"""或''',来允许使用多行字符串文字。与C相似,可以使用反斜杠()表示特殊字符,而IEC中为此使用了美元符号($)。# encoding:utf-8
from __future__ import print_function

a = "a simple string"
b = 'another string'
c = "strings may contain 'quotes' of the other type."
d = "multiple string literals" ' are concatenated ' '''by the parser'''
e = "Escaping: quotes: \" \' backslash: \\ newline: \r\n ascii code: \x40"
f = """triple-quoted strings may contain newlines, "single"
'quotes' and '''multiquotes''' of the other type"""
g = "Üňíçǿđȩ is also possible: 北京, Москва, Αθήνα, القاهرة"
h = r"c:\raw\strings\retain\backslashes.txt"

# 遍历上面定义的所有变量的序列:
for i in (a,b,c,d,e,f,g,h):
    print(i) # 打印变量的内容输出结果:

Python没有字符类型。 通过使用长度为1的字符串来表示字符。通过这种方式,通过字符串进行迭代或在字符串中使用下标将返回单字符字符串。列表和元组(数据集)列表和元组基本上对应于C和IEC中的数组,但是有一些明显的区别:
[*]始终检查索引访问。访问具有无效索引的列表或元组将引发异常。
[*]列表和元组都可以包含不同类型的元素(其他列表和元组也可以)。而C和IEC则不同,数组只能包含单一类型的元素。
[*]列表是动态的,可以随时添加,删除或替换元素。
[*]元组不可更改:创建元组后,将无法再对其进行修改。
列表是使用list()构造函数创建的。。元组是使用tuple()构造函数创建的。from __future__ import print_function
print("Testing tuples and lists")

# 定义一个元组,其数字从1到10:
t = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print("Tuple:", t)

# 我们可以访问元组的第6个元素。
# 与C中一样,索引计数从0开始。
print("Element 5:", t)

# 使用范围语法可进行更强大的操作:
print("Range:", t) # #下限是包含的,上限是排除的。
print("Range:", t) # 从第3个元素开始,并间隔打印元素。
print("Range[-3:-1]:", t[-3:-1]) # 从最后一个元素的第3个开始,在最后一个元素之前结束(上限是排除的)
print("Range[\:\:-1]:", t[\:\:-1]) # 逆向打印

# 列表类似于元组...
l = # 包含混合类型:3个整数,一个字符串和上面定义的元组。
print("List:", l)

# ...但是可以动态添加或删除元素。
l.append(9) # 在列表中添加9。
print("List with 9:", l)
print("List Range:", l) # 打印第4个和第6个元素。

del l # 删除索引1、12的元素
print("Removed:", l)
del l # 删除索引1和2,即13和'8'。
print("Removed:", l)输出结果:
字典Python还具有哈希表类型(也称为“ hashmap”)。与列表不同,它可以用任何元素(例如字符串)进行索引。其构造函数为dict()。from __future__ import print_function
print("Testing dictionaries")

# 声明一个包含三个条目的字典,第三个是列表
d = {1: "a", 2: "b", "my list": }
print(d)

# 打印键1的值
print(d)

# 删除带有"my list"键的值
del d["my list"]

# 添加带有键3的值4
d = 4
print(d)

# 如果找不到密钥,则“get”方法返回第二个参数。
print(d.get(1, 42))
print(d.get(23, 42))

# 打印字典中的所有键
for key in d:
    print(key)

# 访问未知键的索引将抛出"KeyError"异常!
print(d)结果输出:

然后在最后一行中,脚本终止:

您可以通过单击“ 详细信息”按钮查看堆栈跟踪。在这里,您可以找到有关行号27和未知密钥23的信息。
循环与C和ST的for不同,Python中的循环不计算循环变量,而是在序列上进行迭代。这种序列可以是字典,列表,元组,字符串中的字符或文件中的行。from __future__ import print_function

print("Enumerating over a simple list:")
for i in (1,2,3,4):
    print(i, end=", ") # end=将换行符替换为“,”
print()                # 但在本案例的结尾我们仍然需要换行符。

print("Enumerating over the characters in a string:")
for i in "CODESYS": # 字符表示为长度为1的字符串。
    print(i, end=", ")
print()

print("Enumerating over the integers 1 to 4:")
for i in range(1, 5): # 上限是排除的。
    print(i, end=", ")
print()

print("Enumerating using xrange:")
for i in xrange(5): # xrange与range类似,但是需要, 但是在较大范围内需要较少的内存。
    print(i, end=", ")
print()

print("Enumerating including the item number:")
for i, v in enumerate("CODESYS"):
    print(i, v)结果输出:

除了for循环外,Python还具有while与C和ST中的循环非常相似的循环:
i = 0
while i < 3;
    print(i)
    i += 1if/else类似于其他编程语言中的构造。from __future__ import print_function
i = int(system.ui.query_string("Please enter an integral number..."))
if i < 0:
    print("Your number was negative.")
elif i > 0:
    print("Your number was positive.")
else:
    print("It seems your number was zero.")else分支是可选的,也可以是零,一个或多个elif分支。函数,类和方法Python允许使用方法定义函数和类。具有方法的类基本上类似于ST中的功能块,或者类似于C ++,Java或C#等语言的类。但是,Python不支持接口。有关详细信息,可以参考Python文档以定义函数和类。#定义一个具有名称sum和两个参数a和b的函数:
def sum(a, b):
    return a + b # we return the sum of a and b.

# 现在可以调用上面定义的函数:
print(sum(5,7))

# 现在定义一个类Foo:
class Foo:
    # The class gets a method "bar".
    # Note: for methods, the first parameter is always "self" and
    # points to the current instance. This is similar to "this" in
    # ST and other languages.
    def bar(self, a, b):
      print("bar(%s,%s)" % (a,b))

# 创建该类的实例:
f = Foo()

# 调用实例的bar方法。
f.bar("some", "params")模块和标准库在IEC中,您可以导入库以供其他书面代码重复使用。作为挂件,Python中有可能导入模块。
要创建自己的模块,请编写一个Python文件,该文件定义要提供的功能和类。将此文件保存到与示例脚本相同的目录中。如果命名文件为mymodule.py,则可以使用import mymodule导入。from math import cos, pi

print(pi) # prints 3.14159265359

print(cos(pi)) # prints -1.0以下包含更多示例,这些示例访问有关操作系统,Python版本和解释器的信息:import os
print(os.environ["OS"])

from sys import platform, version, executable
print(platform)
print(version)
print(executable)使用Python脚本访问CODESYS功能CODESYS为脚本提供的所有对象和命令也在Python模块“scriptengine”中。每当启动脚本时,都会隐式导入from scriptengine import这样可以轻松访问CODESYS。但是,如果您的脚本导入了需要访问CODESYS API的模块,则这些模块必须自己导入scriptengine模块。在下表中,您将找到可以在Python脚本中用作入口点的主要对象(类别)。有关入口点的全面文档,请参阅CODESYS ScriptEngine的API参考文档。
示例:打印当前项目的设备树# encoding:utf-8
# 启用新的python3打印语法
from __future__ import print_function

# 打印出当前打开的项目中的所有设备。

print("--- Printing the devices of the project: ---")

# 定义打印功能。
# 此函数以所谓的“docstring”开头,这是在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 treeobj.is_device:
      name = treeobj.get_name(False)
      deviceid = treeobj.get_device_identification()
      print("{0}- {1} {2}".format("--"*depth, name, deviceid))

    # 为子对象递归调用print_tree函数。
    for child in treeobj.get_children(False):
      print_tree(child, depth+1)

# 遍历所有顶级对象,并为其调用print_tree函数。
for obj in projects.primary.get_children():
    print_tree(obj)

print("--- Script finished. ---")设备树(从“设备”视图)显示在消息视图中,所有非设备对象均被忽略:
示例:读取变量# encoding:utf-8
from __future__ import print_function

# 必要时关闭打开的项目:
if projects.primary:
    projects.primary.close()

# 打开项目
proj = projects.open(r"D:\data\projects\Ampel.project")

# 将“Ampel.project”设置为活动应用程序
app = proj.active_application
onlineapp = online.create_online_application(app)

# 登录到设备
onlineapp.login(OnlineChangeOption.Try, True)

# 如果应用程序的状态不是在“运行”,则设置为“运行”
if not onlineapp.application_state == ApplicationState.run:
    onlineapp.start()

# 等待1秒
system.delay(1000)

# 读iVar1的值
value = onlineapp.read_value("PLC_PRG.iVar1")

# 在消息视图或命令行中显示值
print(value)

# 从设备注销并关闭“Ampel.project”
onlineapp.logout()
proj.close()示例:创建和编辑POU# encoding:utf-8
from __future__ import print_function

STRUCT_CONTENT = """\
    a : BOOL;
    b : BIT;
    c : BIT;
"""

UNION_WHOLE = """\
TYPE MyUnion :
UNION
    Zahl : INT;
    Prozent : MyAlias;
    Bits : MyStruct;
END_UNION
END_TYPE
"""

proj = projects.primary

folder = proj.find('DataTypes', recursive = True)

# 创建一个结构DUT,并将变量列表插入第二行第0列的正确位置(行编号从第0行开始)
struktur = folder.create_dut('MyStruct') # 默认为DutType.Structure
struktur.textual_declaration.insert(2, 0, STRUCT_CONTENT)

# 别名类型通过基本类型获取其“内容”,该基本类型将最终在声明部分中以一行形式出现:
# TYPE MyAlias : INT (0..100); END_TYPE
bereich = folder.create_dut('MyAlias', DutType.Alias, "INT (0..100)")

# 除了将变量注入到现有的声明中之外,还可以只替换完整的声明部分,包括样板代码。
union = folder.create_dut('MyUnion', DutType.Union)
union.textual_declaration.replace(UNION_WHOLE)示例:用户界面/与用户的交互# encoding:utf-8
from __future__ import print_function

"""在消息存储和UI上执行一些测试。"""

print("Some Error, Warning and Information popups:")
system.ui.error("Fatal error: Everything is OK. :-)")
system.ui.warning("Your bank account is surprisingly low")
system.ui.info("Just for your information: 42")

print("Now, we ask the user something.")
res = system.ui.prompt("Do you like this?", PromptChoice.YesNo, PromptResult.Yes);
print("The user selected '%s'" % res)

print("Now, the user can choose between custom options:")
res = system.ui.choose("Please choose:", ("First", 2, 7.5, "Something else"))
print("The user selected option '%s'" % str(res)) # res是一个元组

print("Now, the user can choose several options:")
res = system.ui.select_many("Please select one or more options", PromptChoice.OKCancel, PromptResult.OK, ("La Premiere", "The Second", "Das Dritte"))
print("The returned result is: '%s'" % str(res)) # res是一个元组

print("Now, the user can select files and directories")
res = system.ui.open_file_dialog("Choose multiple files:", filter="Text files (*.txt)|*.txt|Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*", filter_index = 0, multiselect=True)
print("The user did choose: '%s'" % str(res)) # res是一个元组,因为multiselect为true。

res = system.ui.save_file_dialog("Choose a file to save:", filter="Text files (*.txt)|*.txt|Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*", filter_index = 0)
print("The user did choose: '%s'" % res)

res = system.ui.browse_directory_dialog("Choose a directory", path="C:\\")
print("The user did choose: '%s'" % res)

print("Now we query a single line string")
res = system.ui.query_string("What's your name?")
print("Nice to meet you, dear %s." % res)

print("Now we query a multi line string")
res = system.ui.query_string("Please tell me a nice story about your life!", multi_line=True)
if (res):
    print("Huh, that has been a long text, at least %s characters!" % len(res))
else:
    print("Hey, don't be lazy!")

print("Username and passwort prompts...")
res = system.ui.query_password("Please enter your favourite password!", cancellable=True)
if res:
    print("Huh, it's very careless to tell me your favourite password '%s'!" % res)
else:
    print("Ok, if you don't want...")

res = system.ui.query_credentials("Now, for real...")
if res:
    print("Username '%s' and password '%s'" % res) # res是2元组
else:
    print("Sigh...")示例:操作项目信息对象# encoding:utf-8
from __future__ import print_function

proj = projects.load("D:\Some.library")

info = proj.get_project_info()

# 设置一些值
info.company = "Test Library Ltd"
info.title = "Script Test Project"
info.version = (0, 8, 15, 4711)
info.default_namespace = "testlibrary"
info.author = "Python von Scriptinger"

# 库工具链中推荐的一些值
info.values["DefaultNamespace"] = "testlibrary"
info.values["Placeholder"] = "testlibrary"
info.values["DocFormat"] = "reStructuredText"

# 现在我们设置一个自定义/供应商特定的值。
info.values["SpecialDeviceId"] = "PLC0815_4711"

# 启用访问器功能的生成,因此IEC应用程序可以在信息屏幕中显示版本。
info.change_accessor_generation(True)

# 并将库设置为发布
info.released = True;

proj.save()示例:调用外部命令并导入PLCOpenXML文件# encoding:utf-8
# 通过命令行svn客户端从Subversion导入PLCOpenXML中的设备。

# 启用新的python 3打印语法
from __future__ import print_function

import sys, os

# 一些变量定义:
SVNEXE = r"C:\Program Files\Subversion\bin\svn.exe"
XMLURL = "file:///D:/testrepo/testfolder/TestExport.xml"
PROJECT = r"D:\test.project"

# 清理所有打开的项目:
if projects.primary:
    projects.primary.close()

# 从Subversion获取plcopenxml数据。
# 将程序的输出捕获到xmldata变量中。
# 'with'构造自动为我们关闭打开的管道。
with os.popen('"' + SVNEXE + '" cat ' + XMLURL, 'r') as pipe:
    xmldata = pipe.read()

# 创建一个新项目:
proj = projects.create(PROJECT)

# 将数据导入项目。
proj.import_xml(xmldata, False)

# 最后保存。 :-)
proj.save()

print("--- Script finished. ---")高级示例:从SVN调用库并将其安装在CODESYS中

import tempfile

if projects.primary:
    projects.primary.close()

tempdir = tempfile.mkdtemp()
URL = "svn://localhost/testrepo/trunk/SvnTestLibrary/"

proj = svn.checkout(URL, tempdir, "testlibrary", as_library=True)
proj.save()

repo = librarymanager.repositories
librarymanager.install_library(proj.path, repo, True)

proj.close()



猫哥 发表于 2020-8-10 18:48:18

看不懂,一脸蒙蔽

biantaotian 发表于 2020-8-24 17:33:22

猫哥 发表于 2020-8-10 18:48
看不懂,一脸蒙蔽

现在越来越多的软件都对Python提供了支持,尤其IRpython使得.net软件能够直接于python交互
页: [1]
查看完整版本: 在CODESYS软件中使用Python