4
4
"""
5
5
6
6
from ctypes import *
7
- from struct import *
8
- from enum import Enum
7
+ from enum import IntEnum
9
8
import logging
9
+ from contextlib import contextmanager
10
10
11
11
import can
12
12
25
25
IS_ID_TYPE = 1
26
26
27
27
28
- class CanalError (Enum ):
28
+ class CanalError (IntEnum ):
29
29
SUCCESS = 0
30
30
BAUDRATE = 1
31
31
BUS_OFF = 2
@@ -102,6 +102,14 @@ class CanalMsg(Structure):
102
102
]
103
103
104
104
105
+ @contextmanager
106
+ def error_check (error_message : str ) -> None :
107
+ try :
108
+ yield
109
+ except Exception as error :
110
+ raise can .CanOperationError (error_message ) from error
111
+
112
+
105
113
class Usb2CanAbstractionLayer :
106
114
"""A low level wrapper around the usb2can library.
107
115
@@ -112,21 +120,26 @@ def __init__(self, dll="usb2can.dll"):
112
120
"""
113
121
:type dll: str or path-like
114
122
:param dll (optional): the path to the usb2can DLL to load
115
- :raises OSError: if the DLL could not be loaded
123
+
124
+ :raises can.CanInterfaceNotImplementedError: if the DLL could not be loaded
116
125
"""
117
- self .__m_dllBasic = windll .LoadLibrary (dll )
126
+ try :
127
+ self .__m_dllBasic = windll .LoadLibrary (dll )
128
+ if self .__m_dllBasic is None :
129
+ raise Exception ("__m_dllBasic is None" )
118
130
119
- if self .__m_dllBasic is None :
120
- log .warning ("DLL failed to load at path: {}" .format (dll ))
131
+ except Exception as error :
132
+ message = f"DLL failed to load at path: { dll } "
133
+ raise can .CanInterfaceNotImplementedError (message ) from error
121
134
122
- def open (self , configuration , flags ):
135
+ def open (self , configuration : str , flags : int ):
123
136
"""
124
137
Opens a CAN connection using `CanalOpen()`.
125
138
126
- :param str configuration: the configuration: "device_id; baudrate"
127
- :param int flags: the flags to be set
139
+ :param configuration: the configuration: "device_id; baudrate"
140
+ :param flags: the flags to be set
128
141
129
- :raises can.CanError : if any error occurred
142
+ :raises can.CanInitializationError : if any error occurred
130
143
:returns: Valid handle for CANAL API functions on success
131
144
"""
132
145
try :
@@ -136,100 +149,57 @@ def open(self, configuration, flags):
136
149
result = self .__m_dllBasic .CanalOpen (config_ascii , flags )
137
150
except Exception as ex :
138
151
# catch any errors thrown by this call and re-raise
139
- raise can .CanError (
140
- 'CanalOpen() failed, configuration: "{}", error: {}' .format (
141
- configuration , ex
142
- )
152
+ raise can .CanInitializationError (
153
+ f'CanalOpen() failed, configuration: "{ configuration } ", error: { ex } '
143
154
)
144
155
else :
145
156
# any greater-than-zero return value indicates a success
146
157
# (see https://grodansparadis.gitbooks.io/the-vscp-daemon/canal_interface_specification.html)
147
158
# raise an error if the return code is <= 0
148
159
if result <= 0 :
149
- raise can .CanError (
150
- 'CanalOpen() failed, configuration: "{}", return code: {}' .format (
151
- configuration , result
152
- )
160
+ raise can .CanInitializationError (
161
+ f'CanalOpen() failed, configuration: "{ configuration } "' ,
162
+ error_code = result ,
153
163
)
154
164
else :
155
165
return result
156
166
157
- def close (self , handle ):
158
- try :
159
- res = self .__m_dllBasic .CanalClose (handle )
160
- return CanalError (res )
161
- except :
162
- log .warning ("Failed to close" )
163
- raise
167
+ def close (self , handle ) -> CanalError :
168
+ with error_check ("Failed to close" ):
169
+ return CanalError (self .__m_dllBasic .CanalClose (handle ))
164
170
165
- def send (self , handle , msg ):
166
- try :
167
- res = self .__m_dllBasic .CanalSend (handle , msg )
168
- return CanalError (res )
169
- except :
170
- log .warning ("Sending error" )
171
- raise can .CanError ("Failed to transmit frame" )
171
+ def send (self , handle , msg ) -> CanalError :
172
+ with error_check ("Failed to transmit frame" ):
173
+ return CanalError (self .__m_dllBasic .CanalSend (handle , msg ))
172
174
173
- def receive (self , handle , msg ):
174
- try :
175
- res = self .__m_dllBasic .CanalReceive (handle , msg )
176
- return CanalError (res )
177
- except :
178
- log .warning ("Receive error" )
179
- raise
175
+ def receive (self , handle , msg ) -> CanalError :
176
+ with error_check ("Receive error" ):
177
+ return CanalError (self .__m_dllBasic .CanalReceive (handle , msg ))
180
178
181
- def blocking_send (self , handle , msg , timeout ):
182
- try :
183
- res = self .__m_dllBasic .CanalBlockingSend (handle , msg , timeout )
184
- return CanalError (res )
185
- except :
186
- log .warning ("Blocking send error" )
187
- raise
179
+ def blocking_send (self , handle , msg , timeout ) -> CanalError :
180
+ with error_check ("Blocking send error" ):
181
+ return CanalError (self .__m_dllBasic .CanalBlockingSend (handle , msg , timeout ))
188
182
189
- def blocking_receive (self , handle , msg , timeout ):
190
- try :
191
- res = self .__m_dllBasic .CanalBlockingReceive (handle , msg , timeout )
192
- return CanalError (res )
193
- except :
194
- log .warning ("Blocking Receive Failed" )
195
- raise
183
+ def blocking_receive (self , handle , msg , timeout ) -> CanalError :
184
+ with error_check ("Blocking Receive Failed" ):
185
+ return CanalError (self .__m_dllBasic .CanalBlockingReceive (handle , msg , timeout ))
196
186
197
- def get_status (self , handle , status ):
198
- try :
199
- res = self .__m_dllBasic .CanalGetStatus (handle , status )
200
- return CanalError (res )
201
- except :
202
- log .warning ("Get status failed" )
203
- raise
187
+ def get_status (self , handle , status ) -> CanalError :
188
+ with error_check ("Get status failed" ):
189
+ return CanalError (self .__m_dllBasic .CanalGetStatus (handle , status ))
204
190
205
- def get_statistics (self , handle , statistics ):
206
- try :
207
- res = self .__m_dllBasic .CanalGetStatistics (handle , statistics )
208
- return CanalError (res )
209
- except :
210
- log .warning ("Get Statistics failed" )
211
- raise
191
+ def get_statistics (self , handle , statistics ) -> CanalError :
192
+ with error_check ("Get Statistics failed" ):
193
+ return CanalError (self .__m_dllBasic .CanalGetStatistics (handle , statistics ))
212
194
213
195
def get_version (self ):
214
- try :
215
- res = self .__m_dllBasic .CanalGetVersion ()
216
- return res
217
- except :
218
- log .warning ("Failed to get version info" )
219
- raise
196
+ with error_check ("Failed to get version info" ):
197
+ return self .__m_dllBasic .CanalGetVersion ()
220
198
221
199
def get_library_version (self ):
222
- try :
223
- res = self .__m_dllBasic .CanalGetDllVersion ()
224
- return res
225
- except :
226
- log .warning ("Failed to get DLL version" )
227
- raise
200
+ with error_check ("Failed to get DLL version" ):
201
+ return self .__m_dllBasic .CanalGetDllVersion ()
228
202
229
203
def get_vendor_string (self ):
230
- try :
231
- res = self .__m_dllBasic .CanalGetVendorString ()
232
- return res
233
- except :
234
- log .warning ("Failed to get vendor string" )
235
- raise
204
+ with error_check ("Failed to get vendor string" ):
205
+ return self .__m_dllBasic .CanalGetVendorString ()
0 commit comments