Skip to content
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

Object Dictionary module | UNSIGNED48 | CANOpen Lift 417 IO-Port EDS #437

Closed
joe0799 opened this issue May 19, 2024 · 2 comments · Fixed by #440
Closed

Object Dictionary module | UNSIGNED48 | CANOpen Lift 417 IO-Port EDS #437

joe0799 opened this issue May 19, 2024 · 2 comments · Fixed by #440

Comments

@joe0799
Copy link

joe0799 commented May 19, 2024

Please add UNSIGNED48

File : ~/.local/lib/python3.11/site-packages/canopen/objectdictionary/init.py

TOP Part

UNSIGNED48 = 0x19  # Assuming 0x19 is the identifier for UNSIGNED48

def unpack_unsigned48(data):
    """Unpack 48-bit unsigned integer from bytes."""
    assert len(data) == 6, "UNSIGNED48 data should be exactly 6 bytes"
    return int.from_bytes(data, byteorder='little', signed=False)

def pack_unsigned48(value):
    """Pack 48-bit unsigned integer into bytes."""
    assert 0 <= value < (1 << 48), "Value out of range for UNSIGNED48"
    return value.to_bytes(6, byteorder='little')

Decode-Part:

  def decode_raw(self, data: bytes) -> Union[int, float, str, bytes, bytearray]:
       if self.data_type == UNSIGNED48:
           return unpack_unsigned48(data)
       elif self.data_type == VISIBLE_STRING:
           return data.rstrip(b"\x00").decode("ascii", errors="ignore")
       elif self.data_type == UNICODE_STRING:
           # Is this correct?
           return data.rstrip(b"\x00").decode("utf_16_le", errors="ignore")
       elif self.data_type in self.STRUCT_TYPES:
           try:
               value, = self.STRUCT_TYPES[self.data_type].unpack(data)
               return value
           except struct.error:
               raise ObjectDictionaryError(
                   "Mismatch between expected and actual data size")
       else:
           # Just return the data as is
           return data

`
Encode Part

  def encode_raw(self, value: Union[int, float, str, bytes, bytearray]) -> bytes:
       if isinstance(value, (bytes, bytearray)):
           return value

       elif self.data_type == UNSIGNED48:
           return pack_unsigned48(value)


       elif self.data_type == VISIBLE_STRING:
           return value.encode("ascii")
       elif self.data_type == UNICODE_STRING:
           # Is this correct?
           return value.encode("utf_16_le")
       elif self.data_type in self.STRUCT_TYPES:
           if self.data_type in INTEGER_TYPES:
               value = int(value)
           if self.data_type in NUMBER_TYPES:
               if self.min is not None and value < self.min:
                   logger.warning(
                       "Value %d is less than min value %d", value, self.min)
               if self.max is not None and value > self.max:
                   logger.warning(
                       "Value %d is greater than max value %d",
                       value,
                       self.max)
           try:
               return self.STRUCT_TYPES[self.data_type].pack(value)
           except struct.error:
               raise ValueError("Value does not fit in specified type")
       elif self.data_type is None:
           raise ObjectDictionaryError("Data type has not been specified")
       else:
           raise TypeError(
               "Do not know how to encode %r to data type %Xh" % (
                   value, self.data_type))

`

@sveinse
Copy link
Collaborator

sveinse commented May 19, 2024

Related to #436 - and I am currently working on adding the missing datatypes to canopen

@joe0799
Copy link
Author

joe0799 commented May 19, 2024

Related to #436 - and I am currently working on adding the missing datatypes to canopen

UNSIGNED 48 is integrated with the code snippet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants