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
3 changes: 3 additions & 0 deletions src/python_can_cansub/cansub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
6 changes: 6 additions & 0 deletions tests/send_recv/test_send_recv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))