diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index fa694c56..8eb057f5 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -209,6 +209,8 @@ def __init__(self, name: str, index: int): self.storage_location = None self.subindices = {} self.names = {} + #: Key-Value pairs not defined by the standard + self.custom_options = {} def __repr__(self) -> str: return f"<{type(self).__qualname__} {self.name!r} at {pretty_index(self.index)}>" @@ -268,6 +270,8 @@ def __init__(self, name: str, index: int): self.storage_location = None self.subindices = {} self.names = {} + #: Key-Value pairs not defined by the standard + self.custom_options = {} def __repr__(self) -> str: return f"<{type(self).__qualname__} {self.name!r} at {pretty_index(self.index)}>" @@ -374,6 +378,8 @@ def __init__(self, name: str, index: int, subindex: int = 0): self.storage_location = None #: Can this variable be mapped to a PDO self.pdo_mappable = False + #: Key-Value pairs not defined by the standard + self.custom_options = {} def __repr__(self) -> str: subindex = self.subindex if isinstance(self.parent, (ODRecord, ODArray)) else None diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index 8ab1a349..ab6e2301 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -140,14 +140,17 @@ def import_eds(source, node_id): arr.add_member(last_subindex) arr.add_member(build_variable(eds, section, node_id, index, 1)) arr.storage_location = storage_location + arr.custom_options = _get_custom_options(eds, section) od.add_object(arr) elif object_type == ARR: arr = objectdictionary.ODArray(name, index) arr.storage_location = storage_location + arr.custom_options = _get_custom_options(eds, section) od.add_object(arr) elif object_type == RECORD: record = objectdictionary.ODRecord(name, index) record.storage_location = storage_location + record.custom_options = _get_custom_options(eds, section) od.add_object(record) continue @@ -253,6 +256,19 @@ def _revert_variable(var_type, value): else: return f"0x{value:02X}" +_STANDARD_OPTIONS = ["objecttype" , "parametername" , "datatype" , "accesstype" , + "pdomapping" , "lowlimit" , "highlimit" , "defaultvalue" , + "parametervalue" , "factor" , "description" , "unit" , + "storagelocation" , "compactsubobj" , "nrofentries" , "subnumber" , + "objflags" , "denotation" ] + +def _get_custom_options(eds, section): + custom_options = {} + for option, value in eds.items(section): + if not (option.lower() in _STANDARD_OPTIONS or option.isdigit()) : + custom_options[option] = value + return custom_options + def build_variable(eds, section, node_id, index, subindex=0): """Creates a object dictionary entry. @@ -332,6 +348,8 @@ def build_variable(eds, section, node_id, index, subindex=0): var.unit = eds.get(section, "Unit") except ValueError: pass + + var.custom_options = _get_custom_options(eds, section) return var @@ -406,12 +424,17 @@ def export_variable(var, eds): if getattr(var, 'unit', '') != '': eds.set(section, "Unit", var.unit) + for option, value in var.custom_options.items(): + eds.set(section, option, value) + def export_record(var, eds): section = f"{var.index:04X}" export_common(var, eds, section) eds.set(section, "SubNumber", f"0x{len(var.subindices):X}") ot = RECORD if isinstance(var, objectdictionary.ODRecord) else ARR eds.set(section, "ObjectType", f"0x{ot:X}") + for option, value in var.custom_options.items(): + eds.set(section, option, value) for i in var: export_variable(var[i], eds)