13
13
import time
14
14
import struct
15
15
16
- from can import CanError , BusABC , Message
16
+ from can import BusABC , Message
17
+ from ..exceptions import CanInitializationError , CanOperationError
18
+
17
19
18
20
logger = logging .getLogger (__name__ )
19
21
28
30
database ,
29
31
XnetError ,
30
32
)
31
- except ImportError :
32
- logger .error ("Error, NIXNET python module cannot be loaded." )
33
- raise ImportError ()
33
+ except ImportError as error :
34
+ raise ImportError ("Error, NIXNET python module cannot be loaded" ) from error
34
35
else :
35
- logger .error ("NI-XNET interface is only available on Windows systems" )
36
- raise NotImplementedError ("NiXNET is not supported on not Win32 platforms" )
36
+ raise NotImplementedError ("NiXNET only supported on Win32 platforms" )
37
37
38
38
39
39
class NiXNETcanBus (BusABC ):
40
40
"""
41
41
The CAN Bus implemented for the NI-XNET interface.
42
-
43
42
"""
44
43
45
44
def __init__ (
@@ -52,7 +51,7 @@ def __init__(
52
51
brs = False ,
53
52
can_termination = False ,
54
53
log_errors = True ,
55
- ** kwargs
54
+ ** kwargs ,
56
55
):
57
56
"""
58
57
:param str channel:
@@ -69,9 +68,8 @@ def __init__(
69
68
``is_error_frame`` set to True and ``arbitration_id`` will identify
70
69
the error (default True)
71
70
72
- :raises can.interfaces.nixnet.NiXNETError :
71
+ :raises can.exceptions.CanInitializationError :
73
72
If starting communication fails
74
-
75
73
"""
76
74
self ._rx_queue = []
77
75
self .channel = channel
@@ -120,16 +118,18 @@ def __init__(
120
118
self .__session_send .start ()
121
119
self .__session_receive .start ()
122
120
123
- except errors .XnetError as err :
124
- raise NiXNETError (function = "__init__" , error_message = err .args [0 ]) from None
121
+ except errors .XnetError as error :
122
+ raise CanInitializationError (
123
+ f"{ error .args [0 ]} ({ error .error_type } )" , error .error_code
124
+ ) from None
125
125
126
126
self ._is_filtered = False
127
127
super (NiXNETcanBus , self ).__init__ (
128
128
channel = channel ,
129
129
can_filters = can_filters ,
130
130
bitrate = bitrate ,
131
131
log_errors = log_errors ,
132
- ** kwargs
132
+ ** kwargs ,
133
133
)
134
134
135
135
def _recv_internal (self , timeout ):
@@ -160,8 +160,7 @@ def _recv_internal(self, timeout):
160
160
)
161
161
162
162
return msg , self ._filters is None
163
- except Exception as e :
164
- # print('Error: ', e)
163
+ except Exception :
165
164
return None , self ._filters is None
166
165
167
166
def send (self , msg , timeout = None ):
@@ -174,7 +173,7 @@ def send(self, msg, timeout=None):
174
173
:param float timeout:
175
174
Max time to wait for the device to be ready in seconds, None if time is infinite
176
175
177
- :raises can.interfaces.nixnet.NiXNETError :
176
+ :raises can.exceptions.CanOperationError :
178
177
If writing to transmit buffer fails.
179
178
It does not wait for message to be ACKed currently.
180
179
"""
@@ -201,8 +200,10 @@ def send(self, msg, timeout=None):
201
200
202
201
try :
203
202
self .__session_send .frames .write ([can_frame ], timeout )
204
- except errors .XnetError as err :
205
- raise NiXNETError (function = "send" , error_message = err .args [0 ]) from None
203
+ except errors .XnetError as error :
204
+ raise CanOperationError (
205
+ f"{ error .args [0 ]} ({ error .error_type } )" , error .error_code
206
+ ) from None
206
207
207
208
def reset (self ):
208
209
"""
@@ -253,18 +254,3 @@ def _detect_available_configs():
253
254
logger .debug ("An error occured while searching for configs: %s" , str (error ))
254
255
255
256
return configs
256
-
257
-
258
- # To-Do review error management, I don't like this implementation
259
- class NiXNETError (CanError ):
260
- """Error from NI-XNET driver."""
261
-
262
- def __init__ (self , function = "" , error_message = "" ):
263
- super (NiXNETError , self ).__init__ ()
264
- #: Function that failed
265
- self .function = function
266
- #: Arguments passed to function
267
- self .error_message = error_message
268
-
269
- def __str__ (self ):
270
- return "Function %s failed:\n %s" % (self .function , self .error_message )
0 commit comments