1
+ """ Neousys WDT_DIO CAN driver """
2
+
1
3
#
2
4
# This kind of interface can be found for example on Neousys POC-551VTC
3
5
# One needs to have correct drivers and DLL (Share object for Linux) from Neousys
8
10
# with Windows but you have to replace with correct named DLL
9
11
#
10
12
13
+ # pylint: disable=R0903
14
+ # pylint: disable=R0902
15
+ # pylint: disable=C0413
16
+ # pylint: disable=E0202
17
+ # pylint: disable=W0611
18
+ # pylint: disable=R1725
19
+
11
20
import warnings
12
- from ctypes import *
13
21
import threading
14
22
import logging
15
23
import platform
16
24
import time
25
+
26
+ from ctypes import byref , CFUNCTYPE , c_ubyte , c_uint , c_ushort , POINTER , sizeof , Structure
27
+
28
+ if platform .system () == "Windows" :
29
+ from ctypes import WinDLL
30
+ else :
31
+ from ctypes import CDLL
17
32
from can import BusABC , Message
18
33
19
- logger = logging .getLogger (__name__ )
20
34
35
+ logger = logging .getLogger (__name__ )
21
36
22
- class WDT_CAN_SETUP (Structure ):
37
+ class NeousysWdtCanSetup (Structure ):
38
+ """ C CAN Setup struct """
23
39
_fields_ = [
24
40
("bitRate" , c_uint ),
25
41
("recvConfig" , c_uint ),
@@ -28,7 +44,8 @@ class WDT_CAN_SETUP(Structure):
28
44
]
29
45
30
46
31
- class WDT_CAN_MSG (Structure ):
47
+ class NeousysWdtCanMsg (Structure ):
48
+ """ C CAN Message struct """
32
49
_fields_ = [
33
50
("id" , c_uint ),
34
51
("flags" , c_ushort ),
@@ -38,11 +55,13 @@ class WDT_CAN_MSG(Structure):
38
55
]
39
56
40
57
41
- # valid:2~16, sum of the Synchronization, Propagation, and Phase Buffer 1 segments, measured in time quanta.
58
+ # valid:2~16, sum of the Synchronization, Propagation, and
59
+ # Phase Buffer 1 segments, measured in time quanta.
42
60
# valid:1~8, the Phase Buffer 2 segment in time quanta.
43
61
# valid:1~4, Resynchronization Jump Width in time quanta
44
62
# valid:1~1023, CAN_CLK divider used to determine time quanta
45
- class WDT_CAN_BITCLK (Structure ):
63
+ class NeousysWdtCanBitClk (Structure ):
64
+ """ C CAN BIT Clock struct """
46
65
_fields_ = [
47
66
("syncPropPhase1Seg" , c_ushort ),
48
67
("phase2Seg" , c_ushort ),
@@ -51,7 +70,7 @@ class WDT_CAN_BITCLK(Structure):
51
70
]
52
71
53
72
54
- WDT_CAN_MSG_CALLBACK = CFUNCTYPE (None , POINTER (WDT_CAN_MSG ), c_uint )
73
+ WDT_CAN_MSG_CALLBACK = CFUNCTYPE (None , POINTER (NeousysWdtCanMsg ), c_uint )
55
74
WDT_CAN_STATUS_CALLBACK = CFUNCTYPE (None , c_uint )
56
75
57
76
WDT_CAN_MSG_EXTENDED_ID = 0x0004
@@ -90,6 +109,8 @@ class WDT_CAN_BITCLK(Structure):
90
109
91
110
92
111
class NeousysWdtBus (BusABC ):
112
+ """ Neousys WDT_DIO Canbus Class"""
113
+
93
114
def __init__ (self , channel , device = 0 , bitrate = 500000 , ** kwargs ):
94
115
"""
95
116
:param channel: channel number
@@ -120,21 +141,22 @@ def __init__(self, channel, device=0, bitrate=500000, **kwargs):
120
141
self .recv_msg_array = []
121
142
122
143
# Init with accept all and wanted bitrate
123
- self .init_config = WDT_CAN_SETUP (bitrate , WDT_CAN_MSG_USE_ID_FILTER , 0 , 0 )
144
+ self .init_config = NeousysWdtCanSetup (bitrate , WDT_CAN_MSG_USE_ID_FILTER , 0 , 0 )
124
145
125
146
# These can be needed in some old 2.x consepts not needed in 3.6 though
126
147
# self.canlib.CAN_RegisterReceived.argtypes = [c_uint, WDT_CAN_MSG_CALLBACK]
127
148
# self.canlib.CAN_RegisterReceived.restype = c_int
128
149
# self.canlib.CAN_RegisterStatus.argtypes = [c_uint, WDT_CAN_STATUS_CALLBACK]
129
150
# self.canlib.CAN_RegisterStatus.restype = c_int
130
151
131
- self ._WDTCAN_Received = WDT_CAN_MSG_CALLBACK (self ._WDTCAN_Received )
132
- self ._WDTCAN_Status = WDT_CAN_STATUS_CALLBACK (self ._WDTCAN_Status )
133
152
134
- if self .canlib .CAN_RegisterReceived (0 , self ._WDTCAN_Received ) == 0 :
153
+ self ._neousys_wdt_recv_cb = WDT_CAN_MSG_CALLBACK (self ._neousys_wdt_recv_cb )
154
+ self ._neousys_wdt_status_cb = WDT_CAN_STATUS_CALLBACK (self ._neousys_wdt_status_cb )
155
+
156
+ if self .canlib .CAN_RegisterReceived (0 , self ._neousys_wdt_recv_cb ) == 0 :
135
157
logger .error ("Neousys WDT_DIO CANBus Setup receive callback" )
136
158
137
- if self .canlib .CAN_RegisterStatus (0 , self ._WDTCAN_Status ) == 0 :
159
+ if self .canlib .CAN_RegisterStatus (0 , self ._neousys_wdt_status_cb ) == 0 :
138
160
logger .error ("Neousys WDT_DIO CANBus Setup status callback" )
139
161
140
162
if (
@@ -148,8 +170,8 @@ def __init__(self, channel, device=0, bitrate=500000, **kwargs):
148
170
if self .canlib .CAN_Start (channel ) == 0 :
149
171
logger .error ("Neousys WDT_DIO CANBus Start Error" )
150
172
151
- except OSError as e :
152
- logger .info ("Cannot Neousys WDT_DIO CANBus dll or share object" )
173
+ except OSError as error :
174
+ logger .info ("Cannot Neousys WDT_DIO CANBus dll or share object: %d" , format ( error ) )
153
175
154
176
def send (self , msg , timeout = None ):
155
177
"""
@@ -161,7 +183,7 @@ def send(self, msg, timeout=None):
161
183
if self .canlib is None :
162
184
logger .error ("Can't send msg as Neousys WDT_DIO DLL/SO is not loaded" )
163
185
else :
164
- tx_msg = WDT_CAN_MSG (
186
+ tx_msg = NeousysWdtCanMsg (
165
187
msg .arbitration_id , 0 , 0 , msg .dlc , (c_ubyte * 8 )(* msg .data )
166
188
)
167
189
@@ -180,34 +202,34 @@ def _recv_internal(self, timeout):
180
202
181
203
return msg , False
182
204
183
- def _WDTCAN_Received (self , lpMsg , cbMsg ):
205
+ def _neousys_wdt_recv_cb (self , msg , sizeof_msg ):
184
206
"""
185
- :param lpMsg struct CAN_MSG
186
- :param cbMsg message number
207
+ :param msg struct CAN_MSG
208
+ :param sizeof_msg message number
187
209
:return:
188
210
"""
189
211
remote_frame = False
190
212
extended_frame = False
191
213
192
- msg_bytes = bytearray (lpMsg .contents .data )
214
+ msg_bytes = bytearray (msg .contents .data )
193
215
194
- if lpMsg .contents .flags & WDT_CAN_MSG_REMOTE_FRAME :
216
+ if msg .contents .flags & WDT_CAN_MSG_REMOTE_FRAME :
195
217
remote_frame = True
196
218
197
- if lpMsg .contents .flags & WDT_CAN_MSG_EXTENDED_ID :
219
+ if msg .contents .flags & WDT_CAN_MSG_EXTENDED_ID :
198
220
extended_frame = True
199
221
200
- if lpMsg .contents .flags & WDT_CAN_MSG_DATA_LOST :
201
- logger .error ("_WDTCAN_Received flag CAN_MSG_DATA_LOST" )
222
+ if msg .contents .flags & WDT_CAN_MSG_DATA_LOST :
223
+ logger .error ("_neousys_wdt_recv_cb flag CAN_MSG_DATA_LOST" )
202
224
203
225
msg = Message (
204
226
timestamp = time .time (),
205
- arbitration_id = lpMsg .contents .id ,
227
+ arbitration_id = msg .contents .id ,
206
228
is_remote_frame = remote_frame ,
207
229
is_extended_id = extended_frame ,
208
230
channel = self .channel ,
209
- dlc = lpMsg .contents .len ,
210
- data = msg_bytes [: lpMsg .contents .len ],
231
+ dlc = msg .contents .len ,
232
+ data = msg_bytes [: msg .contents .len ],
211
233
)
212
234
213
235
# Reading happens in Callback function and
@@ -217,18 +239,13 @@ def _WDTCAN_Received(self, lpMsg, cbMsg):
217
239
self .recv_msg_array .append (msg )
218
240
self .lock .release ()
219
241
220
- def _WDTCAN_Status ( status ):
242
+ def _neousys_wdt_status_cb ( self , status ):
221
243
"""
222
244
:param status BUS Status
223
245
:return:
224
246
"""
225
-
226
- logger .info ("_WDTCAN_Status" + str (status ))
247
+ logger .info ("%s _neousys_wdt_status_cb: %d" , self .init_config , status )
227
248
228
249
def shutdown (self ):
229
250
if self .canlib is not None :
230
- logger .error (
231
- "No need Can't send msg as Neousys WDT_DIO DLL/SO is not loaded"
232
- )
233
- else :
234
251
self .canlib .CAN_Stop (self .channel )
0 commit comments