Skip to content

Commit 81c0edc

Browse files
Move exception to exceptions.py
1 parent 91db318 commit 81c0edc

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

can/interfaces/vector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .canlib import VectorBus
2-
from .vxlapi import VectorError
2+
from .exceptions import VectorError

can/interfaces/vector/canlib.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@
1313
# Import Modules
1414
# ==============
1515
from can import BusABC, Message
16+
from .exceptions import VectorError
1617

1718
# Define Module Logger
1819
# ====================
1920
LOG = logging.getLogger(__name__)
2021

2122
# Import safely Vector API module for Travis tests
22-
if sys.platform == 'win32':
23-
try:
24-
from . import vxlapi
25-
except Exception as exc:
26-
LOG.error('Could not import vxlapi: %s', exc)
27-
vxlapi = None
28-
else:
29-
LOG.warning('Vector API does not work on %s platform', sys.platform)
30-
vxlapi = None
23+
vxlapi = None
24+
try:
25+
from . import vxlapi
26+
except Exception as exc:
27+
LOG.warning('Could not import vxlapi: %s', exc)
3128

3229

3330
class VectorBus(BusABC):
@@ -93,7 +90,7 @@ def __init__(self, channel, can_filters=None, poll_interval=0.01,
9390
try:
9491
vxlapi.xlActivateChannel(self.port_handle, self.mask,
9592
vxlapi.XL_BUS_TYPE_CAN, 0)
96-
except vxlapi.VectorError:
93+
except VectorError:
9794
self.shutdown()
9895
raise
9996
# Calculate time offset for absolute timestamps
@@ -114,7 +111,7 @@ def set_filters(self, can_filters=None):
114111
self.port_handle, self.mask,
115112
can_filter["can_id"], can_filter["can_mask"],
116113
vxlapi.XL_CAN_EXT if can_filter.get("extended") else vxlapi.XL_CAN_STD)
117-
except vxlapi.VectorError as exc:
114+
except VectorError as exc:
118115
LOG.warning("Could not set filters: %s", exc)
119116
else:
120117
LOG.warning("Only one filter per extended or standard ID allowed")
@@ -126,7 +123,7 @@ def recv(self, timeout=None):
126123
event_count = ctypes.c_uint(1)
127124
try:
128125
vxlapi.xlReceive(self.port_handle, event_count, event)
129-
except vxlapi.VectorError as exc:
126+
except VectorError as exc:
130127
if exc.error_code != vxlapi.XL_ERR_QUEUE_IS_EMPTY:
131128
raise
132129
else:

can/interfaces/vector/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from can import CanError
2+
3+
4+
class VectorError(CanError):
5+
6+
def __init__(self, error_code, error_string):
7+
self.error_code = error_code
8+
super(VectorError, self).__init__(error_string)

can/interfaces/vector/vxlapi.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import ctypes
99
import logging
1010
import platform
11-
from can import CanError
11+
from .exceptions import VectorError
1212

1313
# Define Module Logger
1414
# ====================
@@ -17,12 +17,8 @@
1717
# Vector XL API Definitions
1818
# =========================
1919
# Load Windows DLL
20-
_xlapi_dll = None
21-
dll_name = 'vxlapi64' if platform.architecture()[0] == '64bit' else 'vxlapi'
22-
try:
23-
_xlapi_dll = ctypes.windll.LoadLibrary(dll_name)
24-
except OSError:
25-
raise ImportError('Cannot load Dynamic XL Driver Library')
20+
DLL_NAME = 'vxlapi64' if platform.architecture()[0] == '64bit' else 'vxlapi'
21+
_xlapi_dll = ctypes.windll.LoadLibrary(DLL_NAME)
2622

2723
XL_BUS_TYPE_CAN = 0x00000001
2824

@@ -73,20 +69,9 @@ class XLevent(ctypes.Structure):
7369
XLportHandle = ctypes.c_long
7470

7571

76-
class VectorError(CanError):
77-
78-
def __init__(self, function, error_code):
79-
self.function = function
80-
self.error_code = error_code
81-
82-
def __str__(self):
83-
error_string = xlGetErrorString(self.error_code).decode()
84-
return "Function %s failed: %s" % (self.function.__name__, error_string)
85-
86-
8772
def check_status(result, function, arguments):
8873
if result > 0:
89-
raise VectorError(function, result)
74+
raise VectorError(result, xlGetErrorString(result).decode())
9075
return result
9176

9277

0 commit comments

Comments
 (0)