1

我正在使用TwinCatand进行一个项目AMR。我使用 Python 作为两个系统之间的通信媒介。我在等待变量更改值时遇到问题。我有一个类型的变量,BOOL并希望在变量更改时执行某个操作。有人可以帮我弄这个吗?

PS我也通知了变量的变化。

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)
4

1 回答 1

2

您正在寻找通知。给出的文档pyads示例如何执行此操作:

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)
于 2020-11-16T17:05:26.350 回答