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))