From 3bcca6f0d51e0d8dfa2eb6153f1f26c19f8e5239 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Wed, 1 Feb 2017 10:36:31 +0100 Subject: [PATCH 01/26] Try to dlsym() the required functions only if the canlib has been loaded --- can/interfaces/kvaser/canlib.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index a951e39de..b1f14d153 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -319,6 +319,17 @@ def __check_bus_handle_validity(handle, function, arguments): errcheck=__check_status_operation, ) + kvFlashLeds = __get_canlib_function("kvFlashLeds", + argtypes=[c_canHandle, ctypes.c_int, + ctypes.c_int], + restype=ctypes.c_short, + errcheck=__check_status) + + if sys.platform == "win32": + canGetVersionEx = __get_canlib_function("canGetVersionEx", + argtypes=[ctypes.c_uint], + restype=ctypes.c_uint, + errcheck=__check_status) def init_kvaser_library(): if __canlib is not None: @@ -330,6 +341,16 @@ def init_kvaser_library(): log.warning("Kvaser canlib could not be initialized.") +def init_kvaser_library(): + if __canlib is not None: + try: + log.debug("Initializing Kvaser CAN library") + canInitializeLibrary() + log.debug("CAN library initialized") + except: + log.warning("Kvaser canlib could not be initialized.") + + DRIVER_MODE_SILENT = False DRIVER_MODE_NORMAL = True From e46464322369461ef90846dabed2e2fee93c249d Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Wed, 13 Oct 2021 14:48:28 +0000 Subject: [PATCH 02/26] Format code with black --- can/interfaces/kvaser/canlib.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index b1f14d153..53b7fec7d 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -319,17 +319,21 @@ def __check_bus_handle_validity(handle, function, arguments): errcheck=__check_status_operation, ) - kvFlashLeds = __get_canlib_function("kvFlashLeds", - argtypes=[c_canHandle, ctypes.c_int, - ctypes.c_int], - restype=ctypes.c_short, - errcheck=__check_status) + kvFlashLeds = __get_canlib_function( + "kvFlashLeds", + argtypes=[c_canHandle, ctypes.c_int, ctypes.c_int], + restype=ctypes.c_short, + errcheck=__check_status, + ) if sys.platform == "win32": - canGetVersionEx = __get_canlib_function("canGetVersionEx", - argtypes=[ctypes.c_uint], - restype=ctypes.c_uint, - errcheck=__check_status) + canGetVersionEx = __get_canlib_function( + "canGetVersionEx", + argtypes=[ctypes.c_uint], + restype=ctypes.c_uint, + errcheck=__check_status, + ) + def init_kvaser_library(): if __canlib is not None: From d6999aec460bebf06458303bf03e1d7c97c7c064 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Fri, 15 Oct 2021 11:32:04 +0200 Subject: [PATCH 03/26] Added comment to CAN_MSGFLAGS_* and CAN_MSGFLAGS2_* constants --- can/interfaces/ixxat/constants.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/can/interfaces/ixxat/constants.py b/can/interfaces/ixxat/constants.py index 1dbc22a44..429881964 100644 --- a/can/interfaces/ixxat/constants.py +++ b/can/interfaces/ixxat/constants.py @@ -106,12 +106,12 @@ VCI_E_WRONG_FLASHFWVERSION = SEV_VCI_ERROR | 0x001A # Controller status -CAN_STATUS_TXPEND = 0x01 -CAN_STATUS_OVRRUN = 0x02 -CAN_STATUS_ERRLIM = 0x04 -CAN_STATUS_BUSOFF = 0x08 -CAN_STATUS_ININIT = 0x10 -CAN_STATUS_BUSCERR = 0x20 +CAN_STATUS_TXPEND = 0x01 # transmission pending +CAN_STATUS_OVRRUN = 0x02 # data overrun occurred +CAN_STATUS_ERRLIM = 0x04 # error warning limit exceeded +CAN_STATUS_BUSOFF = 0x08 # bus off status +CAN_STATUS_ININIT = 0x10 # init mode active +CAN_STATUS_BUSCERR = 0x20 # bus coupling error # Controller operating modes CAN_OPMODE_UNDEFINED = 0x00 @@ -167,6 +167,7 @@ CAN_FILTER_EXCL = 0x04 # exclusive filtering (inhibit registered IDs) +# message information flags (used by ) CAN_MSGFLAGS_DLC = 0x0F # [bit 0] data length code CAN_MSGFLAGS_OVR = 0x10 # [bit 4] data overrun flag CAN_MSGFLAGS_SRR = 0x20 # [bit 5] self reception request @@ -174,6 +175,7 @@ CAN_MSGFLAGS_EXT = 0x80 # [bit 7] frame format (0=11-bit, 1=29-bit) +# extended message information flags (used by ) CAN_MSGFLAGS2_SSM = 0x01 # [bit 0] single shot mode CAN_MSGFLAGS2_HPM = 0x02 # [bit 1] high priority message CAN_MSGFLAGS2_EDL = 0x04 # [bit 2] extended data length From 6aa0582b1ba455ff09e79f9c8ee565da1a9f0f85 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:02:24 +0100 Subject: [PATCH 04/26] CANMSGINFO.bAddFlags has been renamed to bFlags2 in IXXAT VCI4 --- can/interfaces/ixxat/constants.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/interfaces/ixxat/constants.py b/can/interfaces/ixxat/constants.py index 429881964..6866be11d 100644 --- a/can/interfaces/ixxat/constants.py +++ b/can/interfaces/ixxat/constants.py @@ -1,5 +1,5 @@ """ -Ctypes wrapper module for IXXAT Virtual CAN Interface V3 on win32 systems +Ctypes wrapper module for IXXAT Virtual CAN Interface V4 on win32 systems Copyright (C) 2016 Giuseppe Corbelli """ @@ -175,7 +175,7 @@ CAN_MSGFLAGS_EXT = 0x80 # [bit 7] frame format (0=11-bit, 1=29-bit) -# extended message information flags (used by ) +# extended message information flags (used by ) CAN_MSGFLAGS2_SSM = 0x01 # [bit 0] single shot mode CAN_MSGFLAGS2_HPM = 0x02 # [bit 1] high priority message CAN_MSGFLAGS2_EDL = 0x04 # [bit 2] extended data length From 067b45d24f539831eaa3e67a2bedf455641702ce Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:04:06 +0100 Subject: [PATCH 05/26] Added a comment that CANMSGINFO.Bytes.bAddFlags is called bFlags2 in VCI v4. --- can/interfaces/ixxat/structures.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/interfaces/ixxat/structures.py b/can/interfaces/ixxat/structures.py index f76a39a38..222a391f1 100644 --- a/can/interfaces/ixxat/structures.py +++ b/can/interfaces/ixxat/structures.py @@ -1,5 +1,5 @@ """ -Ctypes wrapper module for IXXAT Virtual CAN Interface V3 on win32 systems +Ctypes wrapper module for IXXAT Virtual CAN Interface V4 on win32 systems Copyright (C) 2016 Giuseppe Corbelli """ @@ -118,7 +118,7 @@ class Bytes(ctypes.Structure): ( "bAddFlags", ctypes.c_uint8, - ), # extended flags (see CAN_MSGFLAGS2_ constants) + ), # extended flags (see CAN_MSGFLAGS2_ constants). AKA bFlags2 in VCI v4 ("bFlags", ctypes.c_uint8), # flags (see CAN_MSGFLAGS_ constants) ("bAccept", ctypes.c_uint8), # accept code (see CAN_ACCEPT_ constants) ] From 648287394ad7f7d598dfc2345ef22c06a4ad95f0 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:07:22 +0100 Subject: [PATCH 06/26] Implementation is now tested against VCI v4 --- can/interfaces/ixxat/__init__.py | 2 +- can/interfaces/ixxat/canlib_vcinpl.py | 2 +- can/interfaces/ixxat/exceptions.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/can/interfaces/ixxat/__init__.py b/can/interfaces/ixxat/__init__.py index 347caed50..49399caac 100644 --- a/can/interfaces/ixxat/__init__.py +++ b/can/interfaces/ixxat/__init__.py @@ -1,5 +1,5 @@ """ -Ctypes wrapper module for IXXAT Virtual CAN Interface V3 on win32 systems +Ctypes wrapper module for IXXAT Virtual CAN Interface V4 on win32 systems Copyright (C) 2016 Giuseppe Corbelli """ diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index cb0447b49..54bdae944 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -1,5 +1,5 @@ """ -Ctypes wrapper module for IXXAT Virtual CAN Interface V3 on win32 systems +Ctypes wrapper module for IXXAT Virtual CAN Interface V4 on win32 systems TODO: We could implement this interface such that setting other filters could work when the initial filters were set to zero using the diff --git a/can/interfaces/ixxat/exceptions.py b/can/interfaces/ixxat/exceptions.py index babe08e3b..50b84dfa4 100644 --- a/can/interfaces/ixxat/exceptions.py +++ b/can/interfaces/ixxat/exceptions.py @@ -1,5 +1,5 @@ """ -Ctypes wrapper module for IXXAT Virtual CAN Interface V3 on win32 systems +Ctypes wrapper module for IXXAT Virtual CAN Interface V4 on win32 systems Copyright (C) 2016 Giuseppe Corbelli Copyright (C) 2019 Marcel Kanter From 66cc67f0595f54ffd23af72c86396e6113f11500 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:19:04 +0100 Subject: [PATCH 07/26] Dropped manual timeout handling as it is a job done by BusABC.read(). Better handling of CAN error messages: - In case of HW errors (overrun, warning limit exceeded, bus coupling error) raise VCIError - In case of error log IXXAT-specific error codes --- can/interfaces/ixxat/canlib.py | 1 + can/interfaces/ixxat/canlib_vcinpl.py | 125 +++++++++++--------------- 2 files changed, 53 insertions(+), 73 deletions(-) diff --git a/can/interfaces/ixxat/canlib.py b/can/interfaces/ixxat/canlib.py index 4dc0d3e6e..ece90357c 100644 --- a/can/interfaces/ixxat/canlib.py +++ b/can/interfaces/ixxat/canlib.py @@ -2,6 +2,7 @@ import can.interfaces.ixxat.canlib_vcinpl2 as vcinpl2 from can import BusABC, Message + from typing import Optional diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index 54bdae944..aaff22f6d 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -639,85 +639,64 @@ def flush_tx_buffer(self): def _recv_internal(self, timeout): """Read a message from IXXAT device.""" - - # TODO: handling CAN error messages? data_received = False - if timeout == 0: + if self._inWaiting() or timeout == 0: # Peek without waiting - try: - _canlib.canChannelPeekMessage( - self._channel_handle, ctypes.byref(self._message) - ) - except (VCITimeout, VCIRxQueueEmptyError): - return None, True - else: - if self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_DATA: - data_received = True + recv_function = functools.partial(_canlib.canChannelPeekMessage, self._channel_handle, ctypes.byref(self._message)) else: # Wait if no message available - if timeout is None or timeout < 0: - remaining_ms = constants.INFINITE - t0 = None - else: - timeout_ms = int(timeout * 1000) - remaining_ms = timeout_ms - t0 = perf_counter() - - while True: - try: - _canlib.canChannelReadMessage( - self._channel_handle, remaining_ms, ctypes.byref(self._message) - ) - except (VCITimeout, VCIRxQueueEmptyError): - # Ignore the 2 errors, the timeout is handled manually with the perf_counter() - pass - else: - # See if we got a data or info/error messages - if self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_DATA: - data_received = True - break - elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_INFO: - log.info( - CAN_INFO_MESSAGES.get( - self._message.abData[0], - "Unknown CAN info message code {}".format( - self._message.abData[0] - ), - ) - ) - - elif ( - self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_ERROR - ): - log.warning( - CAN_ERROR_MESSAGES.get( - self._message.abData[0], - "Unknown CAN error message code {}".format( - self._message.abData[0] - ), - ) - ) + timeout = constants.INFINITE if (timeout is None or timeout < 0) else int(timeout * 1000) + recv_function = functools.partial(_canlib.canChannelReadMessage, self._channel_handle, timeout_ms, ctypes.byref(self._message)) - elif ( - self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_STATUS - ): - log.info(_format_can_status(self._message.abData[0])) - if self._message.abData[0] & constants.CAN_STATUS_BUSOFF: - raise VCIBusOffError() - - elif ( - self._message.uMsgInfo.Bits.type - == constants.CAN_MSGTYPE_TIMEOVR - ): - pass - else: - log.warning("Unexpected message info type") - - if t0 is not None: - remaining_ms = timeout_ms - int((perf_counter() - t0) * 1000) - if remaining_ms < 0: - break + try: + recv_function() + except (VCITimeout, VCIRxQueueEmptyError) as e: + # Ignore the 2 errors, overall timeout is handled by BusABC.recv + pass + else: + # See if we got a data or info/error messages + if self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_DATA: + data_received = True + elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_INFO: + log.info(CAN_INFO_MESSAGES.get( + self._message.abData[0], + "Unknown CAN info message code {}".format(self._message.abData[0]) + )) + elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_ERROR: + if self._message.uMsgInfo.Bytes.bFlags & constants.CAN_MSGFLAGS_OVR: + log.warning("CAN error: data overrun") + else: + log.warning(CAN_ERROR_MESSAGES.get( + self._message.abData[0], + "Unknown CAN error message code {}".format(self._message.abData[0]) + )) + log.warning( + "CAN message flags bFlags2 0x%02X bflags 0x%02X", + self._message.uMsgInfo.Bytes.bFlags2, + self._message.uMsgInfo.Bytes.bFlags + ) + elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_TIMEOVR: + pass + else: + log.warn("Unexpected message info type 0x%X", self._message.uMsgInfo.Bits.type) + finally: + # Check hard errors + status = structures.CANLINESTATUS() + _canlib.canControlGetStatus(self._control_handle, ctypes.byref(status)) + error_byte_1 = status.dwStatus & 0x0F + error_byte_2 = status.dwStatus & 0xF0 + if error_byte_1 > constants.CAN_STATUS_TXPEND: + # CAN_STATUS_OVRRUN = 0x02 # data overrun occurred + # CAN_STATUS_ERRLIM = 0x04 # error warning limit exceeded + if error_byte_1 & constants.CAN_STATUS_OVRRUN: + raise VCIError("Data overrun occurred") + elif error_byte_1 & constants.CAN_STATUS_ERRLIM: + raise VCIError("Error warning limit exceeded") + elif error_byte_2 > constants.CAN_STATUS_ININIT: + # CAN_STATUS_BUSCERR = 0x20 # bus coupling error + if error_byte_2 & constants.CAN_STATUS_BUSCERR: + raise VCIError("Bus coupling error") if not data_received: # Timed out / can message type is not DATA From 5153391e7dcf1d8b363c0a24160ca74919174637 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:21:39 +0100 Subject: [PATCH 08/26] Fixed unbound variable usage. Use log.warning instead of deprecated log.warn. --- can/interfaces/ixxat/canlib_vcinpl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index aaff22f6d..71ec5a75f 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -646,7 +646,7 @@ def _recv_internal(self, timeout): recv_function = functools.partial(_canlib.canChannelPeekMessage, self._channel_handle, ctypes.byref(self._message)) else: # Wait if no message available - timeout = constants.INFINITE if (timeout is None or timeout < 0) else int(timeout * 1000) + timeout_ms = constants.INFINITE if (timeout is None or timeout < 0) else int(timeout * 1000) recv_function = functools.partial(_canlib.canChannelReadMessage, self._channel_handle, timeout_ms, ctypes.byref(self._message)) try: @@ -679,7 +679,7 @@ def _recv_internal(self, timeout): elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_TIMEOVR: pass else: - log.warn("Unexpected message info type 0x%X", self._message.uMsgInfo.Bits.type) + log.warning("Unexpected message info type 0x%X", self._message.uMsgInfo.Bits.type) finally: # Check hard errors status = structures.CANLINESTATUS() From 5d4e673678346cb19d81dcdb92ed881d7fd0c310 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Fri, 15 Oct 2021 11:43:16 +0200 Subject: [PATCH 09/26] Now wrapping IXXAT VCI v4 --- can/interfaces/ixxat/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/interfaces/ixxat/__init__.py b/can/interfaces/ixxat/__init__.py index 49399caac..1419d97a1 100644 --- a/can/interfaces/ixxat/__init__.py +++ b/can/interfaces/ixxat/__init__.py @@ -1,7 +1,7 @@ """ Ctypes wrapper module for IXXAT Virtual CAN Interface V4 on win32 systems -Copyright (C) 2016 Giuseppe Corbelli +Copyright (C) 2016-2021 Giuseppe Corbelli """ from can.interfaces.ixxat.canlib import IXXATBus From 48ed790ca5ad4ab82aec56ffacd18993a1487f75 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 18 Oct 2021 10:32:46 +0200 Subject: [PATCH 10/26] Added CAN_OPMODE_AUTOBAUD controller operating mode. --- can/interfaces/ixxat/constants.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/can/interfaces/ixxat/constants.py b/can/interfaces/ixxat/constants.py index 6866be11d..b5b993e39 100644 --- a/can/interfaces/ixxat/constants.py +++ b/can/interfaces/ixxat/constants.py @@ -114,12 +114,13 @@ CAN_STATUS_BUSCERR = 0x20 # bus coupling error # Controller operating modes -CAN_OPMODE_UNDEFINED = 0x00 -CAN_OPMODE_STANDARD = 0x01 -CAN_OPMODE_EXTENDED = 0x02 -CAN_OPMODE_ERRFRAME = 0x04 -CAN_OPMODE_LISTONLY = 0x08 -CAN_OPMODE_LOWSPEED = 0x10 +CAN_OPMODE_UNDEFINED = 0x00 # undefined +CAN_OPMODE_STANDARD = 0x01 # reception of 11-bit id messages +CAN_OPMODE_EXTENDED = 0x02 # reception of 29-bit id messages +CAN_OPMODE_ERRFRAME = 0x04 # reception of error frames +CAN_OPMODE_LISTONLY = 0x08 # listen only mode (TX passive) +CAN_OPMODE_LOWSPEED = 0x10 # use low speed bus interface +CAN_OPMODE_AUTOBAUD = 0x20 # automatic bit rate detection # Extended operating modes CAN_EXMODE_DISABLED = 0x00 From a2ab2c087e7d68e40f794e2b8e59bf9a3acdf515 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 18 Oct 2021 10:33:13 +0200 Subject: [PATCH 11/26] Added a few structure comments --- can/interfaces/ixxat/structures.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/can/interfaces/ixxat/structures.py b/can/interfaces/ixxat/structures.py index 222a391f1..cefcc0752 100644 --- a/can/interfaces/ixxat/structures.py +++ b/can/interfaces/ixxat/structures.py @@ -70,11 +70,13 @@ def __str__(self): class CANLINESTATUS(ctypes.Structure): _fields_ = [ + # current CAN operating mode. Value is a logical combination of + # one or more CAN_OPMODE_xxx constants ("bOpMode", ctypes.c_uint8), - ("bBtReg0", ctypes.c_uint8), - ("bBtReg1", ctypes.c_uint8), - ("bBusLoad", ctypes.c_uint8), - ("dwStatus", ctypes.c_uint32), + ("bBtReg0", ctypes.c_uint8), # current bus timing register 0 value + ("bBtReg1", ctypes.c_uint8), # current bus timing register 1 value + ("bBusLoad", ctypes.c_uint8), # average bus load in percent (0..100) + ("dwStatus", ctypes.c_uint32), # status of the CAN controller (see CAN_STATUS_) ] @@ -83,11 +85,11 @@ class CANLINESTATUS(ctypes.Structure): class CANCHANSTATUS(ctypes.Structure): _fields_ = [ - ("sLineStatus", CANLINESTATUS), - ("fActivated", ctypes.c_uint32), - ("fRxOverrun", ctypes.c_uint32), - ("bRxFifoLoad", ctypes.c_uint8), - ("bTxFifoLoad", ctypes.c_uint8), + ("sLineStatus", CANLINESTATUS), # current CAN line status + ("fActivated", ctypes.c_uint32), # TRUE if the channel is activated + ("fRxOverrun", ctypes.c_uint32), # TRUE if receive FIFO overrun occurred + ("bRxFifoLoad", ctypes.c_uint8), # receive FIFO load in percent (0..100) + ("bTxFifoLoad", ctypes.c_uint8), # transmit FIFO load in percent (0..100) ] From d922cda28491f381bc42f5a932d6d4fcf84b52d2 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:30:58 +0100 Subject: [PATCH 12/26] Mapped symbol canChannelGetStatus, used to implement the 'state' property. Hardware error checking in _recv_internal() handles BUS OFF situation. --- can/interfaces/ixxat/canlib.py | 10 ++++++++ can/interfaces/ixxat/canlib_vcinpl.py | 33 ++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/can/interfaces/ixxat/canlib.py b/can/interfaces/ixxat/canlib.py index ece90357c..5092cde7b 100644 --- a/can/interfaces/ixxat/canlib.py +++ b/can/interfaces/ixxat/canlib.py @@ -2,6 +2,7 @@ import can.interfaces.ixxat.canlib_vcinpl2 as vcinpl2 from can import BusABC, Message +from can.bus import BusState from typing import Optional @@ -146,3 +147,12 @@ def _send_periodic_internal(self, msg, period, duration=None): def shutdown(self): return self.bus.shutdown() + + @property + def state(self) -> BusState: + """ + Return the current state of the hardware + """ + return self.bus.state + +# ~class IXXATBus(BusABC): --------------------------------------------------- diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index 71ec5a75f..12e456b56 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -16,6 +16,7 @@ from typing import Optional, Callable, Tuple from can import BusABC, Message +from can.bus import BusState from can.exceptions import CanInterfaceNotImplementedError, CanInitializationError from can.broadcastmanager import ( LimitedDurationCyclicSendTaskABC, @@ -38,7 +39,6 @@ log = logging.getLogger("can.ixxat") -from time import perf_counter # Hack to have vciFormatError as a free function, see below vciFormatError = None @@ -225,6 +225,13 @@ def __check_status(result, function, args): (HANDLE, ctypes.c_uint32, structures.PCANMSG), __check_status, ) + # HRESULT canChannelGetStatus (HANDLE hCanChn, PCANCHANSTATUS pStatus ); + _canlib.map_symbol( + "canChannelGetStatus", + ctypes.c_long, + (HANDLE, structures.PCANCHANSTATUS), + __check_status + ) # EXTERN_C HRESULT VCIAPI canControlOpen( IN HANDLE hDevice, IN UINT32 dwCanNo, OUT PHANDLE phCanCtl ); _canlib.map_symbol( @@ -767,9 +774,33 @@ def shutdown(self): _canlib.canSchedulerClose(self._scheduler) _canlib.canChannelClose(self._channel_handle) _canlib.canControlStart(self._control_handle, constants.FALSE) + _canlib.canControlReset(self._control_handle) _canlib.canControlClose(self._control_handle) _canlib.vciDeviceClose(self._device_handle) + @property + def state(self) -> BusState: + """ + Return the current state of the hardware + """ + status = structures.CANLINESTATUS() + _canlib.canControlGetStatus(self._control_handle, ctypes.byref(status)) + if status.bOpMode == constants.CAN_OPMODE_LISTONLY: + return BusState.PASSIVE + + error_byte_1 = status.dwStatus & 0x0F + # CAN_STATUS_BUSOFF = 0x08 # bus off status + if error_byte_1 & constants.CAN_STATUS_BUSOFF: + return BusState.ERROR + + error_byte_2 = status.dwStatus & 0xF0 + # CAN_STATUS_BUSCERR = 0x20 # bus coupling error + if error_byte_2 & constants.CAN_STATUS_BUSCERR: + raise BusState.ERROR + + return BusState.ACTIVE +# ~class IXXATBus(BusABC): --------------------------------------------------- + class CyclicSendTask(LimitedDurationCyclicSendTaskABC, RestartableCyclicTaskABC): """A message in the cyclic transmit list.""" From 347962fab60c8a0fd6449114a561860573aad821 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:32:43 +0100 Subject: [PATCH 13/26] Use CANMSGINFO.bAddFlags instead of CANMSGINFO.bFlags2 --- can/interfaces/ixxat/canlib_vcinpl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index 12e456b56..f81222a96 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -679,8 +679,8 @@ def _recv_internal(self, timeout): "Unknown CAN error message code {}".format(self._message.abData[0]) )) log.warning( - "CAN message flags bFlags2 0x%02X bflags 0x%02X", - self._message.uMsgInfo.Bytes.bFlags2, + "CAN message flags bAddFlags/bFlags2 0x%02X bflags 0x%02X", + self._message.uMsgInfo.Bytes.bAddFlags, self._message.uMsgInfo.Bytes.bFlags ) elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_TIMEOVR: From c16a5deb702cdd342dad70e28a18992b81d71fdc Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:41:36 +0100 Subject: [PATCH 14/26] Format code with black --- can/interfaces/ixxat/canlib.py | 1 - can/interfaces/ixxat/canlib_vcinpl.py | 84 ++++++++++++++++++--------- can/interfaces/ixxat/constants.py | 3 +- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/can/interfaces/ixxat/canlib.py b/can/interfaces/ixxat/canlib.py index 5092cde7b..84644f583 100644 --- a/can/interfaces/ixxat/canlib.py +++ b/can/interfaces/ixxat/canlib.py @@ -154,5 +154,4 @@ def state(self) -> BusState: Return the current state of the hardware """ return self.bus.state - # ~class IXXATBus(BusABC): --------------------------------------------------- diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index f81222a96..f37880861 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -650,11 +650,24 @@ def _recv_internal(self, timeout): if self._inWaiting() or timeout == 0: # Peek without waiting - recv_function = functools.partial(_canlib.canChannelPeekMessage, self._channel_handle, ctypes.byref(self._message)) + recv_function = functools.partial( + _canlib.canChannelPeekMessage, + self._channel_handle, + ctypes.byref(self._message), + ) else: # Wait if no message available - timeout_ms = constants.INFINITE if (timeout is None or timeout < 0) else int(timeout * 1000) - recv_function = functools.partial(_canlib.canChannelReadMessage, self._channel_handle, timeout_ms, ctypes.byref(self._message)) + timeout = ( + constants.INFINITE + if (timeout is None or timeout < 0) + else int(timeout * 1000) + ) + recv_function = functools.partial( + _canlib.canChannelReadMessage, + self._channel_handle, + timeout, + ctypes.byref(self._message), + ) try: recv_function() @@ -666,18 +679,26 @@ def _recv_internal(self, timeout): if self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_DATA: data_received = True elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_INFO: - log.info(CAN_INFO_MESSAGES.get( - self._message.abData[0], - "Unknown CAN info message code {}".format(self._message.abData[0]) - )) + log.info( + CAN_INFO_MESSAGES.get( + self._message.abData[0], + "Unknown CAN info message code {}".format( + self._message.abData[0] + ), + ) + ) elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_ERROR: if self._message.uMsgInfo.Bytes.bFlags & constants.CAN_MSGFLAGS_OVR: log.warning("CAN error: data overrun") else: - log.warning(CAN_ERROR_MESSAGES.get( - self._message.abData[0], - "Unknown CAN error message code {}".format(self._message.abData[0]) - )) + log.warning( + CAN_ERROR_MESSAGES.get( + self._message.abData[0], + "Unknown CAN error message code {}".format( + self._message.abData[0] + ), + ) + ) log.warning( "CAN message flags bAddFlags/bFlags2 0x%02X bflags 0x%02X", self._message.uMsgInfo.Bytes.bAddFlags, @@ -686,24 +707,31 @@ def _recv_internal(self, timeout): elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_TIMEOVR: pass else: - log.warning("Unexpected message info type 0x%X", self._message.uMsgInfo.Bits.type) + log.warning( + "Unexpected message info type 0x%X", + self._message.uMsgInfo.Bits.type, + ) finally: - # Check hard errors - status = structures.CANLINESTATUS() - _canlib.canControlGetStatus(self._control_handle, ctypes.byref(status)) - error_byte_1 = status.dwStatus & 0x0F - error_byte_2 = status.dwStatus & 0xF0 - if error_byte_1 > constants.CAN_STATUS_TXPEND: - # CAN_STATUS_OVRRUN = 0x02 # data overrun occurred - # CAN_STATUS_ERRLIM = 0x04 # error warning limit exceeded - if error_byte_1 & constants.CAN_STATUS_OVRRUN: - raise VCIError("Data overrun occurred") - elif error_byte_1 & constants.CAN_STATUS_ERRLIM: - raise VCIError("Error warning limit exceeded") - elif error_byte_2 > constants.CAN_STATUS_ININIT: - # CAN_STATUS_BUSCERR = 0x20 # bus coupling error - if error_byte_2 & constants.CAN_STATUS_BUSCERR: - raise VCIError("Bus coupling error") + if not data_received: + # Check hard errors + status = structures.CANLINESTATUS() + _canlib.canControlGetStatus(self._control_handle, ctypes.byref(status)) + error_byte_1 = status.dwStatus & 0x0F + error_byte_2 = status.dwStatus & 0xF0 + if error_byte_1 > constants.CAN_STATUS_TXPEND: + # CAN_STATUS_OVRRUN = 0x02 # data overrun occurred + # CAN_STATUS_ERRLIM = 0x04 # error warning limit exceeded + # CAN_STATUS_BUSOFF = 0x08 # bus off status + if error_byte_1 & constants.CAN_STATUS_OVRRUN: + raise VCIError("Data overrun occurred") + elif error_byte_1 & constants.CAN_STATUS_ERRLIM: + raise VCIError("Error warning limit exceeded") + elif error_byte_1 & constants.CAN_STATUS_BUSOFF: + raise VCIError("Bus off status") + elif error_byte_2 > constants.CAN_STATUS_ININIT: + # CAN_STATUS_BUSCERR = 0x20 # bus coupling error + if error_byte_2 & constants.CAN_STATUS_BUSCERR: + raise VCIError("Bus coupling error") if not data_received: # Timed out / can message type is not DATA diff --git a/can/interfaces/ixxat/constants.py b/can/interfaces/ixxat/constants.py index b5b993e39..b679f4acb 100644 --- a/can/interfaces/ixxat/constants.py +++ b/can/interfaces/ixxat/constants.py @@ -175,7 +175,6 @@ CAN_MSGFLAGS_RTR = 0x40 # [bit 6] remote transmission request CAN_MSGFLAGS_EXT = 0x80 # [bit 7] frame format (0=11-bit, 1=29-bit) - # extended message information flags (used by ) CAN_MSGFLAGS2_SSM = 0x01 # [bit 0] single shot mode CAN_MSGFLAGS2_HPM = 0x02 # [bit 1] high priority message @@ -258,3 +257,5 @@ dwMode=0, dwBPS=10000000, wTS1=300, wTS2=100, wSJW=100, wTDO=200 ), # SP = 75,0% } +======= +>>>>>>> f22f775 (Format code with black) From 97e4cf96cfeba8ed30acb802993f450011301d0b Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 09:43:58 +0100 Subject: [PATCH 15/26] Call canControlClose() AFTER canControlReset() or it will always fail --- can/interfaces/ixxat/canlib_vcinpl.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index f37880861..d77462ad5 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -780,9 +780,10 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None: _canlib.canChannelSendMessage( self._channel_handle, int(timeout * 1000), message ) - else: _canlib.canChannelPostMessage(self._channel_handle, message) + # Want to log outgoing messages? + # log.log(self.RECV_LOGGING_LEVEL, "Sent: %s", message) def _send_periodic_internal(self, msg, period, duration=None): """Send a message using built-in cyclic transmit list functionality.""" From 906e08c600c6315eb2d8c7cb6c53765c8f9c0369 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Thu, 9 Dec 2021 08:43:06 +0100 Subject: [PATCH 16/26] Added CANMSG.__str__ --- can/interfaces/ixxat/structures.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/can/interfaces/ixxat/structures.py b/can/interfaces/ixxat/structures.py index cefcc0752..28766fff3 100644 --- a/can/interfaces/ixxat/structures.py +++ b/can/interfaces/ixxat/structures.py @@ -4,6 +4,7 @@ Copyright (C) 2016 Giuseppe Corbelli """ +import binascii import ctypes @@ -155,10 +156,15 @@ class Bits(ctypes.Structure): class CANMSG(ctypes.Structure): _fields_ = [ ("dwTime", ctypes.c_uint32), + # CAN ID of the message in Intel format (aligned right) without RTR bit. ("dwMsgId", ctypes.c_uint32), ("uMsgInfo", CANMSGINFO), ("abData", ctypes.c_uint8 * 8), ] + def __str__(self): + return """ID: 0x{0:04x}{1} DLC: {2:02d} DATA: {3}""".format( + self.dwMsgId, "[RTR]" if self.uMsgInfo.Bits.rtr else "", self.uMsgInfo.Bits.dlc, binascii.hexlify(self.abData, ' ') + ) PCANMSG = ctypes.POINTER(CANMSG) From 1ce549a848140fe639f389fd5e38ab47152130ef Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Fri, 10 Dec 2021 14:32:23 +0100 Subject: [PATCH 17/26] Removed conflict marker --- can/interfaces/ixxat/constants.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/can/interfaces/ixxat/constants.py b/can/interfaces/ixxat/constants.py index b679f4acb..3bc1aa42e 100644 --- a/can/interfaces/ixxat/constants.py +++ b/can/interfaces/ixxat/constants.py @@ -257,5 +257,3 @@ dwMode=0, dwBPS=10000000, wTS1=300, wTS2=100, wSJW=100, wTDO=200 ), # SP = 75,0% } -======= ->>>>>>> f22f775 (Format code with black) From 1d4055b76276adce226c748ea44c8f92f57a49d1 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Fri, 10 Dec 2021 14:33:03 +0100 Subject: [PATCH 18/26] A few changes suggested by pylint --- can/interfaces/ixxat/canlib.py | 5 +++-- can/interfaces/ixxat/canlib_vcinpl.py | 20 ++++++++++---------- can/interfaces/ixxat/structures.py | 8 ++++++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/can/interfaces/ixxat/canlib.py b/can/interfaces/ixxat/canlib.py index 84644f583..2ad9d6c1d 100644 --- a/can/interfaces/ixxat/canlib.py +++ b/can/interfaces/ixxat/canlib.py @@ -13,7 +13,8 @@ class IXXATBus(BusABC): Based on the C implementation of IXXAT, two different dlls are provided by IXXAT, one to work with CAN, the other with CAN-FD. - This class only delegates to related implementation (in calib_vcinpl or canlib_vcinpl2) class depending on fd user option. + This class only delegates to related implementation (in calib_vcinpl or canlib_vcinpl2) + class depending on fd user option. """ def __init__( @@ -142,7 +143,7 @@ def _recv_internal(self, timeout): def send(self, msg: Message, timeout: Optional[float] = None) -> None: return self.bus.send(msg, timeout) - def _send_periodic_internal(self, msg, period, duration=None): + def _send_periodic_internal(self, msgs, period, duration=None): return self.bus._send_periodic_internal(msg, period, duration) def shutdown(self): diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index d77462ad5..c07584c08 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -518,11 +518,11 @@ def __init__( == bytes(unique_hardware_id, "ascii") ): break - else: - log.debug( - "Ignoring IXXAT with hardware id '%s'.", - self._device_info.UniqueHardwareId.AsChar.decode("ascii"), - ) + + log.debug( + "Ignoring IXXAT with hardware id '%s'.", + self._device_info.UniqueHardwareId.AsChar.decode("ascii"), + ) _canlib.vciEnumDeviceClose(self._device_handle) try: @@ -531,7 +531,7 @@ def __init__( ctypes.byref(self._device_handle), ) except Exception as exception: - raise CanInitializationError(f"Could not open device: {exception}") + raise CanInitializationError(f"Could not open device: {exception}") from exception log.info("Using unique HW ID %s", self._device_info.UniqueHardwareId.AsChar) @@ -552,7 +552,7 @@ def __init__( except Exception as exception: raise CanInitializationError( f"Could not open and initialize channel: {exception}" - ) + ) from exception # Signal TX/RX events when at least one frame has been handled _canlib.canChannelInitialize( @@ -671,7 +671,7 @@ def _recv_internal(self, timeout): try: recv_function() - except (VCITimeout, VCIRxQueueEmptyError) as e: + except (VCITimeout, VCIRxQueueEmptyError): # Ignore the 2 errors, overall timeout is handled by BusABC.recv pass else: @@ -785,7 +785,7 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None: # Want to log outgoing messages? # log.log(self.RECV_LOGGING_LEVEL, "Sent: %s", message) - def _send_periodic_internal(self, msg, period, duration=None): + def _send_periodic_internal(self, msgs, period, duration=None): """Send a message using built-in cyclic transmit list functionality.""" if self._scheduler is None: self._scheduler = HANDLE() @@ -795,7 +795,7 @@ def _send_periodic_internal(self, msg, period, duration=None): self._scheduler_resolution = caps.dwClockFreq / caps.dwCmsDivisor _canlib.canSchedulerActivate(self._scheduler, constants.TRUE) return CyclicSendTask( - self._scheduler, msg, period, duration, self._scheduler_resolution + self._scheduler, msgs, period, duration, self._scheduler_resolution ) def shutdown(self): diff --git a/can/interfaces/ixxat/structures.py b/can/interfaces/ixxat/structures.py index 28766fff3..75f8643ad 100644 --- a/can/interfaces/ixxat/structures.py +++ b/can/interfaces/ixxat/structures.py @@ -161,9 +161,13 @@ class CANMSG(ctypes.Structure): ("uMsgInfo", CANMSGINFO), ("abData", ctypes.c_uint8 * 8), ] - def __str__(self): + + def __str__(self) -> str: return """ID: 0x{0:04x}{1} DLC: {2:02d} DATA: {3}""".format( - self.dwMsgId, "[RTR]" if self.uMsgInfo.Bits.rtr else "", self.uMsgInfo.Bits.dlc, binascii.hexlify(self.abData, ' ') + self.dwMsgId, + "[RTR]" if self.uMsgInfo.Bits.rtr else "", + self.uMsgInfo.Bits.dlc, + binascii.hexlify(self.abData, ' ') ) From 0e844d85562f630b4152131cddeff634d4090d42 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Fri, 10 Dec 2021 13:34:35 +0000 Subject: [PATCH 19/26] Format code with black --- can/interfaces/ixxat/canlib.py | 2 ++ can/interfaces/ixxat/canlib_vcinpl.py | 10 +++++++--- can/interfaces/ixxat/structures.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/can/interfaces/ixxat/canlib.py b/can/interfaces/ixxat/canlib.py index 2ad9d6c1d..fa088f89a 100644 --- a/can/interfaces/ixxat/canlib.py +++ b/can/interfaces/ixxat/canlib.py @@ -155,4 +155,6 @@ def state(self) -> BusState: Return the current state of the hardware """ return self.bus.state + + # ~class IXXATBus(BusABC): --------------------------------------------------- diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index c07584c08..6b3ffe137 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -230,7 +230,7 @@ def __check_status(result, function, args): "canChannelGetStatus", ctypes.c_long, (HANDLE, structures.PCANCHANSTATUS), - __check_status + __check_status, ) # EXTERN_C HRESULT VCIAPI canControlOpen( IN HANDLE hDevice, IN UINT32 dwCanNo, OUT PHANDLE phCanCtl ); @@ -531,7 +531,9 @@ def __init__( ctypes.byref(self._device_handle), ) except Exception as exception: - raise CanInitializationError(f"Could not open device: {exception}") from exception + raise CanInitializationError( + f"Could not open device: {exception}" + ) from exception log.info("Using unique HW ID %s", self._device_info.UniqueHardwareId.AsChar) @@ -702,7 +704,7 @@ def _recv_internal(self, timeout): log.warning( "CAN message flags bAddFlags/bFlags2 0x%02X bflags 0x%02X", self._message.uMsgInfo.Bytes.bAddFlags, - self._message.uMsgInfo.Bytes.bFlags + self._message.uMsgInfo.Bytes.bFlags, ) elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_TIMEOVR: pass @@ -828,6 +830,8 @@ def state(self) -> BusState: raise BusState.ERROR return BusState.ACTIVE + + # ~class IXXATBus(BusABC): --------------------------------------------------- diff --git a/can/interfaces/ixxat/structures.py b/can/interfaces/ixxat/structures.py index 75f8643ad..c69902269 100644 --- a/can/interfaces/ixxat/structures.py +++ b/can/interfaces/ixxat/structures.py @@ -167,7 +167,7 @@ def __str__(self) -> str: self.dwMsgId, "[RTR]" if self.uMsgInfo.Bits.rtr else "", self.uMsgInfo.Bits.dlc, - binascii.hexlify(self.abData, ' ') + binascii.hexlify(self.abData, " "), ) From ab43a8173b865b03ffbddffed165f0126ef3fdda Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 20 Dec 2021 08:33:30 +0100 Subject: [PATCH 20/26] Renamed parameter 'msg' to 'msgs' in _send_periodic_internal(), consistent with BusABC --- can/interfaces/ixxat/canlib.py | 2 +- can/interfaces/ixxat/canlib_vcinpl2.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/can/interfaces/ixxat/canlib.py b/can/interfaces/ixxat/canlib.py index fa088f89a..1e4055f89 100644 --- a/can/interfaces/ixxat/canlib.py +++ b/can/interfaces/ixxat/canlib.py @@ -144,7 +144,7 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None: return self.bus.send(msg, timeout) def _send_periodic_internal(self, msgs, period, duration=None): - return self.bus._send_periodic_internal(msg, period, duration) + return self.bus._send_periodic_internal(msgs, period, duration) def shutdown(self): return self.bus.shutdown() diff --git a/can/interfaces/ixxat/canlib_vcinpl2.py b/can/interfaces/ixxat/canlib_vcinpl2.py index 37085d74a..ebc0a9c4a 100644 --- a/can/interfaces/ixxat/canlib_vcinpl2.py +++ b/can/interfaces/ixxat/canlib_vcinpl2.py @@ -934,7 +934,7 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None: else: _canlib.canChannelPostMessage(self._channel_handle, message) - def _send_periodic_internal(self, msg, period, duration=None): + def _send_periodic_internal(self, msgs, period, duration=None): """Send a message using built-in cyclic transmit list functionality.""" if self._scheduler is None: self._scheduler = HANDLE() @@ -946,7 +946,7 @@ def _send_periodic_internal(self, msg, period, duration=None): ) # TODO: confirm _canlib.canSchedulerActivate(self._scheduler, constants.TRUE) return CyclicSendTask( - self._scheduler, msg, period, duration, self._scheduler_resolution + self._scheduler, msgs, period, duration, self._scheduler_resolution ) def shutdown(self): From a863d1248470096e6a11be3d1bb0402a9aa66435 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 20 Dec 2021 08:44:12 +0100 Subject: [PATCH 21/26] Changed plain format() calls to f-strings as per #1141 review --- can/interfaces/ixxat/canlib_vcinpl.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index 6b3ffe137..d713f3b1c 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -684,9 +684,7 @@ def _recv_internal(self, timeout): log.info( CAN_INFO_MESSAGES.get( self._message.abData[0], - "Unknown CAN info message code {}".format( - self._message.abData[0] - ), + f"Unknown CAN info message code {self._message.abData[0]}", ) ) elif self._message.uMsgInfo.Bits.type == constants.CAN_MSGTYPE_ERROR: @@ -696,9 +694,7 @@ def _recv_internal(self, timeout): log.warning( CAN_ERROR_MESSAGES.get( self._message.abData[0], - "Unknown CAN error message code {}".format( - self._message.abData[0] - ), + f"Unknown CAN error message code {self._message.abData[0]}" ) ) log.warning( From 4609b11a461c9d0f6f3e743c52c6f662e924625c Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 20 Dec 2021 08:55:14 +0100 Subject: [PATCH 22/26] Removed binascii module dependency using memoryview --- can/interfaces/ixxat/structures.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/can/interfaces/ixxat/structures.py b/can/interfaces/ixxat/structures.py index c69902269..f6553cdb3 100644 --- a/can/interfaces/ixxat/structures.py +++ b/can/interfaces/ixxat/structures.py @@ -4,7 +4,6 @@ Copyright (C) 2016 Giuseppe Corbelli """ -import binascii import ctypes @@ -167,7 +166,7 @@ def __str__(self) -> str: self.dwMsgId, "[RTR]" if self.uMsgInfo.Bits.rtr else "", self.uMsgInfo.Bits.dlc, - binascii.hexlify(self.abData, " "), + memoryview(self.abData)[:self.uMsgInfo.Bits.dlc].hex(sep=" "), ) From 0ac4322aa233573d0784ace403bf37ea591783ad Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 20 Dec 2021 09:09:41 +0100 Subject: [PATCH 23/26] Revert "Try to dlsym() the required functions only if the canlib has been loaded" This reverts commit ad11bf957f9335fc7c9add620e18161e445f14f3. --- can/interfaces/kvaser/canlib.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index 53b7fec7d..a951e39de 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -319,21 +319,6 @@ def __check_bus_handle_validity(handle, function, arguments): errcheck=__check_status_operation, ) - kvFlashLeds = __get_canlib_function( - "kvFlashLeds", - argtypes=[c_canHandle, ctypes.c_int, ctypes.c_int], - restype=ctypes.c_short, - errcheck=__check_status, - ) - - if sys.platform == "win32": - canGetVersionEx = __get_canlib_function( - "canGetVersionEx", - argtypes=[ctypes.c_uint], - restype=ctypes.c_uint, - errcheck=__check_status, - ) - def init_kvaser_library(): if __canlib is not None: @@ -345,16 +330,6 @@ def init_kvaser_library(): log.warning("Kvaser canlib could not be initialized.") -def init_kvaser_library(): - if __canlib is not None: - try: - log.debug("Initializing Kvaser CAN library") - canInitializeLibrary() - log.debug("CAN library initialized") - except: - log.warning("Kvaser canlib could not be initialized.") - - DRIVER_MODE_SILENT = False DRIVER_MODE_NORMAL = True From 9b4e5287b6fbad49a1be8b4822e2e7ada741ccbd Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 20 Dec 2021 08:12:39 +0000 Subject: [PATCH 24/26] Format code with black --- can/interfaces/ixxat/canlib_vcinpl.py | 2 +- can/interfaces/ixxat/structures.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index d713f3b1c..333b4ae3c 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -694,7 +694,7 @@ def _recv_internal(self, timeout): log.warning( CAN_ERROR_MESSAGES.get( self._message.abData[0], - f"Unknown CAN error message code {self._message.abData[0]}" + f"Unknown CAN error message code {self._message.abData[0]}", ) ) log.warning( diff --git a/can/interfaces/ixxat/structures.py b/can/interfaces/ixxat/structures.py index f6553cdb3..b784437e0 100644 --- a/can/interfaces/ixxat/structures.py +++ b/can/interfaces/ixxat/structures.py @@ -166,7 +166,7 @@ def __str__(self) -> str: self.dwMsgId, "[RTR]" if self.uMsgInfo.Bits.rtr else "", self.uMsgInfo.Bits.dlc, - memoryview(self.abData)[:self.uMsgInfo.Bits.dlc].hex(sep=" "), + memoryview(self.abData)[: self.uMsgInfo.Bits.dlc].hex(sep=" "), ) From a018b632ee07ad88d1e359a8695687d586688bfb Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 20 Dec 2021 09:37:46 +0100 Subject: [PATCH 25/26] Changed plain format() calls to f-strings as per #1141 review --- can/interfaces/ixxat/canlib_vcinpl2.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/can/interfaces/ixxat/canlib_vcinpl2.py b/can/interfaces/ixxat/canlib_vcinpl2.py index ebc0a9c4a..58b367418 100644 --- a/can/interfaces/ixxat/canlib_vcinpl2.py +++ b/can/interfaces/ixxat/canlib_vcinpl2.py @@ -826,9 +826,7 @@ def _recv_internal(self, timeout): log.info( CAN_INFO_MESSAGES.get( self._message.abData[0], - "Unknown CAN info message code {}".format( - self._message.abData[0] - ), + f"Unknown CAN info message code {self._message.abData[0]}" ) ) @@ -838,9 +836,7 @@ def _recv_internal(self, timeout): log.warning( CAN_ERROR_MESSAGES.get( self._message.abData[0], - "Unknown CAN error message code {}".format( - self._message.abData[0] - ), + f"Unknown CAN error message code {self._message.abData[0]}" ) ) From 3a021a722b8ac022d297b114d9fd6a25be82971d Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Mon, 20 Dec 2021 08:39:07 +0000 Subject: [PATCH 26/26] Format code with black --- can/interfaces/ixxat/canlib_vcinpl2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/can/interfaces/ixxat/canlib_vcinpl2.py b/can/interfaces/ixxat/canlib_vcinpl2.py index 58b367418..d92743657 100644 --- a/can/interfaces/ixxat/canlib_vcinpl2.py +++ b/can/interfaces/ixxat/canlib_vcinpl2.py @@ -826,7 +826,7 @@ def _recv_internal(self, timeout): log.info( CAN_INFO_MESSAGES.get( self._message.abData[0], - f"Unknown CAN info message code {self._message.abData[0]}" + f"Unknown CAN info message code {self._message.abData[0]}", ) ) @@ -836,7 +836,7 @@ def _recv_internal(self, timeout): log.warning( CAN_ERROR_MESSAGES.get( self._message.abData[0], - f"Unknown CAN error message code {self._message.abData[0]}" + f"Unknown CAN error message code {self._message.abData[0]}", ) )