From 279f700b6878e42c1fb17d781379388820085af7 Mon Sep 17 00:00:00 2001 From: Greter Raffael Date: Tue, 21 Nov 2023 13:53:36 +0100 Subject: [PATCH 1/2] Add support for unsigned24 and integer24 Unsigned24 was inspired by https://github.com/christiansandberg/canopen/pull/267/commits/659899de5fece198852581e7d9473478e766e9c1, but kept a little simpler. I also added the signed version. Signed-off-by: Greter Raffael --- canopen/objectdictionary/__init__.py | 3 ++ canopen/objectdictionary/datatypes.py | 6 ++-- canopen/objectdictionary/datatypes_24bit.py | 33 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 canopen/objectdictionary/datatypes_24bit.py 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 From 5c22932da397c6f62701738ab694be368c568923 Mon Sep 17 00:00:00 2001 From: Greter Raffael Date: Tue, 21 Nov 2023 14:42:29 +0100 Subject: [PATCH 2/2] Extend tests to 24bit types Signed-off-by: Greter Raffael --- test/test_eds.py | 7 +++++++ test/test_od.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+) 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