From e13764392cd61700a2b03b6ccf406f1e61a39739 Mon Sep 17 00:00:00 2001 From: Federico Spada Date: Sat, 6 Apr 2024 11:11:54 +0200 Subject: [PATCH 1/5] Added extra info for Kvaser dongles Changed Kvaser auto-detection function to return additional info regarding the dongles found, such as Serial Number, Name and Dongle Channel number. In this way it's possible to discern between different physical dongles connected to the PC and if they are Virtual Channels or not. --- can/interfaces/kvaser/canlib.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index 4c621ecf4..b14671bae 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -471,6 +471,7 @@ def __init__( log.info("Found %d available channels", num_channels) for idx in range(num_channels): channel_info = get_channel_info(idx) + channel_info = f"{channel_info["name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle channel"]})" log.info("%d: %s", idx, channel_info) if idx == channel: self.channel_info = channel_info @@ -752,16 +753,25 @@ def get_stats(self) -> structures.BusStatistics: @staticmethod def _detect_available_configs(): - num_channels = ctypes.c_int(0) + config_list = [] + try: + num_channels = ctypes.c_int(0) canGetNumberOfChannels(ctypes.byref(num_channels)) + + for channel in range(0, int(num_channels.value)): + info = get_channel_info(channel) + + config_list.append( + { + "interface": "kvaser", + "channel": channel, + **info + } + ) except (CANLIBError, NameError): pass - - return [ - {"interface": "kvaser", "channel": channel} - for channel in range(num_channels.value) - ] + return config_list def get_channel_info(channel): @@ -788,8 +798,10 @@ def get_channel_info(channel): ctypes.sizeof(number), ) - name_decoded = name.value.decode("ascii", errors="replace") - return f"{name_decoded}, S/N {serial.value} (#{number.value + 1})" - + return { + "name": name.value.decode("ascii", errors="replace"), + "serial": serial.value, + "dongle channel": number.value + 1 + } init_kvaser_library() From a598f199e7adf65ed6ad17a629c8477aab3d55ea Mon Sep 17 00:00:00 2001 From: Federico Spada Date: Mon, 8 Apr 2024 17:51:35 +0200 Subject: [PATCH 2/5] Updated key value for dongle name Changed "name" into "device_name" as suggested by python-can owner. --- can/interfaces/kvaser/canlib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index b14671bae..d93e1b314 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -471,7 +471,7 @@ def __init__( log.info("Found %d available channels", num_channels) for idx in range(num_channels): channel_info = get_channel_info(idx) - channel_info = f"{channel_info["name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle channel"]})" + channel_info = f"{channel_info["device_name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle channel"]})" log.info("%d: %s", idx, channel_info) if idx == channel: self.channel_info = channel_info @@ -799,7 +799,7 @@ def get_channel_info(channel): ) return { - "name": name.value.decode("ascii", errors="replace"), + "device_name": name.value.decode("ascii", errors="replace"), "serial": serial.value, "dongle channel": number.value + 1 } From ca330732760ff9f6e797c580da1e2d2e05fecd19 Mon Sep 17 00:00:00 2001 From: Federico Spada Date: Sun, 21 Apr 2024 12:10:30 +0200 Subject: [PATCH 3/5] Fixed retrocompatibility problem Changed " with ' for retrocompatibility problem with older Python versions. --- can/interfaces/kvaser/canlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index d93e1b314..a68f90827 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -471,7 +471,7 @@ def __init__( log.info("Found %d available channels", num_channels) for idx in range(num_channels): channel_info = get_channel_info(idx) - channel_info = f"{channel_info["device_name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle channel"]})" + channel_info = f'{channel_info["device_name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle channel"]})' log.info("%d: %s", idx, channel_info) if idx == channel: self.channel_info = channel_info From 268f14487d99d0b5478d0f09d8923a35e7f7def7 Mon Sep 17 00:00:00 2001 From: Federico Spada Date: Sat, 29 Jun 2024 10:09:11 +0200 Subject: [PATCH 4/5] Fixed failed Test I've updated the format using Black and modified test_kvaser.py\test_available_configs to consider the new _detect_available_configs output. --- can/interfaces/kvaser/canlib.py | 13 ++++--------- test/test_kvaser.py | 4 ++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index 8000ab37c..c6e64b66b 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -761,13 +761,7 @@ def _detect_available_configs(): for channel in range(0, int(num_channels.value)): info = get_channel_info(channel) - config_list.append( - { - "interface": "kvaser", - "channel": channel, - **info - } - ) + config_list.append({"interface": "kvaser", "channel": channel, **info}) except (CANLIBError, NameError): pass return config_list @@ -800,7 +794,8 @@ def get_channel_info(channel): return { "device_name": name.value.decode("ascii", errors="replace"), "serial": serial.value, - "dongle channel": number.value + 1 - } + "dongle_channel": number.value + 1, + } + init_kvaser_library() diff --git a/test/test_kvaser.py b/test/test_kvaser.py index abaf7b38f..7af18e42a 100644 --- a/test/test_kvaser.py +++ b/test/test_kvaser.py @@ -163,8 +163,8 @@ def test_recv_standard(self): def test_available_configs(self): configs = canlib.KvaserBus._detect_available_configs() expected = [ - {"interface": "kvaser", "channel": 0}, - {"interface": "kvaser", "channel": 1}, + {"interface": "kvaser", "channel": 0, 'dongle_channel': 1, 'device_name': '', 'serial': 0}, + {"interface": "kvaser", "channel": 1, 'dongle_channel': 1, 'device_name': '', 'serial': 0}, ] self.assertListEqual(configs, expected) From 875f8d53648c6b2e7c0a95daeb212774a369413c Mon Sep 17 00:00:00 2001 From: Federico Spada Date: Sat, 29 Jun 2024 10:15:54 +0200 Subject: [PATCH 5/5] Fixed missed changes --- can/interfaces/kvaser/canlib.py | 2 +- test/test_kvaser.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index c6e64b66b..a776999a0 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -470,7 +470,7 @@ def __init__( log.info("Found %d available channels", num_channels) for idx in range(num_channels): channel_info = get_channel_info(idx) - channel_info = f'{channel_info["device_name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle channel"]})' + channel_info = f'{channel_info["device_name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle_channel"]})' log.info("%d: %s", idx, channel_info) if idx == channel: self.channel_info = channel_info diff --git a/test/test_kvaser.py b/test/test_kvaser.py index 7af18e42a..e389d0f0b 100644 --- a/test/test_kvaser.py +++ b/test/test_kvaser.py @@ -163,8 +163,20 @@ def test_recv_standard(self): def test_available_configs(self): configs = canlib.KvaserBus._detect_available_configs() expected = [ - {"interface": "kvaser", "channel": 0, 'dongle_channel': 1, 'device_name': '', 'serial': 0}, - {"interface": "kvaser", "channel": 1, 'dongle_channel': 1, 'device_name': '', 'serial': 0}, + { + "interface": "kvaser", + "channel": 0, + "dongle_channel": 1, + "device_name": "", + "serial": 0, + }, + { + "interface": "kvaser", + "channel": 1, + "dongle_channel": 1, + "device_name": "", + "serial": 0, + }, ] self.assertListEqual(configs, expected)