diff --git a/can/interfaces/pcan/pcan.py b/can/interfaces/pcan/pcan.py index 884c1680b..7172da239 100644 --- a/can/interfaces/pcan/pcan.py +++ b/can/interfaces/pcan/pcan.py @@ -26,6 +26,7 @@ FEATURE_FD_CAPABLE, IS_LINUX, IS_WINDOWS, + PCAN_ALLOW_ECHO_FRAMES, PCAN_ALLOW_ERROR_FRAMES, PCAN_API_VERSION, PCAN_ATTACHED_CHANNELS, @@ -47,6 +48,7 @@ PCAN_LANBUS1, PCAN_LISTEN_ONLY, PCAN_MESSAGE_BRS, + PCAN_MESSAGE_ECHO, PCAN_MESSAGE_ERRFRAME, PCAN_MESSAGE_ESI, PCAN_MESSAGE_EXTENDED, @@ -120,6 +122,7 @@ def __init__( state: BusState = BusState.ACTIVE, timing: Optional[Union[BitTiming, BitTimingFd]] = None, bitrate: int = 500000, + receive_own_messages: bool = False, **kwargs: Any, ): """A PCAN USB interface to CAN. @@ -162,6 +165,9 @@ def __init__( Default is 500 kbit/s. Ignored if using CanFD. + :param receive_own_messages: + Enable self-reception of sent messages. + :param bool fd: Should the Bus be initialized in CAN-FD mode. @@ -316,6 +322,14 @@ def __init__( "Ignoring error. PCAN_ALLOW_ERROR_FRAMES is still unsupported by OSX Library PCANUSB v0.11.2" ) + if receive_own_messages: + result = self.m_objPCANBasic.SetValue( + self.m_PcanHandle, PCAN_ALLOW_ECHO_FRAMES, PCAN_PARAMETER_ON + ) + + if result != PCAN_ERROR_OK: + raise PcanCanInitializationError(self._get_formatted_error(result)) + if kwargs.get("auto_reset", False): result = self.m_objPCANBasic.SetValue( self.m_PcanHandle, PCAN_BUSOFF_AUTORESET, PCAN_PARAMETER_ON @@ -548,6 +562,7 @@ def _recv_internal( is_extended_id = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_EXTENDED.value) is_remote_frame = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_RTR.value) is_fd = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_FD.value) + is_rx = not bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ECHO.value) bitrate_switch = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_BRS.value) error_state_indicator = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ESI.value) is_error_frame = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ERRFRAME.value) @@ -575,6 +590,7 @@ def _recv_internal( dlc=dlc, data=pcan_msg.DATA[:dlc], is_fd=is_fd, + is_rx=is_rx, bitrate_switch=bitrate_switch, error_state_indicator=error_state_indicator, )