Skip to content

add silent mode support for vector #1764

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

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 26 additions & 1 deletion can/interfaces/vector/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ def __init__(
self.channel_masks: Dict[int, int] = {}
self.index_to_channel: Dict[int, int] = {}
self._can_protocol = CanProtocol.CAN_FD if is_fd else CanProtocol.CAN_20

self.silent_mode = kwargs.get('silent_mode', False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer listen_only like in socketcan. Also, make this variable private.


for channel in self.channels:
if (
Expand Down Expand Up @@ -232,7 +234,7 @@ def __init__(

permission_mask = xlclass.XLaccess()
# Set mask to request channel init permission if needed
if bitrate or fd or timing:
if bitrate or fd or timing or self.silent_mode:
permission_mask.value = self.mask

interface_version = (
Expand Down Expand Up @@ -311,6 +313,9 @@ def __init__(
if assert_timing:
self._check_can_settings(channel_mask=self.mask, bitrate=bitrate)

if self.silent_mode:
self._set_mode(channel_mask=self.mask, silent=True)

# Enable/disable TX receipts
tx_receipts = 1 if receive_own_messages else 0
self.xldriver.xlCanSetChannelMode(self.port_handle, self.mask, tx_receipts, 0)
Expand Down Expand Up @@ -445,6 +450,26 @@ def _read_bus_params(
f"Channel configuration for channel {channel} not found."
)

def _set_mode(self, channel_mask: int, silent: bool) -> None:
# set parameters for channels with init access
channel_mask = channel_mask & self.permission_mask

if channel_mask:
if silent:
self.xldriver.xlCanSetChannelOutput(
self.port_handle,
channel_mask,
xldefine.XL_OutputMode.XL_OUTPUT_MODE_SILENT
)
else:
self.xldriver.xlCanSetChannelOutput(
self.port_handle,
channel_mask,
xldefine.XL_OutputMode.XL_OUTPUT_MODE_NORMAL
)

LOG.info("xlCanSetChannelOutput: silent_mode=%u", silent)

def _set_bitrate(self, channel_mask: int, bitrate: int) -> None:
# set parameters for channels with init access
channel_mask = channel_mask & self.permission_mask
Expand Down
22 changes: 22 additions & 0 deletions test/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ def mock_xldriver() -> None:
canlib.HAS_EVENTS = real_has_events


def test_silent_mode() -> None:
bus = can.Bus(channel=0,
serial=_find_virtual_can_serial(),
interface="vector",
receive_own_messages=True,
silent=True)
assert isinstance(bus, canlib.VectorBus)
assert bus.protocol == can.CanProtocol.CAN_20

msg = can.Message(
arbitration_id=0xC0FFEF, data=[1, 2, 3, 4, 5, 6, 7, 8], is_extended_id=True
)

bus.send(msg)

received_msg = bus.recv()

assert received_msg == msg

bus.shutdown()


def test_bus_creation_mocked(mock_xldriver) -> None:
bus = can.Bus(channel=0, interface="vector", _testing=True)
assert isinstance(bus, canlib.VectorBus)
Expand Down