Skip to content

Commit 41e028d

Browse files
committed
Minor improvements
* Add support for using dot (.) to address sub-index elements in the OD * Make SdoVariable awaitable for fetching * Fix SdoClient.aabort() that were missing
1 parent abbc2dc commit 41e028d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

canopen/objectdictionary/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def __getitem__(
108108
"""Get object from object dictionary by name or index."""
109109
item = self.names.get(index) or self.indices.get(index)
110110
if item is None:
111+
if isinstance(index, str) and '.' in index:
112+
parts = index.split('.')
113+
return self[parts[0]][".".join(parts[1:])]
111114
name = "0x%X" % index if isinstance(index, int) else index
112115
raise KeyError("%s was not found in Object Dictionary" % name)
113116
return item

canopen/sdo/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ def __init__(self, sdo_node: SdoBase, od: ObjectDictionary):
168168
self.sdo_node = sdo_node
169169
variable.Variable.__init__(self, od)
170170

171+
def __await__(self):
172+
return self.aget_raw().__await__()
173+
171174
def get_data(self) -> bytes:
172175
return self.sdo_node.upload(self.od.index, self.od.subindex)
173176

canopen/sdo/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ async def arequest_response(self, sdo_request):
151151
except SdoCommunicationError as e:
152152
retries_left -= 1
153153
if not retries_left:
154-
self.abort(0x5040000)
154+
await self.aabort(0x5040000)
155155
raise
156156
logger.warning(str(e))
157157

@@ -164,6 +164,15 @@ def abort(self, abort_code=0x08000000):
164164
self.send_request(request)
165165
logger.error("Transfer aborted by client with code 0x{:08X}".format(abort_code))
166166

167+
async def aabort(self, abort_code=0x08000000):
168+
"""Abort current transfer."""
169+
request = bytearray(8)
170+
request[0] = REQUEST_ABORTED
171+
# TODO: Is it necessary to include index and subindex?
172+
struct.pack_into("<L", request, 4, abort_code)
173+
await self.asend_request(request)
174+
logger.error("Transfer aborted by client with code 0x{:08X}".format(abort_code))
175+
167176
@ensure_not_async # NOTE: Safeguard for accidental async use
168177
def upload(self, index: int, subindex: int) -> bytes:
169178
"""May be called to make a read operation without an Object Dictionary.

0 commit comments

Comments
 (0)