Skip to content

Commit d4d7fe2

Browse files
committed
Support lookup by mapping record index in TPDO and RPDO classes.
These two PdoBase-derived classes only supported numeric access based on the sequential index, not with the mapping parameter record index like the PDO class. Implement a fallback to check that if the regular index lookup fails.
1 parent ed4a4af commit d4d7fe2

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

canopen/pdo/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import binascii
4+
import contextlib
45
import logging
56
import math
67
import threading
@@ -169,7 +170,9 @@ def __init__(self, com_offset: int, map_offset: int, pdo_node: PdoBase, cob_base
169170
self.maps[map_no + 1] = new_map
170171

171172
def __getitem__(self, key: int) -> PdoMap:
172-
return self.maps[key]
173+
with contextlib.suppress(KeyError):
174+
return self.maps[key]
175+
return self.maps[key + 1 - self.map_offset]
173176

174177
def __iter__(self) -> Iterator[int]:
175178
return iter(self.maps)

test/test_pdo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def test_pdo_getitem(self):
5353
by_mapping_record = node.pdo[0x1A00]
5454
self.assertIsInstance(by_mapping_record, canopen.pdo.PdoMap)
5555
self.assertEqual(by_mapping_record['INTEGER16 value'].raw, -3)
56+
self.assertIs(node.tpdo[0x1A00], by_mapping_record)
5657
by_object_name = node.pdo['INTEGER16 value']
5758
self.assertIsInstance(by_object_name, canopen.pdo.PdoVariable)
5859
self.assertIs(by_object_name.od, node.object_dictionary['INTEGER16 value'])

0 commit comments

Comments
 (0)