Skip to content

Commit ac0d5fd

Browse files
Merge pull request #240 from jnu144/REQUEST_CRC_SUPPORT
2 parents 9ae9dab + 79ce452 commit ac0d5fd

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

canopen/sdo/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def set_data(self, data):
112112
self.sdo_node.download(self.od.index, self.od.subindex, data, force_segment)
113113

114114
def open(self, mode="rb", encoding="ascii", buffering=1024, size=None,
115-
block_transfer=False):
115+
block_transfer=False, request_crc_support=True):
116116
"""Open the data stream as a file like object.
117117
118118
:param str mode:
@@ -136,9 +136,11 @@ def open(self, mode="rb", encoding="ascii", buffering=1024, size=None,
136136
Size of data to that will be transmitted.
137137
:param bool block_transfer:
138138
If block transfer should be used.
139+
:param bool request_crc_support:
140+
If crc calculation should be requested when using block transfer
139141
140142
:returns:
141143
A file like object.
142144
"""
143145
return self.sdo_node.open(self.od.index, self.od.subindex, mode,
144-
encoding, buffering, size, block_transfer)
146+
encoding, buffering, size, block_transfer, request_crc_support=request_crc_support)

canopen/sdo/client.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def download(self, index, subindex, data, force_segment=False):
156156
fp.close()
157157

158158
def open(self, index, subindex=0, mode="rb", encoding="ascii",
159-
buffering=1024, size=None, block_transfer=False, force_segment=False):
159+
buffering=1024, size=None, block_transfer=False, force_segment=False, request_crc_support=True):
160160
"""Open the data stream as a file like object.
161161
162162
:param int index:
@@ -186,14 +186,16 @@ def open(self, index, subindex=0, mode="rb", encoding="ascii",
186186
If block transfer should be used.
187187
:param bool force_segment:
188188
Force use of segmented download regardless of data size.
189-
189+
:param bool request_crc_support:
190+
If crc calculation should be requested when using block transfer
191+
190192
:returns:
191193
A file like object.
192194
"""
193195
buffer_size = buffering if buffering > 1 else io.DEFAULT_BUFFER_SIZE
194196
if "r" in mode:
195197
if block_transfer:
196-
raw_stream = BlockUploadStream(self, index, subindex)
198+
raw_stream = BlockUploadStream(self, index, subindex, request_crc_support=request_crc_support)
197199
else:
198200
raw_stream = ReadableStream(self, index, subindex)
199201
if buffering:
@@ -202,7 +204,7 @@ def open(self, index, subindex=0, mode="rb", encoding="ascii",
202204
return raw_stream
203205
if "w" in mode:
204206
if block_transfer:
205-
raw_stream = BlockDownloadStream(self, index, subindex, size)
207+
raw_stream = BlockDownloadStream(self, index, subindex, size, request_crc_support=request_crc_support)
206208
else:
207209
raw_stream = WritableStream(self, index, subindex, size, force_segment)
208210
if buffering:
@@ -447,14 +449,16 @@ class BlockUploadStream(io.RawIOBase):
447449

448450
crc_supported = False
449451

450-
def __init__(self, sdo_client, index, subindex=0):
452+
def __init__(self, sdo_client, index, subindex=0, request_crc_support=True):
451453
"""
452454
:param canopen.sdo.SdoClient sdo_client:
453455
The SDO client to use for reading.
454456
:param int index:
455457
Object dictionary index to read from.
456458
:param int subindex:
457459
Object dictionary sub-index to read from.
460+
:param bool request_crc_support:
461+
If crc calculation should be requested when using block transfer
458462
"""
459463
self._done = False
460464
self.sdo_client = sdo_client
@@ -467,7 +471,9 @@ def __init__(self, sdo_client, index, subindex=0):
467471
sdo_client.rx_cobid - 0x600)
468472
# Initiate Block Upload
469473
request = bytearray(8)
470-
command = REQUEST_BLOCK_UPLOAD | INITIATE_BLOCK_TRANSFER | CRC_SUPPORTED
474+
command = REQUEST_BLOCK_UPLOAD | INITIATE_BLOCK_TRANSFER
475+
if request_crc_support:
476+
command |= CRC_SUPPORTED
471477
struct.pack_into("<BHBBB", request, 0,
472478
command, index, subindex, self.blksize, 0)
473479
response = sdo_client.request_response(request)
@@ -597,7 +603,7 @@ def readable(self):
597603
class BlockDownloadStream(io.RawIOBase):
598604
"""File like object for block download."""
599605

600-
def __init__(self, sdo_client, index, subindex=0, size=None):
606+
def __init__(self, sdo_client, index, subindex=0, size=None, request_crc_support=True):
601607
"""
602608
:param canopen.sdo.SdoClient sdo_client:
603609
The SDO client to use for communication.
@@ -607,6 +613,8 @@ def __init__(self, sdo_client, index, subindex=0, size=None):
607613
Object dictionary sub-index to read from.
608614
:param int size:
609615
Size of data in number of bytes if known in advance.
616+
:param bool request_crc_support:
617+
If crc calculation should be requested when using block transfer
610618
"""
611619
self.sdo_client = sdo_client
612620
self.size = size
@@ -615,7 +623,9 @@ def __init__(self, sdo_client, index, subindex=0, size=None):
615623
self._seqno = 0
616624
self._crc = sdo_client.crc_cls()
617625
self._last_bytes_sent = 0
618-
command = REQUEST_BLOCK_DOWNLOAD | INITIATE_BLOCK_TRANSFER | CRC_SUPPORTED
626+
command = REQUEST_BLOCK_DOWNLOAD | INITIATE_BLOCK_TRANSFER
627+
if request_crc_support:
628+
command |= CRC_SUPPORTED
619629
request = bytearray(8)
620630
logger.info("Initiating block download for 0x%X:%d", index, subindex)
621631
if size is not None:

0 commit comments

Comments
 (0)