Skip to content

Commit c87db06

Browse files
Added extra info for Kvaser dongles (#1797)
* 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. * Updated key value for dongle name Changed "name" into "device_name" as suggested by python-can owner. * Fixed retrocompatibility problem Changed " with ' for retrocompatibility problem with older Python versions. * 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. * Fixed missed changes
1 parent 5cc2534 commit c87db06

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

can/interfaces/kvaser/canlib.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ def __init__(
479479
log.info("Found %d available channels", num_channels)
480480
for idx in range(num_channels):
481481
channel_info = get_channel_info(idx)
482+
channel_info = f'{channel_info["device_name"]}, S/N {channel_info["serial"]} (#{channel_info["dongle_channel"]})'
482483
log.info("%d: %s", idx, channel_info)
483484
if idx == channel:
484485
self.channel_info = channel_info
@@ -766,16 +767,19 @@ def get_stats(self) -> structures.BusStatistics:
766767

767768
@staticmethod
768769
def _detect_available_configs():
769-
num_channels = ctypes.c_int(0)
770+
config_list = []
771+
770772
try:
773+
num_channels = ctypes.c_int(0)
771774
canGetNumberOfChannels(ctypes.byref(num_channels))
775+
776+
for channel in range(0, int(num_channels.value)):
777+
info = get_channel_info(channel)
778+
779+
config_list.append({"interface": "kvaser", "channel": channel, **info})
772780
except (CANLIBError, NameError):
773781
pass
774-
775-
return [
776-
{"interface": "kvaser", "channel": channel}
777-
for channel in range(num_channels.value)
778-
]
782+
return config_list
779783

780784

781785
def get_channel_info(channel):
@@ -802,8 +806,11 @@ def get_channel_info(channel):
802806
ctypes.sizeof(number),
803807
)
804808

805-
name_decoded = name.value.decode("ascii", errors="replace")
806-
return f"{name_decoded}, S/N {serial.value} (#{number.value + 1})"
809+
return {
810+
"device_name": name.value.decode("ascii", errors="replace"),
811+
"serial": serial.value,
812+
"dongle_channel": number.value + 1,
813+
}
807814

808815

809816
init_kvaser_library()

test/test_kvaser.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,20 @@ def test_recv_standard(self):
163163
def test_available_configs(self):
164164
configs = canlib.KvaserBus._detect_available_configs()
165165
expected = [
166-
{"interface": "kvaser", "channel": 0},
167-
{"interface": "kvaser", "channel": 1},
166+
{
167+
"interface": "kvaser",
168+
"channel": 0,
169+
"dongle_channel": 1,
170+
"device_name": "",
171+
"serial": 0,
172+
},
173+
{
174+
"interface": "kvaser",
175+
"channel": 1,
176+
"dongle_channel": 1,
177+
"device_name": "",
178+
"serial": 0,
179+
},
168180
]
169181
self.assertListEqual(configs, expected)
170182

0 commit comments

Comments
 (0)