From 2b7f9fd3957cebeceba0cf6b7d42fa607d618b36 Mon Sep 17 00:00:00 2001 From: mfcss Date: Mon, 30 Nov 2026 09:31:08 +0100 Subject: [PATCH] Reject send() in listen-only mode --- src/python_can_cansub/cansub.py | 3 +++ tests/send_recv/test_send_recv.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/python_can_cansub/cansub.py b/src/python_can_cansub/cansub.py index 37cfa1e..4190105 100644 --- a/src/python_can_cansub/cansub.py +++ b/src/python_can_cansub/cansub.py @@ -905,6 +905,9 @@ def send(self, msg: can.Message, timeout: Optional[float] = None): raise can.exceptions.CanOperationError( f"Bus is not operational ({self._io_error or 'shut down or connection lost'})") + if self.listen_only: + raise can.exceptions.CanOperationError("Cannot send in listen-only mode") + if timeout is not None and timeout < 0: raise can.exceptions.CanOperationError("Invalid timeout value") diff --git a/tests/send_recv/test_send_recv.py b/tests/send_recv/test_send_recv.py index 7b91fff..f6e41a9 100644 --- a/tests/send_recv/test_send_recv.py +++ b/tests/send_recv/test_send_recv.py @@ -126,3 +126,9 @@ def test_high_load(self, bus_config): rx_ids = [msg.arbitration_id for msg in rx_msgs] assert rx_ids == list(range(test_message_count)), \ f"sent {test_message_count} messages, received {len(rx_ids)}" + + def test_send_listen_only(self, bus_config): + """Check that sending on a listen-only bus fails (the device would drop the message).""" + with can.Bus(channel=1, listen_only=True, **bus_config) as bus: + with pytest.raises(can.CanOperationError): + bus.send(can.Message(arbitration_id=0x123, is_extended_id=False))