From dc8a4ca79a45db4c5f56fe0db8f3634202b63ada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Thu, 25 Apr 2024 21:56:52 +0200 Subject: [PATCH] Mirror some OD access API to the SdoBase / SdoVariable classes. (#423) --- canopen/sdo/base.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/canopen/sdo/base.py b/canopen/sdo/base.py index 85cfe4e9..dcd35eb3 100644 --- a/canopen/sdo/base.py +++ b/canopen/sdo/base.py @@ -1,5 +1,5 @@ import binascii -from typing import Iterable, Union +from typing import Iterable, Optional, Union try: from collections.abc import Mapping except ImportError: @@ -67,6 +67,19 @@ def __len__(self) -> int: def __contains__(self, key: Union[int, str]) -> bool: return key in self.od + def get_variable( + self, index: Union[int, str], subindex: int = 0 + ) -> Optional["SdoVariable"]: + """Get the variable object at specified index (and subindex if applicable). + + :return: SdoVariable if found, else `None` + """ + obj = self.get(index) + if isinstance(obj, SdoVariable): + return obj + elif isinstance(obj, (SdoRecord, SdoArray)): + return obj.get(subindex) + def upload(self, index: int, subindex: int) -> bytes: raise NotImplementedError() @@ -132,6 +145,14 @@ def set_data(self, data: bytes): force_segment = self.od.data_type == objectdictionary.DOMAIN self.sdo_node.download(self.od.index, self.od.subindex, data, force_segment) + @property + def writable(self) -> bool: + return self.od.writable + + @property + def readable(self) -> bool: + return self.od.readable + def open(self, mode="rb", encoding="ascii", buffering=1024, size=None, block_transfer=False, request_crc_support=True): """Open the data stream as a file like object.