Skip to content
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
3 changes: 2 additions & 1 deletion can/interfaces/kvaser/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,8 @@ def get_channel_info(channel):
ctypes.sizeof(number),
)

return f"{name.value.decode('ascii')}, S/N {serial.value} (#{number.value + 1})"
name_decoded = name.value.decode("ascii", errors="replace")
return f"{name_decoded}, S/N {serial.value} (#{number.value + 1})"


init_kvaser_library()
21 changes: 21 additions & 0 deletions test/test_kvaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
"""

import ctypes
import time
import unittest
from unittest.mock import Mock
Expand Down Expand Up @@ -49,6 +50,26 @@ def test_bus_creation(self):
self.assertTrue(canlib.canOpenChannel.called)
self.assertTrue(canlib.canBusOn.called)

def test_bus_creation_illegal_channel_name(self):
# Test if the bus constructor is able to deal with non-ASCII characters
def canGetChannelDataMock(
channel: ctypes.c_int,
param: ctypes.c_int,
buf: ctypes.c_void_p,
bufsize: ctypes.c_size_t,
):
if param == constants.canCHANNELDATA_DEVDESCR_ASCII:
buf_char_ptr = ctypes.cast(buf, ctypes.POINTER(ctypes.c_char))
for i, char in enumerate(b"hello\x7a\xcb"):
buf_char_ptr[i] = char

canlib.canGetChannelData = canGetChannelDataMock
bus = can.Bus(channel=0, interface="kvaser")

self.assertTrue(bus.channel_info.startswith("hello"))

bus.shutdown()

def test_bus_shutdown(self):
self.bus.shutdown()
self.assertTrue(canlib.canBusOff.called)
Expand Down