From 466b1b2582fded41e8c182aafea2254518d7f290 Mon Sep 17 00:00:00 2001 From: Taras Zaporozhets Date: Thu, 11 Sep 2025 18:46:21 +0200 Subject: [PATCH] xdd: Implement option to load object dictionary from XDD file. Signed-off-by: Taras Zaporozhets --- canopen/objectdictionary/__init__.py | 7 +- canopen/objectdictionary/eds.py | 29 +- canopen/objectdictionary/xdd.py | 306 ++++++ canopen/utils.py | 23 +- test/sample.xdd | 1374 ++++++++++++++++++++++++++ test/test_eds.py | 5 +- test/test_xdd.py | 171 ++++ test/util.py | 1 + 8 files changed, 1884 insertions(+), 32 deletions(-) create mode 100644 canopen/objectdictionary/xdd.py create mode 100644 test/sample.xdd create mode 100644 test/test_xdd.py diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index fa694c56..bb646f4b 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -76,7 +76,7 @@ def import_od( source: Union[str, TextIO, None], node_id: Optional[int] = None, ) -> ObjectDictionary: - """Parse an EDS, DCF, or EPF file. + """Parse an EDS, DCF, EPF or XDD file. :param source: The path to object dictionary file, a file like object, or an EPF XML tree. @@ -106,9 +106,12 @@ def import_od( elif suffix == ".epf": from canopen.objectdictionary import epf return epf.import_epf(source) + elif suffix == ".xdd": + from canopen.objectdictionary import xdd + return xdd.import_xdd(source, node_id) else: doc_type = suffix[1:] - allowed = ", ".join(["eds", "dcf", "epf"]) + allowed = ", ".join(["eds", "dcf", "epf", "xdd"]) raise ValueError( f"Cannot import from the {doc_type!r} format; " f"supported formats: {allowed}" diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index 8ab1a349..e20a5681 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -9,6 +9,7 @@ from canopen import objectdictionary from canopen.objectdictionary import ObjectDictionary, datatypes from canopen.sdo import SdoClient +from canopen.utils import signed_int_from_hex, calc_bit_length if TYPE_CHECKING: import canopen.network @@ -201,30 +202,6 @@ def import_from_node(node_id: int, network: canopen.network.Network): network.unsubscribe(0x580 + node_id) return od - -def _calc_bit_length(data_type): - if data_type == datatypes.INTEGER8: - return 8 - elif data_type == datatypes.INTEGER16: - return 16 - elif data_type == datatypes.INTEGER32: - return 32 - elif data_type == datatypes.INTEGER64: - return 64 - else: - raise ValueError(f"Invalid data_type '{data_type}', expecting a signed integer data_type.") - - -def _signed_int_from_hex(hex_str, bit_length): - number = int(hex_str, 0) - max_value = (1 << (bit_length - 1)) - 1 - - if number > max_value: - return number - (1 << bit_length) - else: - return number - - def _convert_variable(node_id, var_type, value): if var_type in (datatypes.OCTET_STRING, datatypes.DOMAIN): return bytes.fromhex(value) @@ -288,7 +265,7 @@ def build_variable(eds, section, node_id, index, subindex=0): try: min_string = eds.get(section, "LowLimit") if var.data_type in datatypes.SIGNED_TYPES: - var.min = _signed_int_from_hex(min_string, _calc_bit_length(var.data_type)) + var.min = signed_int_from_hex(min_string, calc_bit_length(var.data_type)) else: var.min = int(min_string, 0) except ValueError: @@ -297,7 +274,7 @@ def build_variable(eds, section, node_id, index, subindex=0): try: max_string = eds.get(section, "HighLimit") if var.data_type in datatypes.SIGNED_TYPES: - var.max = _signed_int_from_hex(max_string, _calc_bit_length(var.data_type)) + var.max = signed_int_from_hex(max_string, calc_bit_length(var.data_type)) else: var.max = int(max_string, 0) except ValueError: diff --git a/canopen/objectdictionary/xdd.py b/canopen/objectdictionary/xdd.py new file mode 100644 index 00000000..25b1b070 --- /dev/null +++ b/canopen/objectdictionary/xdd.py @@ -0,0 +1,306 @@ +import logging + +import re +import xml.etree.ElementTree as etree +from configparser import NoOptionError +from typing import TYPE_CHECKING + +from canopen import objectdictionary +from canopen.objectdictionary import ObjectDictionary +from canopen.utils import signed_int_from_hex, calc_bit_length + +if TYPE_CHECKING: + import canopen.network + +logger = logging.getLogger(__name__) + +# Object type. Don't confuse with Data type +VAR = 7 +ARR = 8 +RECORD = 9 + + +def import_xdd(xdd, node_id): + od = ObjectDictionary() + if etree.iselement(xdd): + root = xdd + else: + root = etree.parse(xdd).getroot() + + if node_id is None: + device_commissioning = root.find('.//{*}DeviceCommissioning') + if device_commissioning is not None: + if node_id := device_commissioning.get('nodeID', None): + try: + od.node_id = int(node_id, 0) + except (ValueError, TypeError): + pass + else: + od.node_id = node_id + + _add_device_information_to_od(od, root) + _add_object_list_to_od(od, root) + _add_dummy_objects_to_od(od, root) + + + return od + +def _add_device_information_to_od(od, root): + device_identity = root.find('.//{*}DeviceIdentity') + if device_identity is not None: + for src_prop, dst_prop, f in [ + ("vendorName", "vendor_name", lambda val: str(val)), + ("vendorID", "vendor_number", lambda val: int(val, 0)), + ("productName", "product_name", lambda val: str(val)), + ("productID", "product_number", lambda val: int(val, 0)), + ]: + val = device_identity.find(f'{{*}}{src_prop}') + if val is not None and val.text: + try: + setattr(od.device_information, dst_prop, f(val.text)) + except NoOptionError: + pass + + general_features = root.find('.//{*}CANopenGeneralFeatures') + if general_features is not None: + for src_prop, dst_prop, f in [ + ("granularity", "granularity", lambda val: int(val, 0)), + ("nrOfRxPDO", "nr_of_RXPDO", lambda val: int(val, 0)), + ("nrOfTxPDO", "nr_of_TXPDO", lambda val: int(val, 0)), + ("bootUpSlave", "simple_boot_up_slave", lambda val: bool(val)), + ]: + if val := general_features.get(src_prop, None): + try: + setattr(od.device_information, dst_prop, f(val)) + except NoOptionError: + pass + + baud_rate = root.find('.//{*}PhysicalLayer/{*}baudRate') + for baud in baud_rate: + try: + rate = int(baud.get("value").replace(' Kbps', ''), 10) * 1000 + od.device_information.allowed_baudrates.add(rate) + except (ValueError, TypeError): + pass + + if default_baud := baud_rate.get('defaultValue', None): + try: + od.bitrate = int(default_baud.replace(' Kbps', ''), 10) * 1000 + except (ValueError, TypeError): + pass + +def _add_object_list_to_od(od: ObjectDictionary, root): + # Process all CANopen objects in the file + for obj in root.findall('.//{*}CANopenObjectList/{*}CANopenObject'): + name = obj.get('name', '') + index = int(obj.get('index', '0'), 16) + object_type = int(obj.get('objectType', '0')) + sub_number = obj.get('subNumber') + + # Simple variable + if object_type == VAR: + unique_id_ref = obj.get('uniqueIDRef', None) + parameters = root.find(f'.//{{*}}parameter[@uniqueID="{unique_id_ref}"]') + + var = _build_variable(parameters, od.node_id, name, index) + _set_parameters_from_xdd_canopen_object(od.node_id, var, obj) + od.add_object(var) + + # Array + elif object_type == ARR and sub_number: + array = objectdictionary.ODArray(name, index) + for sub_obj in obj: + sub_name = sub_obj.get('name', '') + sub_index = int(sub_obj.get('subIndex'), 16) + sub_unique_id = sub_obj.get('uniqueIDRef', None) + sub_parameters = root.find(f'.//{{*}}parameter[@uniqueID="{sub_unique_id}"]') + + sub_var = _build_variable(sub_parameters, od.node_id, sub_name, index, sub_index) + _set_parameters_from_xdd_canopen_object(od.node_id, sub_var, sub_obj) + array.add_member(sub_var) + od.add_object(array) + + # Record/Struct + elif object_type == RECORD and sub_number: + record = objectdictionary.ODRecord(name, index) + for sub_obj in obj: + sub_name = sub_obj.get('name', '') + sub_index = int(sub_obj.get('subIndex')) + sub_unique_id = sub_obj.get('uniqueIDRef', None) + sub_parameters = root.find(f'.//{{*}}parameter[@uniqueID="{sub_unique_id}"]') + sub_var = _build_variable(sub_parameters, od.node_id, sub_name, index, sub_index) + _set_parameters_from_xdd_canopen_object(od.node_id, sub_var, sub_obj) + record.add_member(sub_var) + od.add_object(record) + +def _add_dummy_objects_to_od(od: ObjectDictionary, root): + dummy_section = root.find('.//{*}ApplicationLayers/{*}dummyUsage') + for dummy in dummy_section: + p = dummy.get('entry').split('=') + key = p[0] + value = int(p[1], 10) + index = int(key.replace('Dummy', ''), 10) + if value == 1: + var = objectdictionary.ODVariable(key, index, 0) + var.data_type = index + var.access_type = "const" + od.add_object(var) + +def _set_parameters_from_xdd_canopen_object(node_id, dst, src): + # PDO mapping of the object, optional, string + # Valid values: + # * no – not mappable + # * default – mapped by default + # * optional – optionally mapped + # * TPDO – may be mapped into TPDO only + # * RPDO – may be mapped into RPDO only + pdo_mapping = src.get('PDOmapping', 'no') + dst.pdo_mappable = pdo_mapping != 'no' + + # Name of the object, optional, string + if var_name := src.get('name', None): + dst.name = var_name + + # CANopen data type (two hex digits), optional + # data_type matches canopen library, no conversion needed + if var_data_type := src.get('dataType', None): + try: + dst.data_type = int(var_data_type, 16) + except (ValueError, TypeError): + pass + + # Access type of the object; valid values, optional, string + # * const – read access only; the value is not changing + # * ro – read access only + # * wo – write access only + # * rw – both read and write access + # strings match with access_type in canopen library, no conversion needed + if access_type := src.get('accessType', None): + dst.access_type = access_type + + # Low limit of the parameter value, optional, string + if min_value := src.get('lowLimit', None): + try: + dst.min = _convert_variable(node_id, dst.data_type, min_value) + except (ValueError, TypeError): + pass + + # High limit of the parameter value, optional, string + if max_value := src.get('highLimit', None): + try: + dst.max = _convert_variable(node_id, dst.data_type, max_value) + except (ValueError, TypeError): + pass + + # Default value of the object, optional, string + if default_value := src.get('defaultValue', None): + try: + dst.default_raw = default_value + if '$NODEID' in dst.default_raw: + dst.relative = True + dst.default = _convert_variable(node_id, dst.data_type, dst.default_raw) + except (ValueError, TypeError): + pass + +def _build_variable(par_tree, node_id, name, index, subindex=0): + var = objectdictionary.ODVariable(name, index, subindex) + # Set default parameters + var.default_raw = None + var.access_type = 'ro' + if par_tree is None: + return + + var.description = par_tree.get('description', '') + + # Extract data type + data_types = { + 'BOOL': objectdictionary.BOOLEAN, + 'SINT': objectdictionary.INTEGER8, + 'INT': objectdictionary.INTEGER16, + 'DINT': objectdictionary.INTEGER32, + 'LINT': objectdictionary.INTEGER64, + 'USINT': objectdictionary.UNSIGNED8, + 'UINT': objectdictionary.UNSIGNED16, + 'UDINT': objectdictionary.UNSIGNED32, + 'ULINT': objectdictionary.UNSIGNED32, + 'REAL': objectdictionary.REAL32, + 'LREAL': objectdictionary.REAL64, + 'STRING': objectdictionary.VISIBLE_STRING, + 'BITSTRING': objectdictionary.DOMAIN, + 'WSTRING': objectdictionary.UNICODE_STRING + } + + #print(f'par_tree={etree.tostring(par_tree, encoding="unicode")}') + for k, v in data_types.items(): + if par_tree.find(f'{{*}}{k}') is not None: + var.data_type = v + + # Extract access type + if access_type_str := par_tree.get('access', None): + # Defines which operations are valid for the parameter: + # * const – read access only; the value is not changing + # * read – read access only (default value) + # * write – write access only + # * readWrite – both read and write access + # * readWriteInput – both read and write access, but represents process input data + # * readWriteOutput – both read and write access, but represents process output data + # * noAccess – access denied + access_types = { + 'const': 'const', + 'read': 'ro', + 'write': 'wo', + 'readWrite': 'rw', + 'readWriteInput': 'rw', + 'readWriteOutput': 'rw', + 'noAccess': 'const', + } + var.access_type = access_types.get(access_type_str) + + # Extract default value + default_value = par_tree.find('{*}defaultValue') + if default_value is not None: + try: + var.default_raw = default_value.get('value') + if '$NODEID' in var.default_raw: + var.relative = True + var.default = _convert_variable(node_id, var.data_type, var.default_raw) + except (ValueError, TypeError): + pass + + # Extract allowed values range + min_value = par_tree.find('{*}allowedValues/{*}range/{*}minValue') + if min_value is not None: + try: + var.min = _convert_variable(node_id, var.data_type, min_value.get('value')) + except (ValueError, TypeError): + pass + + max_value = par_tree.find('{*}allowedValues/{*}range/{*}maxValue') + if max_value is not None: + try: + var.max = _convert_variable(node_id, var.data_type, max_value.get('value')) + except (ValueError, TypeError): + pass + return var + +def _convert_variable(node_id, var_type, value): + if var_type in (objectdictionary.OCTET_STRING, objectdictionary.DOMAIN): + return bytes.fromhex(value) + elif var_type in (objectdictionary.VISIBLE_STRING, objectdictionary.UNICODE_STRING): + return value + elif var_type in objectdictionary.FLOAT_TYPES: + return float(value) + else: + # COB-ID can contain '$NODEID+' so replace this with node_id before converting + value = value.replace(" ", "").upper() + if '$NODEID' in value: + if node_id is None: + logger.warn("Cannot convert value with $NODEID, skipping conversion") + return None + else: + return int(re.sub(r'\+?\$NODEID\+?', '', value), 0) + node_id + else: + if var_type in objectdictionary.SIGNED_TYPES: + return signed_int_from_hex(value, calc_bit_length(var_type)) + else: + return int(value, 0) diff --git a/canopen/utils.py b/canopen/utils.py index 7ddffda3..c18ca63d 100644 --- a/canopen/utils.py +++ b/canopen/utils.py @@ -1,7 +1,7 @@ """Additional utility functions for canopen.""" from typing import Optional, Union - +from canopen.objectdictionary import datatypes def pretty_index(index: Optional[Union[int, str]], sub: Optional[Union[int, str]] = None): @@ -21,3 +21,24 @@ def pretty_index(index: Optional[Union[int, str]], sub_str = f"{sub!r}" return ":".join(s for s in (index_str, sub_str) if s) + +def signed_int_from_hex(hex_str, bit_length): + number = int(hex_str, 0) + max_value = (1 << (bit_length - 1)) - 1 + + if number > max_value: + return number - (1 << bit_length) + else: + return number + +def calc_bit_length(data_type): + if data_type == datatypes.INTEGER8: + return 8 + elif data_type == datatypes.INTEGER16: + return 16 + elif data_type == datatypes.INTEGER32: + return 32 + elif data_type == datatypes.INTEGER64: + return 64 + else: + raise ValueError(f"Invalid data_type '{data_type}', expecting a signed integer data_type.") \ No newline at end of file diff --git a/test/sample.xdd b/test/sample.xdd new file mode 100644 index 00000000..38b6dc58 --- /dev/null +++ b/test/sample.xdd @@ -0,0 +1,1374 @@ + + + + + + + CANopen device profile + 1.1 + + + Device + + 1 + 1 + CANopen + + + + + Vendor Name + 1 + + + 0 + 0 + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CANopen communication network profile + 1.1 + + + CommunicationNetwork + + 1 + 1 + CANopen + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/test_eds.py b/test/test_eds.py index 68f5ad3c..9f6db2b9 100644 --- a/test/test_eds.py +++ b/test/test_eds.py @@ -2,8 +2,7 @@ import unittest import canopen -from canopen.objectdictionary.eds import _signed_int_from_hex -from canopen.utils import pretty_index +from canopen.utils import pretty_index, signed_int_from_hex from .util import DATATYPES_EDS, SAMPLE_EDS, tmp_file @@ -151,7 +150,7 @@ def test_signed_int_from_hex(self): for data_type, test_cases in self.test_data.items(): for test_case in test_cases: with self.subTest(data_type=data_type, test_case=test_case): - result = _signed_int_from_hex('0x' + test_case["hex_str"], test_case["bit_length"]) + result = signed_int_from_hex('0x' + test_case["hex_str"], test_case["bit_length"]) self.assertEqual(result, test_case["expected"]) def test_array_compact_subobj(self): diff --git a/test/test_xdd.py b/test/test_xdd.py new file mode 100644 index 00000000..1161b1ce --- /dev/null +++ b/test/test_xdd.py @@ -0,0 +1,171 @@ +import unittest + +import canopen +from canopen.objectdictionary.eds import signed_int_from_hex +from canopen.utils import pretty_index + +from .util import SAMPLE_XDD + + +class TestXDD(unittest.TestCase): + + test_data = { + "int8": [ + {"hex_str": "7F", "bit_length": 8, "expected": 127}, + {"hex_str": "80", "bit_length": 8, "expected": -128}, + {"hex_str": "FF", "bit_length": 8, "expected": -1}, + {"hex_str": "00", "bit_length": 8, "expected": 0}, + {"hex_str": "01", "bit_length": 8, "expected": 1} + ], + "int16": [ + {"hex_str": "7FFF", "bit_length": 16, "expected": 32767}, + {"hex_str": "8000", "bit_length": 16, "expected": -32768}, + {"hex_str": "FFFF", "bit_length": 16, "expected": -1}, + {"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}, + {"hex_str": "FFFFFFFF", "bit_length": 32, "expected": -1}, + {"hex_str": "00000000", "bit_length": 32, "expected": 0}, + {"hex_str": "00000001", "bit_length": 32, "expected": 1} + ], + "int64": [ + {"hex_str": "7FFFFFFFFFFFFFFF", "bit_length": 64, "expected": 9223372036854775807}, + {"hex_str": "8000000000000000", "bit_length": 64, "expected": -9223372036854775808}, + {"hex_str": "FFFFFFFFFFFFFFFF", "bit_length": 64, "expected": -1}, + {"hex_str": "0000000000000000", "bit_length": 64, "expected": 0}, + {"hex_str": "0000000000000001", "bit_length": 64, "expected": 1} + ] + } + + def setUp(self): + self.od = canopen.import_od(SAMPLE_XDD, 2) + + def test_load_nonexisting_file(self): + with self.assertRaises(IOError): + canopen.import_od('/path/to/wrong_file.xdd') + + def test_load_unsupported_format(self): + with self.assertRaisesRegex(ValueError, "'py'"): + canopen.import_od(__file__) + + def test_load_file_object(self): + with open(SAMPLE_XDD) as fp: + od = canopen.import_od(fp) + self.assertTrue(len(od) > 0) + + def test_load_explicit_nodeid(self): + od = canopen.import_od(SAMPLE_XDD, node_id=3) + self.assertEqual(od.node_id, 3) + + def test_variable(self): + var = self.od['Producer heartbeat time'] + self.assertIsInstance(var, canopen.objectdictionary.ODVariable) + self.assertEqual(var.index, 0x1017) + self.assertEqual(var.subindex, 0) + self.assertEqual(var.name, 'Producer heartbeat time') + self.assertEqual(var.data_type, canopen.objectdictionary.UNSIGNED16) + self.assertEqual(var.access_type, 'rw') + self.assertEqual(var.default, 0) + self.assertFalse(var.relative) + + def test_relative_variable(self): + var = self.od['Receive PDO 0 Communication Parameter']['COB-ID use by RPDO 1'] + self.assertTrue(var.relative) + self.assertEqual(var.default, 512 + self.od.node_id) + + def test_record(self): + record = self.od['Identity object'] + self.assertIsInstance(record, canopen.objectdictionary.ODRecord) + self.assertEqual(len(record), 4) + self.assertEqual(record.index, 0x1018) + self.assertEqual(record.name, 'Identity object') + var = record['Vendor-ID'] + self.assertIsInstance(var, canopen.objectdictionary.ODVariable) + self.assertEqual(var.name, 'Vendor-ID') + self.assertEqual(var.index, 0x1018) + self.assertEqual(var.subindex, 1) + self.assertEqual(var.data_type, canopen.objectdictionary.UNSIGNED32) + self.assertEqual(var.access_type, 'ro') + + def test_record_with_limits(self): + int8 = self.od[0x3020] + self.assertEqual(int8.min, 0) + self.assertEqual(int8.max, 127) + uint8 = self.od[0x3021] + self.assertEqual(uint8.min, 2) + self.assertEqual(uint8.max, 10) + int32 = self.od[0x3030] + self.assertEqual(int32.min, -2147483648) + self.assertEqual(int32.max, -1) + int64 = self.od[0x3040] + self.assertEqual(int64.min, -10) + self.assertEqual(int64.max, +10) + + def test_array_compact_subobj(self): + array = self.od[0x1003] + self.assertIsInstance(array, canopen.objectdictionary.ODArray) + self.assertEqual(array.index, 0x1003) + self.assertEqual(array.name, 'Pre-defined error field') + var = array[5] + self.assertIsInstance(var, canopen.objectdictionary.ODVariable) + self.assertEqual(var.name, 'Pre-defined error field_5') + self.assertEqual(var.index, 0x1003) + self.assertEqual(var.subindex, 5) + self.assertEqual(var.data_type, canopen.objectdictionary.UNSIGNED32) + self.assertEqual(var.access_type, 'ro') + + def test_explicit_name_subobj(self): + name = self.od[0x3004].name + self.assertEqual(name, 'Sensor Status') + name = self.od[0x3004][1].name + self.assertEqual(name, 'Sensor Status 1') + name = self.od[0x3004][3].name + self.assertEqual(name, 'Sensor Status 3') + value = self.od[0x3004][3].default + self.assertEqual(value, 3) + + def test_parameter_name_with_percent(self): + name = self.od[0x3003].name + self.assertEqual(name, 'Valve % open') + + def test_compact_subobj_parameter_name_with_percent(self): + name = self.od[0x3006].name + self.assertEqual(name, 'Valve 1 % Open') + + def test_sub_index_w_capital_s(self): + name = self.od[0x3010][0].name + self.assertEqual(name, 'Temperature') + + def test_dummy_variable(self): + var = self.od['Dummy0003'] + self.assertIsInstance(var, canopen.objectdictionary.ODVariable) + self.assertEqual(var.index, 0x0003) + self.assertEqual(var.subindex, 0) + self.assertEqual(var.name, 'Dummy0003') + self.assertEqual(var.data_type, canopen.objectdictionary.INTEGER16) + self.assertEqual(var.access_type, 'const') + self.assertEqual(len(var), 16) + + def test_dummy_variable_undefined(self): + with self.assertRaises(KeyError): + var_undef = self.od['Dummy0001'] + + def test_signed_int_from_hex(self): + for data_type, test_cases in self.test_data.items(): + for test_case in test_cases: + with self.subTest(data_type=data_type, test_case=test_case): + result = signed_int_from_hex('0x' + test_case["hex_str"], test_case["bit_length"]) + self.assertEqual(result, test_case["expected"]) + +if __name__ == "__main__": + unittest.main() diff --git a/test/util.py b/test/util.py index 2c8eccca..9c87856f 100644 --- a/test/util.py +++ b/test/util.py @@ -5,6 +5,7 @@ DATATYPES_EDS = os.path.join(os.path.dirname(__file__), "datatypes.eds") SAMPLE_EDS = os.path.join(os.path.dirname(__file__), "sample.eds") +SAMPLE_XDD = os.path.join(os.path.dirname(__file__), "sample.xdd") @contextlib.contextmanager