Skip to content

Commit 4e39768

Browse files
Enable echo frames in PCAN driver if receive_own_messages is set (#1723)
* Enable echo frames in PCAN driver if receive_own_messages is set * Appease the linter * Set message direction to TX if the received message is an echo frame
1 parent ec105ac commit 4e39768

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

can/interfaces/pcan/pcan.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
FEATURE_FD_CAPABLE,
2727
IS_LINUX,
2828
IS_WINDOWS,
29+
PCAN_ALLOW_ECHO_FRAMES,
2930
PCAN_ALLOW_ERROR_FRAMES,
3031
PCAN_API_VERSION,
3132
PCAN_ATTACHED_CHANNELS,
@@ -47,6 +48,7 @@
4748
PCAN_LANBUS1,
4849
PCAN_LISTEN_ONLY,
4950
PCAN_MESSAGE_BRS,
51+
PCAN_MESSAGE_ECHO,
5052
PCAN_MESSAGE_ERRFRAME,
5153
PCAN_MESSAGE_ESI,
5254
PCAN_MESSAGE_EXTENDED,
@@ -120,6 +122,7 @@ def __init__(
120122
state: BusState = BusState.ACTIVE,
121123
timing: Optional[Union[BitTiming, BitTimingFd]] = None,
122124
bitrate: int = 500000,
125+
receive_own_messages: bool = False,
123126
**kwargs: Any,
124127
):
125128
"""A PCAN USB interface to CAN.
@@ -162,6 +165,9 @@ def __init__(
162165
Default is 500 kbit/s.
163166
Ignored if using CanFD.
164167
168+
:param receive_own_messages:
169+
Enable self-reception of sent messages.
170+
165171
:param bool fd:
166172
Should the Bus be initialized in CAN-FD mode.
167173
@@ -316,6 +322,14 @@ def __init__(
316322
"Ignoring error. PCAN_ALLOW_ERROR_FRAMES is still unsupported by OSX Library PCANUSB v0.11.2"
317323
)
318324

325+
if receive_own_messages:
326+
result = self.m_objPCANBasic.SetValue(
327+
self.m_PcanHandle, PCAN_ALLOW_ECHO_FRAMES, PCAN_PARAMETER_ON
328+
)
329+
330+
if result != PCAN_ERROR_OK:
331+
raise PcanCanInitializationError(self._get_formatted_error(result))
332+
319333
if kwargs.get("auto_reset", False):
320334
result = self.m_objPCANBasic.SetValue(
321335
self.m_PcanHandle, PCAN_BUSOFF_AUTORESET, PCAN_PARAMETER_ON
@@ -548,6 +562,7 @@ def _recv_internal(
548562
is_extended_id = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_EXTENDED.value)
549563
is_remote_frame = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_RTR.value)
550564
is_fd = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_FD.value)
565+
is_rx = not bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ECHO.value)
551566
bitrate_switch = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_BRS.value)
552567
error_state_indicator = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ESI.value)
553568
is_error_frame = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ERRFRAME.value)
@@ -575,6 +590,7 @@ def _recv_internal(
575590
dlc=dlc,
576591
data=pcan_msg.DATA[:dlc],
577592
is_fd=is_fd,
593+
is_rx=is_rx,
578594
bitrate_switch=bitrate_switch,
579595
error_state_indicator=error_state_indicator,
580596
)

0 commit comments

Comments
 (0)