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

How to wait for a variable change when using Pyads library?

我正在与 TwinCatAMR 合作一个项目。我使用 Python 作为两个系统之间的通信媒介。我在等待变量更改值时遇到问题。我有一个 BOOL 类型的变量,想在变量发生变化时执行某个动作。有人可以帮我解决这个问题吗?

P.S。我也通知了变量的变化。

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)

您正在查找通知。 pyads gives and example 如何做到这一点的文档:

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)