Skip to content

Commit 279f700

Browse files
committed
Add support for unsigned24 and integer24
Unsigned24 was inspired by 659899d, but kept a little simpler. I also added the signed version. Signed-off-by: Greter Raffael <[email protected]>
1 parent ba4fa0c commit 279f700

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

canopen/objectdictionary/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import logging
1111

1212
from canopen.objectdictionary.datatypes import *
13+
from canopen.objectdictionary.datatypes_24bit import Integer24, Unsigned24
1314

1415
logger = logging.getLogger(__name__)
1516

@@ -277,10 +278,12 @@ class ODVariable:
277278
BOOLEAN: struct.Struct("?"),
278279
INTEGER8: struct.Struct("b"),
279280
INTEGER16: struct.Struct("<h"),
281+
INTEGER24: Integer24(),
280282
INTEGER32: struct.Struct("<l"),
281283
INTEGER64: struct.Struct("<q"),
282284
UNSIGNED8: struct.Struct("B"),
283285
UNSIGNED16: struct.Struct("<H"),
286+
UNSIGNED24: Unsigned24(),
284287
UNSIGNED32: struct.Struct("<L"),
285288
UNSIGNED64: struct.Struct("<Q"),
286289
REAL32: struct.Struct("<f"),

canopen/objectdictionary/datatypes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
OCTET_STRING = 0xA
1212
UNICODE_STRING = 0xB
1313
DOMAIN = 0xF
14+
INTEGER24 = 0x10
1415
REAL64 = 0x11
1516
INTEGER64 = 0x15
17+
UNSIGNED24 = 0x16
1618
UNSIGNED64 = 0x1B
1719

18-
SIGNED_TYPES = (INTEGER8, INTEGER16, INTEGER32, INTEGER64)
19-
UNSIGNED_TYPES = (UNSIGNED8, UNSIGNED16, UNSIGNED32, UNSIGNED64)
20+
SIGNED_TYPES = (INTEGER8, INTEGER16, INTEGER24, INTEGER32, INTEGER64)
21+
UNSIGNED_TYPES = (UNSIGNED8, UNSIGNED16, UNSIGNED24, UNSIGNED32, UNSIGNED64)
2022
INTEGER_TYPES = SIGNED_TYPES + UNSIGNED_TYPES
2123
FLOAT_TYPES = (REAL32, REAL64)
2224
NUMBER_TYPES = INTEGER_TYPES + FLOAT_TYPES
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import struct
2+
3+
4+
class Unsigned24:
5+
def __init__(self):
6+
self.__st = struct.Struct("<L")
7+
8+
def unpack(self, __buffer):
9+
return self.__st.unpack(__buffer + b'\x00')
10+
11+
def pack(self, *v):
12+
return self.__st.pack(*v)[:3]
13+
14+
@property
15+
def size(self):
16+
return 3
17+
18+
19+
class Integer24:
20+
def __init__(self):
21+
self.__st = struct.Struct("<l")
22+
23+
def unpack(self, __buffer):
24+
mask = 0x80
25+
neg = (__buffer[2] & mask) > 0
26+
return self.__st.unpack(__buffer + (b'\xff' if neg else b'\x00'))
27+
28+
def pack(self, *v):
29+
return self.__st.pack(*v)[:3]
30+
31+
@property
32+
def size(self):
33+
return 3

0 commit comments

Comments
 (0)