From 436e78f4356774d67ba00d575b01f943f871bd0e Mon Sep 17 00:00:00 2001 From: Pierre-Luc Date: Thu, 9 Jul 2020 09:42:52 -0400 Subject: [PATCH 1/2] Raising more precise API error when set bitrate fails Also calling shutdown before raising exception from the init if the device is opened --- can/interfaces/ics_neovi/neovi_bus.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index 9d41cec8e..eff3620dc 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -158,15 +158,25 @@ def __init__(self, channel, can_filters=None, **kwargs): with open_lock: ics.open_device(self.dev) - if "bitrate" in kwargs: - for channel in self.channels: - ics.set_bit_rate(self.dev, kwargs.get("bitrate"), channel) - - fd = kwargs.get("fd", False) - if fd: - if "data_bitrate" in kwargs: + try: + if "bitrate" in kwargs: for channel in self.channels: - ics.set_fd_bit_rate(self.dev, kwargs.get("data_bitrate"), channel) + ics.set_bit_rate(self.dev, kwargs.get("bitrate"), channel) + + fd = kwargs.get("fd", False) + if fd: + if "data_bitrate" in kwargs: + for channel in self.channels: + ics.set_fd_bit_rate( + self.dev, kwargs.get("data_bitrate"), channel + ) + except ics.RuntimeError as re: + logger.error(re) + err = ICSApiError(*ics.get_last_api_error(self.dev)) + try: + self.shutdown() + finally: + raise err self._use_system_timestamp = bool(kwargs.get("use_system_timestamp", False)) self._receive_own_messages = kwargs.get("receive_own_messages", True) From 8fca2a9d1af97204ce4d864323a552eba2ac5a60 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Date: Thu, 9 Jul 2020 10:56:12 -0400 Subject: [PATCH 2/2] Simplified if fd condition in init --- can/interfaces/ics_neovi/neovi_bus.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index eff3620dc..aa332bfcf 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -163,8 +163,7 @@ def __init__(self, channel, can_filters=None, **kwargs): for channel in self.channels: ics.set_bit_rate(self.dev, kwargs.get("bitrate"), channel) - fd = kwargs.get("fd", False) - if fd: + if kwargs.get("fd", False): if "data_bitrate" in kwargs: for channel in self.channels: ics.set_fd_bit_rate(