Skip to content

Commit 1f456a4

Browse files
committed
Separate subscription from the PDO's read() and save() methods.
Normally one needs to call read() or save() on a single pdo (or the node's whole PDO collection) in order to receive any such objects from the network. That however requires quite a few SDO exchanges to make sure the node's PDO configuration matches the parameters configured in the object. For applications where the PDO configuration is stored persistently in the node (e.g. device EEPROM), doing this SDO exchange can be skipped entirely if the application programmer takes care to mirror the same configuration in the python-canopen objects. Another use case is reconnecting to a node for which the same python-canopen script previously ran and the PDO configuration is still known to be valid. Factor out a new method subscribe() from read() and save() to offer doing only that last part via the public API. Adapt the log message and make sure it is logged in read() as well.
1 parent 179022c commit 1f456a4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

canopen/pdo/base.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def save(self):
5959
for pdo_map in self.map.values():
6060
pdo_map.save()
6161

62+
def subscribe(self):
63+
"""Register the node's PDOs for reception on the network.
64+
65+
This normally happens when the PDO configuration is read from
66+
or saved to the node. Use this method to avoid the SDO flood
67+
associated with read() or save(), if the local PDO setup is
68+
known to match what's stored on the node.
69+
"""
70+
for pdo_map in self.map.values():
71+
pdo_map.subscribe()
72+
6273
def export(self, filename):
6374
"""Export current configuration to a database file.
6475
@@ -331,8 +342,7 @@ def read(self):
331342
if index and size:
332343
self.add_variable(index, subindex, size)
333344

334-
if self.enabled:
335-
self.pdo_node.network.subscribe(self.cob_id, self.on_message)
345+
self.subscribe()
336346

337347
def save(self):
338348
"""Save PDO configuration for this map using SDO."""
@@ -387,9 +397,19 @@ def save(self):
387397
self._update_data_size()
388398

389399
if self.enabled:
390-
logger.info("Enabling PDO")
391400
self.com_record[1].raw = self.cob_id | (RTR_NOT_ALLOWED if not self.rtr_allowed else 0x0)
401+
self.subscribe()
402+
403+
def subscribe(self):
404+
"""Register the PDO for reception on the network.
392405
406+
This normally happens when the PDO configuration is read from
407+
or saved to the node. Use this method to avoid the SDO flood
408+
associated with read() or save(), if the local PDO setup is
409+
known to match what's stored on the node.
410+
"""
411+
if self.enabled:
412+
logger.info("Subscribing to enabled PDO 0x%X on the network", self.cob_id)
393413
self.pdo_node.network.subscribe(self.cob_id, self.on_message)
394414

395415
def clear(self):

0 commit comments

Comments
 (0)