diff --git a/src/python_can_cansub/cansub.py b/src/python_can_cansub/cansub.py index 37cfa1e..c386656 100644 --- a/src/python_can_cansub/cansub.py +++ b/src/python_can_cansub/cansub.py @@ -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 @@ -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, @@ -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: """ diff --git a/tests/bus/test_open_close.py b/tests/bus/test_open_close.py index 7547d5b..b89a386 100644 --- a/tests/bus/test_open_close.py +++ b/tests/bus/test_open_close.py @@ -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"}