Skip to content

Commit 0a1ecd4

Browse files
PCAN send() will raise CanError on failed transmissions (issue #77).
Use Message constructor on received messages (fixes #66).
1 parent a5e479a commit 0a1ecd4

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

can/interfaces/pcan.py

Lines changed: 12 additions & 19 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
@@ -173,28 +174,25 @@ def recv(self, timeout=None):
173174

174175
log.debug("Received a message")
175176

176-
arbitration_id = theMsg.ID
177-
178177
bIsRTR = (theMsg.MSGTYPE & PCAN_MESSAGE_RTR.value) == PCAN_MESSAGE_RTR.value
179178
bIsExt = (theMsg.MSGTYPE & PCAN_MESSAGE_EXTENDED.value) == PCAN_MESSAGE_EXTENDED.value
180179

181-
# Flags: EXT, RTR, ERR
182-
#flags = (PYCAN_RTRFLG if bIsRTR else 0) | (PYCAN_STDFLG if not bIsExt else 0)
183-
184180
if bIsExt:
185181
#rx_msg.id_type = ID_TYPE_EXTENDED
186182
log.debug("CAN: Extended")
187183
else:
188184
#rx_msg.id_type = ID_TYPE_STANDARD
189185
log.debug("CAN: Standard")
190186

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))
187+
dlc = theMsg.LEN
188+
timestamp = boottimeEpoch + ((itsTimeStamp.micros + (1000 * itsTimeStamp.millis)) / (1000.0 * 1000.0))
189+
190+
rx_msg = Message(timestamp=timestamp,
191+
arbitration_id=theMsg.ID,
192+
extended_id=bIsExt,
193+
is_remote_frame=bIsRTR,
194+
dlc=dlc,
195+
data=theMsg.DATA[:dlc])
198196

199197
return rx_msg
200198

@@ -224,13 +222,8 @@ def send(self, msg):
224222
log.debug("Type: %s", type(msg.data))
225223

226224
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
225+
if result != PCAN_ERROR_OK:
226+
raise CanError("Failed to send: " + self._get_formatted_error(result))
234227

235228
def flash(self, flash):
236229
"""

0 commit comments

Comments
 (0)