Skip to content

Proposed fix for #436 #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from canopen.objectdictionary.datatypes import *
from canopen.objectdictionary.datatypes_24bit import Integer24, Unsigned24
from canopen.utils import pretty_index

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -389,6 +390,13 @@ def decode_raw(self, data: bytes) -> Union[int, float, str, bytes, bytearray]:
# Is this correct?
return data.rstrip(b"\x00").decode("utf_16_le", errors="ignore")
elif self.data_type in self.STRUCT_TYPES:
size = self.STRUCT_TYPES[self.data_type].size
if len(data) > size:
logger.warning("Excessive data in %s. Data type 0x%X expects %s bytes, got %s",
pretty_index(self.index, self.subindex), self.data_type,
size, len(data))
# Truncate the data to the expected size
data = data[:size]
try:
value, = self.STRUCT_TYPES[self.data_type].unpack(data)
return value
Expand Down
18 changes: 1 addition & 17 deletions canopen/sdo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,7 @@ def upload(self, index: int, subindex: int) -> bytes:
When node responds with an error.
"""
with self.open(index, subindex, buffering=0) as fp:
response_size = fp.size
data = fp.read()

# If size is available through variable in OD, then use the smaller of the two sizes.
# Some devices send U32/I32 even if variable is smaller in OD
var = self.od.get_variable(index, subindex)
if var is not None:
# Found a matching variable in OD
# If this is a data type (string, domain etc) the size is
# unknown anyway so keep the data as is
if var.data_type not in objectdictionary.DATA_TYPES:
# Get the size in bytes for this variable
var_size = len(var) // 8
if response_size is None or var_size < response_size:
# Truncate the data to specified size
data = data[0:var_size]
return data
return fp.read()

def download(
self,
Expand Down
Loading