From f3fa07107d08c6225b9ab77443df13507c1a9c4b Mon Sep 17 00:00:00 2001 From: luojiaaoo <62821977+luojiaaoo@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:11:04 +0800 Subject: [PATCH] Kvaser: add parameter exclusive and override_exclusive (#1660) * * For interface Kvaser, add parameter exclusive Don't allow sharing of this CANlib channel. * For interface Kvaser, add parameter override_exclusive Open the channel even if it is opened for exclusive access already. * fix --------- Co-authored-by: luoja --- can/interfaces/kvaser/canlib.py | 17 ++++++++++++++++- can/interfaces/kvaser/constants.py | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index 4e5e8c51b..38949137d 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -404,6 +404,10 @@ def __init__(self, channel, can_filters=None, **kwargs): computer, set this to True or set single_handle to True. :param bool fd: If CAN-FD frames should be supported. + :param bool exclusive: + Don't allow sharing of this CANlib channel. + :param bool override_exclusive: + Open the channel even if it is opened for exclusive access already. :param int data_bitrate: Which bitrate to use for data phase in CAN FD. Defaults to arbitration bitrate. @@ -420,6 +424,8 @@ def __init__(self, channel, can_filters=None, **kwargs): driver_mode = kwargs.get("driver_mode", DRIVER_MODE_NORMAL) single_handle = kwargs.get("single_handle", False) receive_own_messages = kwargs.get("receive_own_messages", False) + exclusive = kwargs.get("exclusive", False) + override_exclusive = kwargs.get("override_exclusive", False) accept_virtual = kwargs.get("accept_virtual", True) fd = kwargs.get("fd", False) data_bitrate = kwargs.get("data_bitrate", None) @@ -445,6 +451,10 @@ def __init__(self, channel, can_filters=None, **kwargs): self.channel_info = channel_info flags = 0 + if exclusive: + flags |= canstat.canOPEN_EXCLUSIVE + if override_exclusive: + flags |= canstat.canOPEN_OVERRIDE_EXCLUSIVE if accept_virtual: flags |= canstat.canOPEN_ACCEPT_VIRTUAL if fd: @@ -491,7 +501,12 @@ def __init__(self, channel, can_filters=None, **kwargs): self._write_handle = self._read_handle else: log.debug("Creating separate handle for TX on channel: %s", channel) - self._write_handle = canOpenChannel(channel, flags) + if exclusive: + flags_ = flags & ~canstat.canOPEN_EXCLUSIVE + flags_ |= canstat.canOPEN_OVERRIDE_EXCLUSIVE + else: + flags_ = flags + self._write_handle = canOpenChannel(channel, flags_) canBusOn(self._read_handle) can_driver_mode = ( diff --git a/can/interfaces/kvaser/constants.py b/can/interfaces/kvaser/constants.py index 9dd3a9163..3d01faa84 100644 --- a/can/interfaces/kvaser/constants.py +++ b/can/interfaces/kvaser/constants.py @@ -161,6 +161,8 @@ def CANSTATUS_SUCCESS(status): canDRIVER_SELFRECEPTION = 8 canDRIVER_OFF = 0 +canOPEN_EXCLUSIVE = 0x0008 +canOPEN_REQUIRE_EXTENDED = 0x0010 canOPEN_ACCEPT_VIRTUAL = 0x0020 canOPEN_OVERRIDE_EXCLUSIVE = 0x0040 canOPEN_REQUIRE_INIT_ACCESS = 0x0080