From d6498dc1e3fad7f62ab5ad2b2777a1227da3f62c Mon Sep 17 00:00:00 2001 From: Yash Chaudhary Date: Thu, 16 Apr 2026 18:20:29 +0530 Subject: [PATCH 1/2] pdo: handle None cob_id gracefully in __repr__ and network calls --- canopen/pdo/base.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/canopen/pdo/base.py b/canopen/pdo/base.py index 771ed5af..5bc78ab8 100644 --- a/canopen/pdo/base.py +++ b/canopen/pdo/base.py @@ -229,7 +229,8 @@ def __init__(self, pdo_node, com_record, map_array): self._task = None def __repr__(self) -> str: - return f"<{type(self).__qualname__} {self.name!r} at COB-ID 0x{self.cob_id:X}>" + cob = f"0x{self.cob_id:X}" if self.cob_id is not None else "Unassigned" + return f"<{type(self).__qualname__} {self.name!r} at COB-ID {cob}>" def __getitem_by_index(self, value): valid_values = [] @@ -492,7 +493,7 @@ def subscribe(self) -> None: associated with read() or save(), if the local PDO setup is known to match what's stored on the node. """ - if self.enabled: + if self.enabled and self.cob_id is not None: logger.info("Subscribing to enabled PDO 0x%X on the network", self.cob_id) self.pdo_node.network.subscribe(self.cob_id, self.on_message) @@ -540,6 +541,8 @@ def add_variable( def transmit(self) -> None: """Transmit the message once.""" + if self.cob_id is None: + raise ValueError("A valid COB-ID has not been configured") self.pdo_node.network.send_message(self.cob_id, self.data) def start(self, period: Optional[float] = None) -> None: @@ -559,6 +562,8 @@ def start(self, period: Optional[float] = None) -> None: if not self.period: raise ValueError("A valid transmission period has not been given") + if self.cob_id is None: + raise ValueError("A valid COB-ID has not been configured") logger.info("Starting %s with a period of %s seconds", self.name, self.period) self._task = self.pdo_node.network.send_periodic( @@ -579,7 +584,7 @@ def remote_request(self) -> None: """Send a remote request for the transmit PDO. Silently ignore if not allowed. """ - if self.enabled and self.rtr_allowed: + if self.enabled and self.rtr_allowed and self.cob_id is not None: self.pdo_node.network.send_message(self.cob_id, bytes(), remote=True) def wait_for_reception(self, timeout: float = 10) -> float: From 17f169b2ed0c536eb57ad6ce507b11c56ea62e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Thu, 23 Apr 2026 22:53:12 +0200 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Colomb --- canopen/pdo/base.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/canopen/pdo/base.py b/canopen/pdo/base.py index 5bc78ab8..f433c91e 100644 --- a/canopen/pdo/base.py +++ b/canopen/pdo/base.py @@ -229,7 +229,7 @@ def __init__(self, pdo_node, com_record, map_array): self._task = None def __repr__(self) -> str: - cob = f"0x{self.cob_id:X}" if self.cob_id is not None else "Unassigned" + cob = f"0x{self.cob_id:X}" if self.cob_id else "Unassigned" return f"<{type(self).__qualname__} {self.name!r} at COB-ID {cob}>" def __getitem_by_index(self, value): @@ -493,7 +493,7 @@ def subscribe(self) -> None: associated with read() or save(), if the local PDO setup is known to match what's stored on the node. """ - if self.enabled and self.cob_id is not None: + if self.enabled and self.cob_id: logger.info("Subscribing to enabled PDO 0x%X on the network", self.cob_id) self.pdo_node.network.subscribe(self.cob_id, self.on_message) @@ -541,7 +541,7 @@ def add_variable( def transmit(self) -> None: """Transmit the message once.""" - if self.cob_id is None: + if not self.cob_id: raise ValueError("A valid COB-ID has not been configured") self.pdo_node.network.send_message(self.cob_id, self.data) @@ -562,7 +562,7 @@ def start(self, period: Optional[float] = None) -> None: if not self.period: raise ValueError("A valid transmission period has not been given") - if self.cob_id is None: + if not self.cob_id: raise ValueError("A valid COB-ID has not been configured") logger.info("Starting %s with a period of %s seconds", self.name, self.period) @@ -584,7 +584,7 @@ def remote_request(self) -> None: """Send a remote request for the transmit PDO. Silently ignore if not allowed. """ - if self.enabled and self.rtr_allowed and self.cob_id is not None: + if self.enabled and self.rtr_allowed and self.cob_id: self.pdo_node.network.send_message(self.cob_id, bytes(), remote=True) def wait_for_reception(self, timeout: float = 10) -> float: