Skip to content

Commit

Permalink
Added extra info for Kvaser dongles (#1797)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FedericoSpada authored Jul 3, 2024
1 parent 5cc2534 commit c87db06
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
23 changes: 15 additions & 8 deletions can/interfaces/kvaser/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,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"]})'
log.info("%d: %s", idx, channel_info)
if idx == channel:
self.channel_info = channel_info
Expand Down Expand Up @@ -766,16 +767,19 @@ 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):
Expand All @@ -802,8 +806,11 @@ 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 {
"device_name": name.value.decode("ascii", errors="replace"),
"serial": serial.value,
"dongle_channel": number.value + 1,
}


init_kvaser_library()
16 changes: 14 additions & 2 deletions test/test_kvaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
{"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)

Expand Down

0 comments on commit c87db06

Please sign in to comment.