Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions src/python_can_cansub/cansub.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def __init__(self,
# listen-only
if not isinstance(listen_only, bool):
raise can.exceptions.CanInitializationError("Invalid listen_only type")
self.listen_only = listen_only
self._listen_only = listen_only
del listen_only

# auto-reset
Expand Down Expand Up @@ -428,7 +428,7 @@ def __init__(self,

# Set channel configuration
phy_config = {
"listen_only": self.listen_only,
"listen_only": self._listen_only,
"auto_reset": self.auto_reset,
"error_frames": self.error_frames,
"tx_ack_frames": self.receive_own_messages,
Expand Down Expand Up @@ -981,6 +981,13 @@ def _recv_internal(self, timeout: Optional[float] = None) -> Tuple[Optional[can.

return msg, True

@property
def listen_only(self) -> bool:
"""
Whether the bus was opened in listen-only mode (read-only; set via the constructor)
"""
return self._listen_only

@property
def state(self) -> can.BusState:
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/bus/test_open_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def test_open(self, bus_config):
assert open_time < 0.5, f"open took {open_time:.3f} s"
assert close_time < 0.1, f"close took {close_time:.3f} s"

def test_open_listen_only(self, bus_config):
"""Open a bus in listen-only mode, and check that it reports the mode."""
with can.Bus(channel=1, listen_only=True, **bus_config) as bus:
assert bus.listen_only is True

def test_open_no_data_bitrate(self, bus_config):
"""Open a bus with no data bitrate, and check that it operates as (ISO) CAN 2.0."""
classic_config = {key: value for key, value in bus_config.items() if key != "data_bitrate"}
Expand Down