Skip to content

Commit d6498dc

Browse files
committed
pdo: handle None cob_id gracefully in __repr__ and network calls
1 parent cb1622a commit d6498dc

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

canopen/pdo/base.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ def __init__(self, pdo_node, com_record, map_array):
229229
self._task = None
230230

231231
def __repr__(self) -> str:
232-
return f"<{type(self).__qualname__} {self.name!r} at COB-ID 0x{self.cob_id:X}>"
232+
cob = f"0x{self.cob_id:X}" if self.cob_id is not None else "Unassigned"
233+
return f"<{type(self).__qualname__} {self.name!r} at COB-ID {cob}>"
233234

234235
def __getitem_by_index(self, value):
235236
valid_values = []
@@ -492,7 +493,7 @@ def subscribe(self) -> None:
492493
associated with read() or save(), if the local PDO setup is
493494
known to match what's stored on the node.
494495
"""
495-
if self.enabled:
496+
if self.enabled and self.cob_id is not None:
496497
logger.info("Subscribing to enabled PDO 0x%X on the network", self.cob_id)
497498
self.pdo_node.network.subscribe(self.cob_id, self.on_message)
498499

@@ -540,6 +541,8 @@ def add_variable(
540541

541542
def transmit(self) -> None:
542543
"""Transmit the message once."""
544+
if self.cob_id is None:
545+
raise ValueError("A valid COB-ID has not been configured")
543546
self.pdo_node.network.send_message(self.cob_id, self.data)
544547

545548
def start(self, period: Optional[float] = None) -> None:
@@ -559,6 +562,8 @@ def start(self, period: Optional[float] = None) -> None:
559562

560563
if not self.period:
561564
raise ValueError("A valid transmission period has not been given")
565+
if self.cob_id is None:
566+
raise ValueError("A valid COB-ID has not been configured")
562567
logger.info("Starting %s with a period of %s seconds", self.name, self.period)
563568

564569
self._task = self.pdo_node.network.send_periodic(
@@ -579,7 +584,7 @@ def remote_request(self) -> None:
579584
"""Send a remote request for the transmit PDO.
580585
Silently ignore if not allowed.
581586
"""
582-
if self.enabled and self.rtr_allowed:
587+
if self.enabled and self.rtr_allowed and self.cob_id is not None:
583588
self.pdo_node.network.send_message(self.cob_id, bytes(), remote=True)
584589

585590
def wait_for_reception(self, timeout: float = 10) -> float:

0 commit comments

Comments
 (0)