1
- """ Neousys WDT_DIO CAN driver """
1
+ """ Neousys CAN bus driver """
2
2
3
3
#
4
4
# This kind of interface can be found for example on Neousys POC-551VTC
18
18
# pylint: disable=R1725
19
19
20
20
import warnings
21
- import threading
21
+ import queue
22
22
import logging
23
23
import platform
24
24
import time
@@ -83,46 +83,46 @@ class NeousysCanBitClk(Structure):
83
83
]
84
84
85
85
86
- WDT_CAN_MSG_CALLBACK = CFUNCTYPE (None , POINTER (NeousysCanMsg ), c_uint )
87
- WDT_CAN_STATUS_CALLBACK = CFUNCTYPE (None , c_uint )
86
+ NEOUSYS_CAN_MSG_CALLBACK = CFUNCTYPE (None , POINTER (NeousysCanMsg ), c_uint )
87
+ NEOUSYS_CAN_STATUS_CALLBACK = CFUNCTYPE (None , c_uint )
88
88
89
- WDT_CAN_MSG_EXTENDED_ID = 0x0004
90
- WDT_CAN_MSG_REMOTE_FRAME = 0x0040
91
- WDT_CAN_MSG_DATA_NEW = 0x0080
92
- WDT_CAN_MSG_DATA_LOST = 0x0100
89
+ NEOUSYS_CAN_MSG_EXTENDED_ID = 0x0004
90
+ NEOUSYS_CAN_MSG_REMOTE_FRAME = 0x0040
91
+ NEOUSYS_CAN_MSG_DATA_NEW = 0x0080
92
+ NEOUSYS_CAN_MSG_DATA_LOST = 0x0100
93
93
94
- WDT_CAN_MSG_USE_ID_FILTER = 0x00000008
95
- WDT_CAN_MSG_USE_DIR_FILTER = (
96
- 0x00000010 | WDT_CAN_MSG_USE_ID_FILTER
94
+ NEOUSYS_CAN_MSG_USE_ID_FILTER = 0x00000008
95
+ NEOUSYS_CAN_MSG_USE_DIR_FILTER = (
96
+ 0x00000010 | NEOUSYS_CAN_MSG_USE_ID_FILTER
97
97
) # only accept the direction specified in the message type
98
- WDT_CAN_MSG_USE_EXT_FILTER = (
99
- 0x00000020 | WDT_CAN_MSG_USE_ID_FILTER
98
+ NEOUSYS_CAN_MSG_USE_EXT_FILTER = (
99
+ 0x00000020 | NEOUSYS_CAN_MSG_USE_ID_FILTER
100
100
) # filters on only extended identifiers
101
101
102
- WDT_CAN_STATUS_BUS_OFF = 0x00000080
103
- WDT_CAN_STATUS_EWARN = (
102
+ NEOUSYS_CAN_STATUS_BUS_OFF = 0x00000080
103
+ NEOUSYS_CAN_STATUS_EWARN = (
104
104
0x00000040 # can controller error level has reached warning level.
105
105
)
106
- WDT_CAN_STATUS_EPASS = (
106
+ NEOUSYS_CAN_STATUS_EPASS = (
107
107
0x00000020 # can controller error level has reached error passive level.
108
108
)
109
- WDT_CAN_STATUS_LEC_STUFF = 0x00000001 # a bit stuffing error has occurred.
110
- WDT_CAN_STATUS_LEC_FORM = 0x00000002 # a formatting error has occurred.
111
- WDT_CAN_STATUS_LEC_ACK = 0x00000003 # an acknowledge error has occurred.
112
- WDT_CAN_STATUS_LEC_BIT1 = (
109
+ NEOUSYS_CAN_STATUS_LEC_STUFF = 0x00000001 # a bit stuffing error has occurred.
110
+ NEOUSYS_CAN_STATUS_LEC_FORM = 0x00000002 # a formatting error has occurred.
111
+ NEOUSYS_CAN_STATUS_LEC_ACK = 0x00000003 # an acknowledge error has occurred.
112
+ NEOUSYS_CAN_STATUS_LEC_BIT1 = (
113
113
0x00000004 # the bus remained a bit level of 1 for longer than is allowed.
114
114
)
115
- WDT_CAN_STATUS_LEC_BIT0 = (
115
+ NEOUSYS_CAN_STATUS_LEC_BIT0 = (
116
116
0x00000005 # the bus remained a bit level of 0 for longer than is allowed.
117
117
)
118
- WDT_CAN_STATUS_LEC_CRC = 0x00000006 # a crc error has occurred.
119
- WDT_CAN_STATUS_LEC_MASK = (
118
+ NEOUSYS_CAN_STATUS_LEC_CRC = 0x00000006 # a crc error has occurred.
119
+ NEOUSYS_CAN_STATUS_LEC_MASK = (
120
120
0x00000007 # this is the mask for the can last error code (lec).
121
121
)
122
122
123
123
124
124
class NeousysBus (BusABC ):
125
- """ Neousys WDT_DIO Canbus Class"""
125
+ """ Neousys CAN bus Class"""
126
126
127
127
def __init__ (self , channel , device = 0 , bitrate = 500000 , ** kwargs ):
128
128
"""
@@ -146,50 +146,41 @@ def __init__(self, channel, device=0, bitrate=500000, **kwargs):
146
146
147
147
self .device = device
148
148
149
- self .channel_info = "Neousys WDT_DIO Can: device {}, channel {}" .format (
149
+ self .channel_info = "Neousys Can: device {}, channel {}" .format (
150
150
self .device , self .channel
151
151
)
152
152
153
- self .lock = threading .Lock ()
154
- self .recv_msg_array = []
153
+ self .queue = queue .Queue ()
155
154
156
155
# Init with accept all and wanted bitrate
157
156
self .init_config = NeousysCanSetup (
158
- bitrate , WDT_CAN_MSG_USE_ID_FILTER , 0 , 0
157
+ bitrate , NEOUSYS_CAN_MSG_USE_ID_FILTER , 0 , 0
159
158
)
160
159
161
- # These can be needed in some old 2.x consepts not needed in 3.6 though
162
- # self.canlib.CAN_RegisterReceived.argtypes = [c_uint, WDT_CAN_MSG_CALLBACK]
163
- # self.canlib.CAN_RegisterReceived.restype = c_int
164
- # self.canlib.CAN_RegisterStatus.argtypes = [c_uint, WDT_CAN_STATUS_CALLBACK]
165
- # self.canlib.CAN_RegisterStatus.restype = c_int
166
-
167
- self ._neousys_wdt_recv_cb = WDT_CAN_MSG_CALLBACK (self ._neousys_wdt_recv_cb )
168
- self ._neousys_wdt_status_cb = WDT_CAN_STATUS_CALLBACK (
169
- self ._neousys_wdt_status_cb
160
+ self ._neousys_recv_cb = NEOUSYS_CAN_MSG_CALLBACK (self ._neousys_recv_cb )
161
+ self ._neousys_status_cb = NEOUSYS_CAN_STATUS_CALLBACK (
162
+ self ._neousys_status_cb
170
163
)
171
164
172
- if self .canlib .CAN_RegisterReceived (0 , self ._neousys_wdt_recv_cb ) == 0 :
173
- logger .error ("Neousys WDT_DIO CANBus Setup receive callback" )
165
+ if self .canlib .CAN_RegisterReceived (0 , self ._neousys_recv_cb ) == 0 :
166
+ logger .error ("Neousys CAN bus Setup receive callback" )
174
167
175
- if self .canlib .CAN_RegisterStatus (0 , self ._neousys_wdt_status_cb ) == 0 :
176
- logger .error ("Neousys WDT_DIO CANBus Setup status callback" )
168
+ if self .canlib .CAN_RegisterStatus (0 , self ._neousys_status_cb ) == 0 :
169
+ logger .error ("Neousys CAN bus Setup status callback" )
177
170
178
171
if (
179
172
self .canlib .CAN_Setup (
180
173
channel , byref (self .init_config ), sizeof (self .init_config )
181
174
)
182
175
== 0
183
176
):
184
- logger .error ("Neousys WDT_DIO CANBus Setup Error" )
177
+ logger .error ("Neousys CAN bus Setup Error" )
185
178
186
179
if self .canlib .CAN_Start (channel ) == 0 :
187
- logger .error ("Neousys WDT_DIO CANBus Start Error" )
180
+ logger .error ("Neousys CAN bus Start Error" )
188
181
189
182
except OSError as error :
190
- logger .info (
191
- "Cannot Neousys WDT_DIO CANBus dll or share object: %d" , format (error )
192
- )
183
+ logger .info ("Cannot Neousys CAN bus dll or share object: %d" , format (error ))
193
184
194
185
def send (self , msg , timeout = None ):
195
186
"""
@@ -199,28 +190,24 @@ def send(self, msg, timeout=None):
199
190
"""
200
191
201
192
if self .canlib is None :
202
- logger .error ("Can't send msg as Neousys WDT_DIO DLL/SO is not loaded" )
193
+ logger .error ("Can't send msg as Neousys DLL/SO is not loaded" )
203
194
else :
204
195
tx_msg = NeousysCanMsg (
205
196
msg .arbitration_id , 0 , 0 , msg .dlc , (c_ubyte * 8 )(* msg .data )
206
197
)
207
198
208
199
if self .canlib .CAN_Send (self .channel , byref (tx_msg ), sizeof (tx_msg )) == 0 :
209
- logger .error ("Neousys WDT_DIO Can can't send message" )
200
+ logger .error ("Neousys Can can't send message" )
210
201
211
202
def _recv_internal (self , timeout ):
212
203
msg = None
213
204
214
- # If there is message waiting in array
215
- # pass it as new message
216
- if len (self .recv_msg_array ) > 0 :
217
- self .lock .acquire ()
218
- msg = self .recv_msg_array .pop (0 )
219
- self .lock .release ()
205
+ if not self .queue .empty ():
206
+ msg = self .queue .get ()
220
207
221
208
return msg , False
222
209
223
- def _neousys_wdt_recv_cb (self , msg , sizeof_msg ):
210
+ def _neousys_recv_cb (self , msg , sizeof_msg ):
224
211
"""
225
212
:param msg struct CAN_MSG
226
213
:param sizeof_msg message number
@@ -231,14 +218,14 @@ def _neousys_wdt_recv_cb(self, msg, sizeof_msg):
231
218
232
219
msg_bytes = bytearray (msg .contents .data )
233
220
234
- if msg .contents .flags & WDT_CAN_MSG_REMOTE_FRAME :
221
+ if msg .contents .flags & NEOUSYS_CAN_MSG_REMOTE_FRAME :
235
222
remote_frame = True
236
223
237
- if msg .contents .flags & WDT_CAN_MSG_EXTENDED_ID :
224
+ if msg .contents .flags & NEOUSYS_CAN_MSG_EXTENDED_ID :
238
225
extended_frame = True
239
226
240
- if msg .contents .flags & WDT_CAN_MSG_DATA_LOST :
241
- logger .error ("_neousys_wdt_recv_cb flag CAN_MSG_DATA_LOST" )
227
+ if msg .contents .flags & NEOUSYS_CAN_MSG_DATA_LOST :
228
+ logger .error ("_neousys_recv_cb flag CAN_MSG_DATA_LOST" )
242
229
243
230
msg = Message (
244
231
timestamp = time .time (),
@@ -253,17 +240,30 @@ def _neousys_wdt_recv_cb(self, msg, sizeof_msg):
253
240
# Reading happens in Callback function and
254
241
# with Python-CAN it happens polling
255
242
# so cache stuff in array to for poll
256
- self .lock .acquire ()
257
- self .recv_msg_array .append (msg )
258
- self .lock .release ()
243
+ if not self .queue .full ():
244
+ self .queue .put (msg )
245
+ else :
246
+ logger .error ("Neousys message Queue is full" )
259
247
260
- def _neousys_wdt_status_cb (self , status ):
248
+ def _neousys_status_cb (self , status ):
261
249
"""
262
250
:param status BUS Status
263
251
:return:
264
252
"""
265
- logger .info ("%s _neousys_wdt_status_cb : %d" , self .init_config , status )
253
+ logger .info ("%s _neousys_status_cb : %d" , self .init_config , status )
266
254
267
255
def shutdown (self ):
268
256
if self .canlib is not None :
269
257
self .canlib .CAN_Stop (self .channel )
258
+
259
+ def fileno (self ):
260
+ # Return an invalid file descriptor as not used
261
+ return - 1
262
+
263
+ @staticmethod
264
+ def _detect_available_configs ():
265
+ channels = []
266
+
267
+ # There is only one channel
268
+ channels .append ({"interface" : "neousys" , "channel" : 0 })
269
+ return channels
0 commit comments