Skip to content

Commit cff4b1c

Browse files
committed
Merged in evs-inmotion/python-can/pcan-fixes (pull request #30)
PCAN fixes
2 parents a5e479a + 145e5bf commit cff4b1c

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

can/interfaces/pcan.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from can.interfaces.PCANBasic import *
1111
from can.bus import BusABC
1212
from can.message import Message
13+
from can import CanError
1314
import time
1415

1516
boottimeEpoch = 0
@@ -58,14 +59,14 @@ def __init__(self, channel, *args, **kwargs):
5859
5960
:param str channel:
6061
The can interface name. An example would be PCAN_USBBUS1
61-
62+
6263
Backend Configuration
6364
---------------------
64-
65+
6566
:param int bitrate:
6667
Bitrate of channel in bit/s.
6768
Default is 500 Kbs
68-
69+
6970
"""
7071
if channel is None or channel == '':
7172
raise ArgumentError("Must specify a PCAN channel")
@@ -74,7 +75,7 @@ def __init__(self, channel, *args, **kwargs):
7475

7576
bitrate = kwargs.get('bitrate', 500000)
7677
pcan_bitrate = pcan_bitrate_objs.get(bitrate, PCAN_BAUD_500K)
77-
78+
7879
hwtype = PCAN_TYPE_ISA
7980
ioport = 0x02A0
8081
interrupt = 11
@@ -85,8 +86,7 @@ def __init__(self, channel, *args, **kwargs):
8586
result = self.m_objPCANBasic.Initialize(self.m_PcanHandle, pcan_bitrate, hwtype, ioport, interrupt)
8687

8788
if result != PCAN_ERROR_OK:
88-
# TODO throw a specific exception.
89-
raise Exception(self._get_formatted_error(result))
89+
raise PcanError(self._get_formatted_error(result))
9090

9191
super(PcanBus, self).__init__(*args, **kwargs)
9292

@@ -166,35 +166,32 @@ def recv(self, timeout=None):
166166
result = None
167167
time.sleep(0.001)
168168
elif result[0] != PCAN_ERROR_OK:
169-
raise Exception(self._get_formatted_error(result[0]))
169+
raise PcanError(self._get_formatted_error(result[0]))
170170

171171
theMsg = result[1]
172172
itsTimeStamp = result[2]
173173

174174
log.debug("Received a message")
175175

176-
arbitration_id = theMsg.ID
177-
178176
bIsRTR = (theMsg.MSGTYPE & PCAN_MESSAGE_RTR.value) == PCAN_MESSAGE_RTR.value
179177
bIsExt = (theMsg.MSGTYPE & PCAN_MESSAGE_EXTENDED.value) == PCAN_MESSAGE_EXTENDED.value
180178

181-
# Flags: EXT, RTR, ERR
182-
#flags = (PYCAN_RTRFLG if bIsRTR else 0) | (PYCAN_STDFLG if not bIsExt else 0)
183-
184179
if bIsExt:
185180
#rx_msg.id_type = ID_TYPE_EXTENDED
186181
log.debug("CAN: Extended")
187182
else:
188183
#rx_msg.id_type = ID_TYPE_STANDARD
189184
log.debug("CAN: Standard")
190185

191-
rx_msg.arbitration_id = arbitration_id
192-
rx_msg.id_type = bIsExt
193-
rx_msg.is_remote_frame = bIsRTR
194-
rx_msg.dlc = theMsg.LEN
195-
#rx_msg.flags = flags
196-
rx_msg.data = theMsg.DATA
197-
rx_msg.timestamp = boottimeEpoch + ((itsTimeStamp.micros + (1000 * itsTimeStamp.millis)) / (1000.0 * 1000.0))
186+
dlc = theMsg.LEN
187+
timestamp = boottimeEpoch + ((itsTimeStamp.micros + (1000 * itsTimeStamp.millis)) / (1000.0 * 1000.0))
188+
189+
rx_msg = Message(timestamp=timestamp,
190+
arbitration_id=theMsg.ID,
191+
extended_id=bIsExt,
192+
is_remote_frame=bIsRTR,
193+
dlc=dlc,
194+
data=theMsg.DATA[:dlc])
198195

199196
return rx_msg
200197

@@ -214,7 +211,7 @@ def send(self, msg):
214211

215212
# if a remote frame will be sent, data bytes are not important.
216213
if msg.is_remote_frame:
217-
CANMsg.MSGTYPE = msgType | PCAN_MESSAGE_RTR
214+
CANMsg.MSGTYPE = msgType.value | PCAN_MESSAGE_RTR.value
218215
else:
219216
# copy data
220217
for i in range(CANMsg.LEN):
@@ -224,13 +221,8 @@ def send(self, msg):
224221
log.debug("Type: %s", type(msg.data))
225222

226223
result = self.m_objPCANBasic.Write(self.m_PcanHandle, CANMsg)
227-
228-
sent = result == PCAN_ERROR_OK
229-
230-
if not sent:
231-
logging.warning("Failed to send: " + self._get_formatted_error(result))
232-
233-
return sent
224+
if result != PCAN_ERROR_OK:
225+
raise PcanError("Failed to send: " + self._get_formatted_error(result))
234226

235227
def flash(self, flash):
236228
"""
@@ -241,3 +233,7 @@ def flash(self, flash):
241233

242234
def shutdown(self):
243235
self.m_objPCANBasic.Uninitialize(self.m_PcanHandle)
236+
237+
238+
class PcanError(CanError):
239+
pass

0 commit comments

Comments
 (0)