Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions canopen/sdo/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import binascii
try:
from collections.abc import Mapping
except ImportError:
Expand All @@ -7,8 +8,24 @@
from .. import variable


class CrcXmodem(object):
"""Mimics CrcXmodem 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 = CrcXmodem

def __init__(self, rx_cobid, tx_cobid, od):
"""
:param int rx_cobid:
Expand Down
13 changes: 6 additions & 7 deletions canopen/sdo/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import struct
import logging
import io
import binascii
import time
try:
import queue
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -738,7 +737,7 @@ def close(self):
request[0] = command
if self.crc_supported:
# Add CRC
struct.pack_into("<H", request, 1, self._crc)
struct.pack_into("<H", request, 1, self._crc.final())
logger.debug("Ending block transfer...")
response = self.sdo_client.request_response(request)
res_command, = struct.unpack_from("B", response)
Expand Down