使用Pyads库时如何等待变量更改?

2024-04-29 01:43:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在与TwinCatAMR合作一个项目。我使用Python作为两个系统之间的通信媒介。我在等待变量改变值时遇到了一个问题。我有一个类型为BOOL的变量,希望在变量更改时执行某个操作。有人能帮我吗

另外,我已经通知了变量的变化

import pyads

PLC = pyads.Connection('127.0.0.1.1.1', pyads.PORT_SPS1)
PLC.open()

StnF = PLC.read_by_name('GVL.AGVgotoStnF', pyads.PLCTYPE_BOOL)
print(StnF)

if StnF == 'TRUE' :
    ArrStnF = PLC.write_by_name('GVL.iPosAGV',3,pyads.PLCTYPE_INT)
    print(ArrStnF)

Tags: 项目nameby系统plcboolprintpyads
1条回答
网友
1楼 · 发布于 2024-04-29 01:43:22

您正在查找通知。{a1}的文档给出了和{a2}如何做到这一点:

import pyads
from ctypes import sizeof

# define the callback which extracts the value of the variable
def callback(notification, data):
    contents = notification.contents
    var = next(map(int, bytearray(contents.data)[0:contents.cbSampleSize]))

plc = pyads.Connection('127.0.0.1.1.1', pyads.PORT_SPS1)
plc.open()
attr = pyads.NotificationAttrib(sizeof(pyads.PLCTYPE_INT))

# add_device_notification returns a tuple of notification_handle and
# user_handle which we just store in handles
handles = plc.add_device_notification('GVL.integer_value', attr, callback)

# To remove the device notification just use the del_device_notication
# function.
plc.del_device_notification(*handles)

相关问题 更多 >