Skip to content

Commit

Permalink
Add tests for bitrate parsing in import_eds()
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Jul 3, 2024
1 parent f1315d3 commit 8ea1d1d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions canopen/objectdictionary/eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 15 additions & 0 deletions test/test_eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8ea1d1d

Please sign in to comment.