diff --git a/canopen/pdo/base.py b/canopen/pdo/base.py index 7bef6e62..c4104143 100644 --- a/canopen/pdo/base.py +++ b/canopen/pdo/base.py @@ -39,18 +39,22 @@ def __init__(self, node: Union[LocalNode, RemoteNode]): def __iter__(self): return iter(self.map) - def __getitem__(self, key): - if isinstance(key, int) and (0x1A00 <= key <= 0x1BFF or # By TPDO ID (512) - 0x1600 <= key <= 0x17FF or # By RPDO ID (512) - 0 < key <= 512): # By PDO Index - return self.map[key] - else: - for pdo_map in self.map.values(): - try: - return pdo_map[key] - except KeyError: - # ignore if one specific PDO does not have the key and try the next one - continue + def __getitem__(self, key: Union[int, str]): + if isinstance(key, int): + if key == 0: + raise KeyError("PDO index zero requested for 1-based sequence") + if ( + 0 < key <= 512 # By PDO Index + or 0x1600 <= key <= 0x17FF # By RPDO ID (512) + or 0x1A00 <= key <= 0x1BFF # By TPDO ID (512) + ): + return self.map[key] + for pdo_map in self.map.values(): + try: + return pdo_map[key] + except KeyError: + # ignore if one specific PDO does not have the key and try the next one + continue raise KeyError(f"PDO: {key} was not found in any map") def __len__(self):