【问题标题】:How to wait for a variable change when using Pyads library?使用 Pyads 库时如何等待变量更改?
【发布时间】:2020-11-15 17:14:05
【问题描述】:

我正在与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)

【问题讨论】:

    标签: python twincat twincat-ads


    【解决方案1】:

    您正在寻找通知。 pyads 的文档给出了和 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)
    

    【讨论】:

    • @AbhishekKp 不客气。那你能把它标记为已回答吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    相关资源
    最近更新 更多