Skip to content

Commit cd89d30

Browse files
Various minor SDO modifications
Add read() and write() methods to SDO variables Add some more debug output when doing SDO operations Reduce SDO timeout and retries
1 parent 266328f commit cd89d30

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

canopen/sdo.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class SdoClient(collections.Mapping):
3434
"""Handles communication with an SDO server."""
3535

3636
#: Max time in seconds to wait for response from server
37-
RESPONSE_TIMEOUT = 0.5
37+
RESPONSE_TIMEOUT = 0.3
3838

3939
#: Max number of request retries before raising error
40-
MAX_RETRIES = 5
40+
MAX_RETRIES = 3
4141

4242
def __init__(self, node_id, od):
4343
#: Node ID
@@ -222,6 +222,45 @@ def set_data(self, data):
222222
force_segment = self.od.data_type == objectdictionary.DOMAIN
223223
self.sdo_node.download(self.od.index, self.od.subindex, data, force_segment)
224224

225+
def read(self, fmt="raw"):
226+
"""Alternative way of reading using a function instead of attributes.
227+
228+
May be useful for asynchronous reading.
229+
230+
:param str fmt:
231+
How to return the value
232+
- 'raw'
233+
- 'phys'
234+
- 'desc'
235+
236+
:returns:
237+
The value of the variable.
238+
"""
239+
if fmt == "raw":
240+
return self.raw
241+
elif fmt == "phys":
242+
return self.phys
243+
elif fmt == "desc":
244+
return self.desc
245+
246+
def write(self, value, fmt="raw"):
247+
"""Alternative way of writing using a function instead of attributes.
248+
249+
May be useful for asynchronous writing.
250+
251+
:param str fmt:
252+
How to write the value
253+
- 'raw'
254+
- 'phys'
255+
- 'desc'
256+
"""
257+
if fmt == "raw":
258+
self.raw = value
259+
elif fmt == "phys":
260+
self.phys = value
261+
elif fmt == "desc":
262+
self.desc = value
263+
225264
def open(self, mode="rb", encoding="ascii", buffering=112):
226265
"""Open the data stream as a file like object.
227266
@@ -286,11 +325,12 @@ def __init__(self, sdo_client, index, subindex=0):
286325
self.sdo_client = sdo_client
287326
self.command = REQUEST_SEGMENT_UPLOAD
288327

328+
logger.debug("Reading 0x%X:%d from node %d", index, subindex, sdo_client.id)
289329
request = SDO_STRUCT.pack(REQUEST_UPLOAD, index, subindex)
290330
request += b"\x00\x00\x00\x00"
291331
response = sdo_client.send_request(request)
292332
res_command, res_index, res_subindex = SDO_STRUCT.unpack(response[0:4])
293-
res_data = response[4:]
333+
res_data = response[4:8]
294334

295335
if res_command & 0xE0 != RESPONSE_UPLOAD:
296336
raise SdoCommunicationError("Unexpected response 0x%02X" % res_command)
@@ -312,6 +352,9 @@ def __init__(self, sdo_client, index, subindex=0):
312352
self.exp_data = res_data[:self.size]
313353
elif res_command & SIZE_SPECIFIED:
314354
self.size, = struct.unpack("<L", res_data)
355+
logger.debug("Using segmented transfer of %d bytes", self.size)
356+
else:
357+
logger.debug("Using segmented transfer")
315358

316359
def read(self, size=-1):
317360
"""Read one segment which may be up to 7 bytes.

0 commit comments

Comments
 (0)