Skip to content

Commit 2d5d6d3

Browse files
Feature/eds parse factor and description (#378)
1 parent 2f42ec6 commit 2d5d6d3

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

canopen/objectdictionary/eds.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,22 @@ def build_variable(eds, section, node_id, index, subindex=0):
309309
var.value = _convert_variable(node_id, var.data_type, eds.get(section, "ParameterValue"))
310310
except ValueError:
311311
pass
312+
# Factor, Description and Unit are not standard according to the CANopen specifications, but they are implemented in the python canopen package, so we can at least try to use them
313+
if eds.has_option(section, "Factor"):
314+
try:
315+
var.factor = float(eds.get(section, "Factor"))
316+
except ValueError:
317+
pass
318+
if eds.has_option(section, "Description"):
319+
try:
320+
var.description = eds.get(section, "Description")
321+
except ValueError:
322+
pass
323+
if eds.has_option(section, "Unit"):
324+
try:
325+
var.unit = eds.get(section, "Unit")
326+
except ValueError:
327+
pass
312328
return var
313329

314330

@@ -376,6 +392,13 @@ def export_variable(var, eds):
376392
if getattr(var, 'max', None) is not None:
377393
eds.set(section, "HighLimit", var.max)
378394

395+
if getattr(var, 'description', '') != '':
396+
eds.set(section, "Description", var.description)
397+
if getattr(var, 'factor', 1) != 1:
398+
eds.set(section, "Factor", var.factor)
399+
if getattr(var, 'unit', '') != '':
400+
eds.set(section, "Unit", var.unit)
401+
379402
def export_record(var, eds):
380403
section = "%04X" % var.index
381404
export_common(var, eds, section)

test/sample.eds

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,3 +938,37 @@ AccessType=rw
938938
HighLimit=0x000000000000000A
939939
LowLimit=0x8000000000000009
940940
PDOMapping=0
941+
942+
943+
[3050]
944+
ParameterName=EDS file extensions
945+
SubNumber=0x7
946+
ObjectType=0x9
947+
948+
[3050sub0]
949+
ParameterName=Highest subindex
950+
ObjectType=0x7
951+
DataType=0x0005
952+
AccessType=ro
953+
DefaultValue=0x02
954+
PDOMapping=0x0
955+
956+
[3050sub1]
957+
ParameterName=FactorAndDescription
958+
ObjectType=0x7
959+
DataType=0x0004
960+
AccessType=ro
961+
PDOMapping=0x0
962+
Factor=0.1
963+
Description=This is the a test description
964+
Unit=mV
965+
966+
[3050sub2]
967+
ParameterName=Error Factor and No Description
968+
ObjectType=0x7
969+
DataType=0x0004
970+
AccessType=ro
971+
PDOMapping=0x0
972+
Factor=ERROR
973+
Description=
974+
Unit=

test/test_eds.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ def test_dummy_variable(self):
111111
def test_dummy_variable_undefined(self):
112112
with self.assertRaises(KeyError):
113113
var_undef = self.od['Dummy0001']
114+
115+
def test_reading_factor(self):
116+
var = self.od['EDS file extensions']['FactorAndDescription']
117+
self.assertEqual(var.factor, 0.1)
118+
self.assertEqual(var.description, "This is the a test description")
119+
self.assertEqual(var.unit,'mV')
120+
var2 = self.od['EDS file extensions']['Error Factor and No Description']
121+
self.assertEqual(var2.description, '')
122+
self.assertEqual(var2.factor, 1)
123+
self.assertEqual(var2.unit, '')
124+
125+
114126

115127
def test_comments(self):
116128
self.assertEqual(self.od.comments,

0 commit comments

Comments
 (0)