Skip to content

Enable echo frames in PCAN driver if receive_own_messages is set #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions can/interfaces/pcan/pcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,6 +48,7 @@
PCAN_LANBUS1,
PCAN_LISTEN_ONLY,
PCAN_MESSAGE_BRS,
PCAN_MESSAGE_ECHO,
PCAN_MESSAGE_ERRFRAME,
PCAN_MESSAGE_ESI,
PCAN_MESSAGE_EXTENDED,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down