Skip to content

Commit 86d13ed

Browse files
committed
Neousys: Remove threading code and start using Queue as thread safe message bug
1 parent d62f2ee commit 86d13ed

File tree

2 files changed

+65
-65
lines changed

2 files changed

+65
-65
lines changed

can/interfaces/neousys/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
""" Neousys WDT_DIO CAN driver """
1+
""" Neousys CAN bus driver """
22

33
from can.interfaces.neousys.neousys import NeousysBus

can/interfaces/neousys/neousys.py

+64-64
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Neousys WDT_DIO CAN driver """
1+
""" Neousys CAN bus driver """
22

33
#
44
# This kind of interface can be found for example on Neousys POC-551VTC
@@ -18,7 +18,7 @@
1818
# pylint: disable=R1725
1919

2020
import warnings
21-
import threading
21+
import queue
2222
import logging
2323
import platform
2424
import time
@@ -83,46 +83,46 @@ class NeousysCanBitClk(Structure):
8383
]
8484

8585

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)
8888

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
9393

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
9797
) # 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
100100
) # filters on only extended identifiers
101101

102-
WDT_CAN_STATUS_BUS_OFF = 0x00000080
103-
WDT_CAN_STATUS_EWARN = (
102+
NEOUSYS_CAN_STATUS_BUS_OFF = 0x00000080
103+
NEOUSYS_CAN_STATUS_EWARN = (
104104
0x00000040 # can controller error level has reached warning level.
105105
)
106-
WDT_CAN_STATUS_EPASS = (
106+
NEOUSYS_CAN_STATUS_EPASS = (
107107
0x00000020 # can controller error level has reached error passive level.
108108
)
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 = (
113113
0x00000004 # the bus remained a bit level of 1 for longer than is allowed.
114114
)
115-
WDT_CAN_STATUS_LEC_BIT0 = (
115+
NEOUSYS_CAN_STATUS_LEC_BIT0 = (
116116
0x00000005 # the bus remained a bit level of 0 for longer than is allowed.
117117
)
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 = (
120120
0x00000007 # this is the mask for the can last error code (lec).
121121
)
122122

123123

124124
class NeousysBus(BusABC):
125-
""" Neousys WDT_DIO Canbus Class"""
125+
""" Neousys CAN bus Class"""
126126

127127
def __init__(self, channel, device=0, bitrate=500000, **kwargs):
128128
"""
@@ -146,50 +146,41 @@ def __init__(self, channel, device=0, bitrate=500000, **kwargs):
146146

147147
self.device = device
148148

149-
self.channel_info = "Neousys WDT_DIO Can: device {}, channel {}".format(
149+
self.channel_info = "Neousys Can: device {}, channel {}".format(
150150
self.device, self.channel
151151
)
152152

153-
self.lock = threading.Lock()
154-
self.recv_msg_array = []
153+
self.queue = queue.Queue()
155154

156155
# Init with accept all and wanted bitrate
157156
self.init_config = NeousysCanSetup(
158-
bitrate, WDT_CAN_MSG_USE_ID_FILTER, 0, 0
157+
bitrate, NEOUSYS_CAN_MSG_USE_ID_FILTER, 0, 0
159158
)
160159

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
170163
)
171164

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")
174167

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")
177170

178171
if (
179172
self.canlib.CAN_Setup(
180173
channel, byref(self.init_config), sizeof(self.init_config)
181174
)
182175
== 0
183176
):
184-
logger.error("Neousys WDT_DIO CANBus Setup Error")
177+
logger.error("Neousys CAN bus Setup Error")
185178

186179
if self.canlib.CAN_Start(channel) == 0:
187-
logger.error("Neousys WDT_DIO CANBus Start Error")
180+
logger.error("Neousys CAN bus Start Error")
188181

189182
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))
193184

194185
def send(self, msg, timeout=None):
195186
"""
@@ -199,28 +190,24 @@ def send(self, msg, timeout=None):
199190
"""
200191

201192
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")
203194
else:
204195
tx_msg = NeousysCanMsg(
205196
msg.arbitration_id, 0, 0, msg.dlc, (c_ubyte * 8)(*msg.data)
206197
)
207198

208199
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")
210201

211202
def _recv_internal(self, timeout):
212203
msg = None
213204

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()
220207

221208
return msg, False
222209

223-
def _neousys_wdt_recv_cb(self, msg, sizeof_msg):
210+
def _neousys_recv_cb(self, msg, sizeof_msg):
224211
"""
225212
:param msg struct CAN_MSG
226213
:param sizeof_msg message number
@@ -231,14 +218,14 @@ def _neousys_wdt_recv_cb(self, msg, sizeof_msg):
231218

232219
msg_bytes = bytearray(msg.contents.data)
233220

234-
if msg.contents.flags & WDT_CAN_MSG_REMOTE_FRAME:
221+
if msg.contents.flags & NEOUSYS_CAN_MSG_REMOTE_FRAME:
235222
remote_frame = True
236223

237-
if msg.contents.flags & WDT_CAN_MSG_EXTENDED_ID:
224+
if msg.contents.flags & NEOUSYS_CAN_MSG_EXTENDED_ID:
238225
extended_frame = True
239226

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")
242229

243230
msg = Message(
244231
timestamp=time.time(),
@@ -253,17 +240,30 @@ def _neousys_wdt_recv_cb(self, msg, sizeof_msg):
253240
# Reading happens in Callback function and
254241
# with Python-CAN it happens polling
255242
# 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")
259247

260-
def _neousys_wdt_status_cb(self, status):
248+
def _neousys_status_cb(self, status):
261249
"""
262250
:param status BUS Status
263251
:return:
264252
"""
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)
266254

267255
def shutdown(self):
268256
if self.canlib is not None:
269257
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

Comments
 (0)