From f02444ba3d1f0c9dcdba659409e803ba29a88769 Mon Sep 17 00:00:00 2001 From: Christian Sandberg Date: Mon, 3 Jun 2019 13:34:57 +0200 Subject: [PATCH 1/2] Allow to override CRC algorithm for block transfer --- canopen/sdo/base.py | 17 +++++++++++++++++ canopen/sdo/client.py | 13 ++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/canopen/sdo/base.py b/canopen/sdo/base.py index 0b4a5e97..506d4218 100644 --- a/canopen/sdo/base.py +++ b/canopen/sdo/base.py @@ -1,3 +1,4 @@ +import binascii try: from collections.abc import Mapping except ImportError: @@ -7,8 +8,24 @@ from .. import variable +class Crc16Ccitt(object): + """Mimics Crc16Ccitt from crccheck.""" + + def __init__(self): + self._value = 0 + + def process(self, data): + self._value = binascii.crc_hqx(data, self._value) + + def final(self): + return self._value + + class SdoBase(Mapping): + #: The CRC algorithm used for block transfers + crc_cls = Crc16Ccitt + def __init__(self, rx_cobid, tx_cobid, od): """ :param int rx_cobid: diff --git a/canopen/sdo/client.py b/canopen/sdo/client.py index b9ecc6e8..05a0276d 100644 --- a/canopen/sdo/client.py +++ b/canopen/sdo/client.py @@ -1,7 +1,6 @@ import struct import logging import io -import binascii import time try: import queue @@ -458,7 +457,7 @@ def __init__(self, sdo_client, index, subindex=0): self._done = False self.sdo_client = sdo_client self.pos = 0 - self._crc = 0 + self._crc = sdo_client.crc_cls() self._server_crc = None self._ackseq = 0 @@ -523,9 +522,9 @@ def read(self, size=-1): else: data = response[1:8] if self.crc_supported: - self._crc = binascii.crc_hqx(data, self._crc) + self._crc.process(data) if self._done: - if self._server_crc != self._crc: + if self._server_crc != self._crc.final(): self.sdo_client.abort(0x05040004) raise SdoCommunicationError("CRC is not OK") logger.info("CRC is OK") @@ -612,7 +611,7 @@ def __init__(self, sdo_client, index, subindex=0, size=None): self.pos = 0 self._done = False self._seqno = 0 - self._crc = 0 + self._crc = sdo_client.crc_cls() self._last_bytes_sent = 0 command = REQUEST_BLOCK_DOWNLOAD | INITIATE_BLOCK_TRANSFER | CRC_SUPPORTED request = bytearray(8) @@ -694,7 +693,7 @@ def send(self, b, end=False): self.pos += len(b) if self.crc_supported: # Calculate CRC - self._crc = binascii.crc_hqx(b, self._crc) + self._crc.process(b) if self._seqno >= self._blksize: # End of this block, wait for ACK self._block_ack() @@ -738,7 +737,7 @@ def close(self): request[0] = command if self.crc_supported: # Add CRC - struct.pack_into(" Date: Mon, 3 Jun 2019 13:40:27 +0200 Subject: [PATCH 2/2] Corrected name to CrcXmodem --- canopen/sdo/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/canopen/sdo/base.py b/canopen/sdo/base.py index 506d4218..8f1fcb18 100644 --- a/canopen/sdo/base.py +++ b/canopen/sdo/base.py @@ -8,8 +8,8 @@ from .. import variable -class Crc16Ccitt(object): - """Mimics Crc16Ccitt from crccheck.""" +class CrcXmodem(object): + """Mimics CrcXmodem from crccheck.""" def __init__(self): self._value = 0 @@ -24,7 +24,7 @@ def final(self): class SdoBase(Mapping): #: The CRC algorithm used for block transfers - crc_cls = Crc16Ccitt + crc_cls = CrcXmodem def __init__(self, rx_cobid, tx_cobid, od): """