Skip to content

Commit

Permalink
Merge pull request stlehmann#139 from stlehmann/pr/132
Browse files Browse the repository at this point in the history
Fix documentation and test issues with DeviceNotifications
  • Loading branch information
stlehmann authored May 11, 2020
2 parents 0d365ed + 91f705b commit ef0167d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
8 changes: 4 additions & 4 deletions doc/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ can be seen here:
>>> from ctypes import sizeof
>>>
>>> # define the callback which extracts the value of the variable
>>> def callback(addr, notification, user_handle):
>>> def callback(notification, data):
>>> contents = notification.contents
>>> var = next(map(int, bytearray(contents.data)[0:contents.cbSampleSize]))
>>>
Expand Down Expand Up @@ -325,15 +325,15 @@ For more information about the NotificationAttrib settings have a look at
**Here are some examples of callbacks for other datatypes:**

```python
def callbackBool(adr, notification, user):
def callbackBool(notification, data):
contents = notification.contents
var = map(bool, bytearray(contents.data)[0:contents.cbSampleSize])[0]

def callbackInt(adr, notification, user):
def callbackInt(notification, data):
contents = notification.contents
var = map(int, bytearray(contents.data)[0:contents.cbSampleSize])[0]

def callbackString(adr, notification, user):
def callbackString(notification, data):
dest = (c_ubyte * contents.cbSampleSize)()
memmove(addressof(dest), addressof(contents.data), contents.cbSampleSize)
# Remove nullbytes
Expand Down
13 changes: 5 additions & 8 deletions pyads/ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ def add_device_notification(adr, data, attr, callback, user_handle=None):
:param Union[str, Tuple[int, int] data: PLC storage address as string or Tuple with index group and offset
:param pyads.structs.NotificationAttrib attr: object that contains
all the attributes for the definition of a notification
:param callback: callback function that gets executed on in the event
of a notification
:param callback: callback function that gets executed in the event of a notification
:rtype: (int, int)
:returns: notification handle, user handle
Expand Down Expand Up @@ -980,8 +979,7 @@ def add_device_notification(self, data, attr, callback, user_handle=None):
:param Union[str, Tuple[int, int] data: PLC storage address as string or Tuple with index group and offset
:param pyads.structs.NotificationAttrib attr: object that contains
all the attributes for the definition of a notification
:param callback: callback function that gets executed on in the event
of a notification
:param callback: callback function that gets executed in the event of a notification
:rtype: (int, int)
:returns: notification handle, user handle
Expand All @@ -999,7 +997,7 @@ def add_device_notification(self, data, attr, callback, user_handle=None):
>>> plc = pyads.Connection('127.0.0.1.1.1', 851)
>>>
>>> # Create callback function that prints the value
>>> def mycallback(adr, notification, user):
>>> def mycallback(notification, data):
>>> contents = notification.contents
>>> value = next(
>>> map(int,
Expand All @@ -1011,11 +1009,10 @@ def add_device_notification(self, data, attr, callback, user_handle=None):
>>> # Add notification with default settings
>>> attr = pyads.NotificationAttrib(size_of(pyads.PLCTYPE_INT))
>>>
>>> hnotification, huser = plc.add_device_notification(
>>> adr, attr, mycallback)
>>> handles = plc.add_device_notification("GVL.myvalue", attr, mycallback)
>>>
>>> # Remove notification
>>> plc.del_device_notification(hnotification, huser)
>>> plc.del_device_notification(handles)
"""
if self._port is not None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_connection_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def test_write_by_name_with_handle(self):
self.plc.release_handle(handle)

def test_device_notification(self):
def callback(adr, notification, user):
def callback(notification, data):
pass

handle_name = "test"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def test_write_by_name(self):
self.assert_command_id(requests[2], constants.ADSCOMMAND_WRITE)

def test_device_notification_by_name(self):
def callback(adr, notification, user):
def callback(notification, data):
pass

handle_name = "TestHandle"
Expand All @@ -500,7 +500,7 @@ def callback(adr, notification, user):
self.assert_command_id(requests[2], constants.ADSCOMMAND_DELDEVICENOTE)

def test_device_notification_by_tuple(self):
def callback(adr, notification, user):
def callback(notification, data):
pass

n_index_group = 1
Expand All @@ -527,7 +527,7 @@ def callback(adr, notification, user):
self.assert_command_id(requests[1], constants.ADSCOMMAND_DELDEVICENOTE)

def test_device_notification_data_error(self):
def callback(adr, notification, user):
def callback(notification, data):
pass

attr = NotificationAttrib(length=4)
Expand Down

0 comments on commit ef0167d

Please sign in to comment.