From 8ea1d1d64679f91a24de9e531183dd0232c1b757 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 4 Jul 2024 01:50:51 +0200 Subject: [PATCH] Add tests for bitrate parsing in import_eds() --- canopen/objectdictionary/eds.py | 4 ++-- test/test_eds.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index 8dd73762..3884d809 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -85,8 +85,8 @@ def import_eds(source, node_id): pass if eds.has_section("DeviceComissioning"): - if val := eds.get("DeviceComissioning", "Baudrate", fallback=None): - od.bitrate = int(val) * 1000 + if val := eds.getint("DeviceComissioning", "Baudrate", fallback=None): + od.bitrate = val * 1000 if node_id is None: if val := eds.get("DeviceComissioning", "NodeID", fallback=None): diff --git a/test/test_eds.py b/test/test_eds.py index da59e0e6..49d09ab7 100644 --- a/test/test_eds.py +++ b/test/test_eds.py @@ -89,6 +89,21 @@ def test_load_explicit_nodeid(self): od = canopen.import_od(SAMPLE_EDS, node_id=3) self.assertEqual(od.node_id, 3) + def test_load_baudrate(self): + od = canopen.import_od(SAMPLE_EDS) + self.assertEqual(od.bitrate, 500_000) + + def test_load_baudrate_fallback(self): + import io + + # Remove the Baudrate option. + with open(SAMPLE_EDS) as f: + lines = [L for L in f.readlines() if not L.startswith("Baudrate=")] + with io.StringIO("".join(lines)) as buf: + buf.name = "mock.eds" + od = canopen.import_od(buf) + self.assertIsNone(od.bitrate) + def test_variable(self): var = self.od['Producer heartbeat time'] self.assertIsInstance(var, canopen.objectdictionary.ODVariable)