diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index 608024f3..1be85e7a 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -72,7 +72,7 @@ def import_eds(source, node_id): (str, "OrderCode", "order_code"), (bool, "SimpleBootUpMaster", "simple_boot_up_master"), (bool, "SimpleBootUpSlave", "simple_boot_up_slave"), - (bool, "Granularity", "granularity"), + (int, "Granularity", "granularity"), (bool, "DynamicChannelsSupported", "dynamic_channels_supported"), (bool, "GroupMessaging", "group_messaging"), (int, "NrOfRXPDO", "nr_of_RXPDO"), diff --git a/test/test_eds.py b/test/test_eds.py index 4241d1e8..6c243240 100644 --- a/test/test_eds.py +++ b/test/test_eds.py @@ -92,6 +92,40 @@ def test_load_baudrate(self): od = canopen.import_od(SAMPLE_EDS) self.assertEqual(od.bitrate, 500_000) + def test_load_granularity(self): + for raw_value, expected in [("0", 0), ("1", 1), ("8", 8), ("0x08", 8), (None, None)]: + with self.subTest(raw_value=raw_value): + data = "[DeviceInfo]\n" + if raw_value is not None: + data += f"Granularity={raw_value}\n" + with io.StringIO(data) as source: + source.name = "mock.eds" + od = canopen.import_od(source) + self.assertEqual(od.device_information.granularity, expected) + if expected is not None: + self.assertIs(type(od.device_information.granularity), int) + + def test_export_granularity(self): + # The sample declares Granularity=8; exporting must not turn it into 1. + for doc_type in "eds", "dcf": + with self.subTest(doc_type=doc_type): + with io.StringIO() as dest: + canopen.export_od(self.od, dest, doc_type) + eds = RawConfigParser() + eds.read_string(dest.getvalue()) + self.assertEqual(eds.getint("DeviceInfo", "Granularity"), 8) + + def test_load_device_information_boolean_flags(self): + for name, expected in [ + ("simple_boot_up_master", False), + ("simple_boot_up_slave", True), + ("dynamic_channels_supported", False), + ("group_messaging", False), + ("LSS_supported", False), + ]: + with self.subTest(name=name): + self.assertIs(getattr(self.od.device_information, name), expected) + def test_load_baudrate_fallback(self): # Remove the Baudrate option. with open(SAMPLE_EDS) as f: