Skip to content

Commit c370ad3

Browse files
committed
Neousys WDT_DIO CAN interface pylint fixes
1 parent 143f7b3 commit c370ad3

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

can/interfaces/neousys_wdt.py

+50-33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
""" Neousys WDT_DIO CAN driver """
2+
13
#
24
# This kind of interface can be found for example on Neousys POC-551VTC
35
# One needs to have correct drivers and DLL (Share object for Linux) from Neousys
@@ -8,18 +10,32 @@
810
# with Windows but you have to replace with correct named DLL
911
#
1012

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+
1120
import warnings
12-
from ctypes import *
1321
import threading
1422
import logging
1523
import platform
1624
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
1732
from can import BusABC, Message
1833

19-
logger = logging.getLogger(__name__)
2034

35+
logger = logging.getLogger(__name__)
2136

22-
class WDT_CAN_SETUP(Structure):
37+
class NeousysWdtCanSetup(Structure):
38+
""" C CAN Setup struct """
2339
_fields_ = [
2440
("bitRate", c_uint),
2541
("recvConfig", c_uint),
@@ -28,7 +44,8 @@ class WDT_CAN_SETUP(Structure):
2844
]
2945

3046

31-
class WDT_CAN_MSG(Structure):
47+
class NeousysWdtCanMsg(Structure):
48+
""" C CAN Message struct """
3249
_fields_ = [
3350
("id", c_uint),
3451
("flags", c_ushort),
@@ -38,11 +55,13 @@ class WDT_CAN_MSG(Structure):
3855
]
3956

4057

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.
4260
# valid:1~8, the Phase Buffer 2 segment in time quanta.
4361
# valid:1~4, Resynchronization Jump Width in time quanta
4462
# 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 """
4665
_fields_ = [
4766
("syncPropPhase1Seg", c_ushort),
4867
("phase2Seg", c_ushort),
@@ -51,7 +70,7 @@ class WDT_CAN_BITCLK(Structure):
5170
]
5271

5372

54-
WDT_CAN_MSG_CALLBACK = CFUNCTYPE(None, POINTER(WDT_CAN_MSG), c_uint)
73+
WDT_CAN_MSG_CALLBACK = CFUNCTYPE(None, POINTER(NeousysWdtCanMsg), c_uint)
5574
WDT_CAN_STATUS_CALLBACK = CFUNCTYPE(None, c_uint)
5675

5776
WDT_CAN_MSG_EXTENDED_ID = 0x0004
@@ -90,6 +109,8 @@ class WDT_CAN_BITCLK(Structure):
90109

91110

92111
class NeousysWdtBus(BusABC):
112+
""" Neousys WDT_DIO Canbus Class"""
113+
93114
def __init__(self, channel, device=0, bitrate=500000, **kwargs):
94115
"""
95116
:param channel: channel number
@@ -120,21 +141,22 @@ def __init__(self, channel, device=0, bitrate=500000, **kwargs):
120141
self.recv_msg_array = []
121142

122143
# 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)
124145

125146
# These can be needed in some old 2.x consepts not needed in 3.6 though
126147
# self.canlib.CAN_RegisterReceived.argtypes = [c_uint, WDT_CAN_MSG_CALLBACK]
127148
# self.canlib.CAN_RegisterReceived.restype = c_int
128149
# self.canlib.CAN_RegisterStatus.argtypes = [c_uint, WDT_CAN_STATUS_CALLBACK]
129150
# self.canlib.CAN_RegisterStatus.restype = c_int
130151

131-
self._WDTCAN_Received = WDT_CAN_MSG_CALLBACK(self._WDTCAN_Received)
132-
self._WDTCAN_Status = WDT_CAN_STATUS_CALLBACK(self._WDTCAN_Status)
133152

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:
135157
logger.error("Neousys WDT_DIO CANBus Setup receive callback")
136158

137-
if self.canlib.CAN_RegisterStatus(0, self._WDTCAN_Status) == 0:
159+
if self.canlib.CAN_RegisterStatus(0, self._neousys_wdt_status_cb) == 0:
138160
logger.error("Neousys WDT_DIO CANBus Setup status callback")
139161

140162
if (
@@ -148,8 +170,8 @@ def __init__(self, channel, device=0, bitrate=500000, **kwargs):
148170
if self.canlib.CAN_Start(channel) == 0:
149171
logger.error("Neousys WDT_DIO CANBus Start Error")
150172

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

154176
def send(self, msg, timeout=None):
155177
"""
@@ -161,7 +183,7 @@ def send(self, msg, timeout=None):
161183
if self.canlib is None:
162184
logger.error("Can't send msg as Neousys WDT_DIO DLL/SO is not loaded")
163185
else:
164-
tx_msg = WDT_CAN_MSG(
186+
tx_msg = NeousysWdtCanMsg(
165187
msg.arbitration_id, 0, 0, msg.dlc, (c_ubyte * 8)(*msg.data)
166188
)
167189

@@ -180,34 +202,34 @@ def _recv_internal(self, timeout):
180202

181203
return msg, False
182204

183-
def _WDTCAN_Received(self, lpMsg, cbMsg):
205+
def _neousys_wdt_recv_cb(self, msg, sizeof_msg):
184206
"""
185-
:param lpMsg struct CAN_MSG
186-
:param cbMsg message number
207+
:param msg struct CAN_MSG
208+
:param sizeof_msg message number
187209
:return:
188210
"""
189211
remote_frame = False
190212
extended_frame = False
191213

192-
msg_bytes = bytearray(lpMsg.contents.data)
214+
msg_bytes = bytearray(msg.contents.data)
193215

194-
if lpMsg.contents.flags & WDT_CAN_MSG_REMOTE_FRAME:
216+
if msg.contents.flags & WDT_CAN_MSG_REMOTE_FRAME:
195217
remote_frame = True
196218

197-
if lpMsg.contents.flags & WDT_CAN_MSG_EXTENDED_ID:
219+
if msg.contents.flags & WDT_CAN_MSG_EXTENDED_ID:
198220
extended_frame = True
199221

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

203225
msg = Message(
204226
timestamp=time.time(),
205-
arbitration_id=lpMsg.contents.id,
227+
arbitration_id=msg.contents.id,
206228
is_remote_frame=remote_frame,
207229
is_extended_id=extended_frame,
208230
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],
211233
)
212234

213235
# Reading happens in Callback function and
@@ -217,18 +239,13 @@ def _WDTCAN_Received(self, lpMsg, cbMsg):
217239
self.recv_msg_array.append(msg)
218240
self.lock.release()
219241

220-
def _WDTCAN_Status(status):
242+
def _neousys_wdt_status_cb(self, status):
221243
"""
222244
:param status BUS Status
223245
:return:
224246
"""
225-
226-
logger.info("_WDTCAN_Status" + str(status))
247+
logger.info("%s _neousys_wdt_status_cb: %d", self.init_config, status)
227248

228249
def shutdown(self):
229250
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:
234251
self.canlib.CAN_Stop(self.channel)

0 commit comments

Comments
 (0)