Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extra info for Kvaser dongles #1797

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions can/interfaces/kvaser/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +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"]})'
log.info("%d: %s", idx, channel_info)
if idx == channel:
self.channel_info = channel_info
Expand Down Expand Up @@ -751,16 +752,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 @@ -787,8 +791,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