diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index 066a069c..6cc762b6 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -10,6 +10,7 @@ import logging from canopen.objectdictionary.datatypes import * +from canopen.objectdictionary.datatypes_24bit import Integer24, Unsigned24 logger = logging.getLogger(__name__) @@ -277,10 +278,12 @@ class ODVariable: BOOLEAN: struct.Struct("?"), INTEGER8: struct.Struct("b"), INTEGER16: struct.Struct(" 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 diff --git a/test/test_eds.py b/test/test_eds.py index a3923709..a35cbb99 100644 --- a/test/test_eds.py +++ b/test/test_eds.py @@ -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}, diff --git a/test/test_od.py b/test/test_od.py index fe90dc13..a5f00985 100644 --- a/test/test_od.py +++ b/test/test_od.py @@ -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 @@ -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