回调通知Pyads的返回值

问题描述 投票:0回答:1

我正在使用 pyads 通过 ADS 从 PLC 提取数据并在 python 中处理这些数据。 由于我必须非常快地提取数据(200 毫秒周期),因此我使用 ADS 通知来始终在值发生变化时获取值,即每 200 毫秒或更快一次。

在 while 循环中,我正在等待数据传入并将它们组合到一个数组中。我必须走这条路,因为数据量很大。每个周期 1000 个值,我必须将它们放在一起,这样我就可以得到每个信号 100000 个值的数组。总共可以有 15 个信号。您可以在下面找到 pyads 文档中的示例。 我正在寻找一种很好的 Python 方式,从 while 循环内的回调函数获取值,并将它们一起构建为 100000 值数组。我想过创建一个类来处理我需要的东西,但我找不到让它工作的方法。 我能想象的唯一选择是使用我不喜欢做的全局变量。

structure_def = (
    ('nVar', pyads.PLCTYPE_DINT, 1000),
    ('nVar2', pyads.PLCTYPE_DINT, 1000),
    ('nVar3', pyads.PLCTYPE_DINT, 1000),
    ('nVar4', pyads.PLCTYPE_DINT, 1000),
    ('nVar5', pyads.PLCTYPE_DINT, 1000))

size_of_struct = pyads.size_of_structure(structure_def)

@plc.notification(ctypes.c_ubyte * size_of_struct)
def callback(handle, name, timestamp, value):
    values = pyads.dict_from_bytes(value, structure_def)
    print(values)

attr = pyads.NotificationAttrib(size_of_struct)
plc.add_device_notification('global.sample_structure', attr, callback)

使用全局变量当然可以。 我尝试一次提取 100000 个值,但当信号量增加时失败。所以我必须拉较小的包。

python callback ads python-decorators twincat-ads
1个回答
0
投票
import pyads

def notification_callback(adr, notification, user):
    print(f"Notification received: ADR: {adr}, Notification: {notification}, User: {user}")
    # Your logic here based on the notification data
    # ...

plc = pyads.Connection('127.0.0.1.1.1', pyads.PORT_SPS1)
plc.open()
notification_handle = plc.add_device_notification('GVL.test', pyads.ADSTIMEDDOUBLE(0, 10), notification_callback)
© www.soinside.com 2019 - 2024. All rights reserved.