Closed gbcarlos closed 1 year ago
I have an implementation of this, let me dig it out for you. Give me a few days as quite busy and can't copy it in verbatim as its work related
So...you can't use the decorator inside a class unfortunately, you have to set up the device notifications manually.
e.g.
class Example:
def __init__:
plc = pyads.Conection(...)
heartbeat = 0
set_up_notifications()
def set_up_notifications(self):
self.plc.add_device_notification(
VAR_NAME,
pyads.NotificationAttrib(ctypes.sizeof(pyads.PLCTYPE_BOOL)),
self.on_plc_heartbeat,
)
def on_plc_heartbeat(self, *_):
self.heartbeat += 1
An example for detcting the plc status change:
self._plc.add_device_notification(
(int("0xF100", 16), int("0x0000", 16)), # ADSIGRP_DEVICE_DATA, ADSIOFFS_DEVDATA_ADSSTATE
pyads.NotificationAttrib(ctypes.sizeof(pyads.PLCTYPE_INT)),
self._on_plc_status_change,
def _on_plc_status_change(self, notification, _):
"""Notification callback for a change in PLC state (run to config etc)."""
*_, value = self._plc.parse_notification(notification, pyads.PLCTYPE_INT) # type: ignore
if value != pyads.ADSSTATE_RUN:
self._plc_fatal_com_error_message = "PLC exited run mode"
Please can yu close this issue if that helps. Thanks.
Thanks a lot @chrisbeardy! That helps
Hi,
This might be more a Python related question than to Pyads but maybe there is a workaround. I'm trying to use the device notification decorator inside a class,
self
, however, is not known when using it in the decorator. Did someone try out to use the decorator inside a class?This is my code: