Skip to content

24 bit support #406

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

Merged
merged 2 commits into from
Dec 4, 2023
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
3 changes: 3 additions & 0 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging

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

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -277,10 +278,12 @@ class ODVariable:
BOOLEAN: struct.Struct("?"),
INTEGER8: struct.Struct("b"),
INTEGER16: struct.Struct("<h"),
INTEGER24: Integer24(),
INTEGER32: struct.Struct("<l"),
INTEGER64: struct.Struct("<q"),
UNSIGNED8: struct.Struct("B"),
UNSIGNED16: struct.Struct("<H"),
UNSIGNED24: Unsigned24(),
UNSIGNED32: struct.Struct("<L"),
UNSIGNED64: struct.Struct("<Q"),
REAL32: struct.Struct("<f"),
Expand Down
6 changes: 4 additions & 2 deletions canopen/objectdictionary/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
OCTET_STRING = 0xA
UNICODE_STRING = 0xB
DOMAIN = 0xF
INTEGER24 = 0x10
REAL64 = 0x11
INTEGER64 = 0x15
UNSIGNED24 = 0x16
UNSIGNED64 = 0x1B

SIGNED_TYPES = (INTEGER8, INTEGER16, INTEGER32, INTEGER64)
UNSIGNED_TYPES = (UNSIGNED8, UNSIGNED16, UNSIGNED32, UNSIGNED64)
SIGNED_TYPES = (INTEGER8, INTEGER16, INTEGER24, INTEGER32, INTEGER64)
UNSIGNED_TYPES = (UNSIGNED8, UNSIGNED16, UNSIGNED24, UNSIGNED32, UNSIGNED64)
INTEGER_TYPES = SIGNED_TYPES + UNSIGNED_TYPES
FLOAT_TYPES = (REAL32, REAL64)
NUMBER_TYPES = INTEGER_TYPES + FLOAT_TYPES
Expand Down
33 changes: 33 additions & 0 deletions canopen/objectdictionary/datatypes_24bit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import struct


class Unsigned24:
def __init__(self):
self.__st = struct.Struct("<L")

def unpack(self, __buffer):
return self.__st.unpack(__buffer + b'\x00')

def pack(self, *v):
return self.__st.pack(*v)[:3]

@property
def size(self):
return 3


class Integer24:
def __init__(self):
self.__st = struct.Struct("<l")

def unpack(self, __buffer):
mask = 0x80
neg = (__buffer[2] & mask) > 0
return self.__st.unpack(__buffer + (b'\xff' if neg else b'\x00'))

def pack(self, *v):
return self.__st.pack(*v)[:3]

@property
def size(self):
return 3
7 changes: 7 additions & 0 deletions test/test_eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class TestEDS(unittest.TestCase):
{"hex_str": "0000", "bit_length": 16, "expected": 0},
{"hex_str": "0001", "bit_length": 16, "expected": 1}
],
"int24": [
{"hex_str": "7FFFFF", "bit_length": 24, "expected": 8388607},
{"hex_str": "800000", "bit_length": 24, "expected": -8388608},
{"hex_str": "FFFFFF", "bit_length": 24, "expected": -1},
{"hex_str": "000000", "bit_length": 24, "expected": 0},
{"hex_str": "000001", "bit_length": 24, "expected": 1}
],
"int32": [
{"hex_str": "7FFFFFFF", "bit_length": 32, "expected": 2147483647},
{"hex_str": "80000000", "bit_length": 32, "expected": -2147483648},
Expand Down
14 changes: 14 additions & 0 deletions test/test_od.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_unsigned16(self):
self.assertEqual(var.decode_raw(b"\xfe\xff"), 65534)
self.assertEqual(var.encode_raw(65534), b"\xfe\xff")

def test_unsigned24(self):
var = od.ODVariable("Test UNSIGNED24", 0x1000)
var.data_type = od.UNSIGNED24
self.assertEqual(var.decode_raw(b"\xfd\xfe\xff"), 16776957)
self.assertEqual(var.encode_raw(16776957), b"\xfd\xfe\xff")

def test_unsigned32(self):
var = od.ODVariable("Test UNSIGNED32", 0x1000)
var.data_type = od.UNSIGNED32
Expand All @@ -46,6 +52,14 @@ def test_integer16(self):
self.assertEqual(var.encode_raw(-2), b"\xfe\xff")
self.assertEqual(var.encode_raw(1), b"\x01\x00")

def test_integer24(self):
var = od.ODVariable("Test INTEGER24", 0x1000)
var.data_type = od.INTEGER24
self.assertEqual(var.decode_raw(b"\xfe\xff\xff"), -2)
self.assertEqual(var.decode_raw(b"\x01\x00\x00"), 1)
self.assertEqual(var.encode_raw(-2), b"\xfe\xff\xff")
self.assertEqual(var.encode_raw(1), b"\x01\x00\x00")

def test_integer32(self):
var = od.ODVariable("Test INTEGER32", 0x1000)
var.data_type = od.INTEGER32
Expand Down