Skip to content

Commit 07defb0

Browse files
committed
Implement CANProtocol for VirtualBus
1 parent 059f33a commit 07defb0

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

can/interfaces/virtual.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from random import randint
1717

1818
from can import CanOperationError
19-
from can.bus import BusABC
19+
from can.bus import BusABC, CANProtocol
2020
from can.message import Message
2121
from can.typechecking import AutoDetectedConfig
2222

@@ -34,7 +34,8 @@
3434

3535
class VirtualBus(BusABC):
3636
"""
37-
A virtual CAN bus using an internal message queue. It can be used for example for testing.
37+
A virtual CAN bus using an internal message queue. It can be used for
38+
example for testing.
3839
3940
In this interface, a channel is an arbitrary object used as
4041
an identifier for connected buses.
@@ -49,9 +50,11 @@ class VirtualBus(BusABC):
4950
if a message is sent to 5 receivers with the timeout set to 1.0.
5051
5152
.. warning::
52-
This interface guarantees reliable delivery and message ordering, but does *not* implement rate
53-
limiting or ID arbitration/prioritization under high loads. Please refer to the section
54-
:ref:`virtual_interfaces_doc` for more information on this and a comparison to alternatives.
53+
This interface guarantees reliable delivery and message ordering, but
54+
does *not* implement rate limiting or ID arbitration/prioritization
55+
under high loads. Please refer to the section
56+
:ref:`virtual_interfaces_doc` for more information on this and a
57+
comparison to alternatives.
5558
"""
5659

5760
def __init__(
@@ -60,10 +63,41 @@ def __init__(
6063
receive_own_messages: bool = False,
6164
rx_queue_size: int = 0,
6265
preserve_timestamps: bool = False,
66+
protocol: CANProtocol = CANProtocol.CAN_20,
6367
**kwargs: Any,
6468
) -> None:
69+
"""
70+
The constructed instance has access to the bus identified by the
71+
channel parameter. It is able to see all messages transmitted on the
72+
bus by virtual instances constructed with the same channel identifier.
73+
74+
:param channel: The channel identifier. This parameter can be an
75+
arbitrary value. The bus instance will be able to see messages
76+
from other virtual bus instances that were created with the same
77+
value.
78+
:param receive_own_messages: If set to True, sent messages will be
79+
reflected back on the input queue.
80+
:param rx_queue_size: The size of the reception queue. The reception
81+
queue stores messages until they are read. If the queue reaches
82+
its capacity, it will start dropping the oldest messages to make
83+
room for new ones. If set to 0, the queue has an infinite capacity.
84+
Be aware that this can cause memory leaks if messages are read
85+
with a lower frequency than they arrive on the bus.
86+
:param preserve_timestamps: If set to True, messages transmitted via
87+
:func:`~can.BusABC.send` will keep the timestamp set in the
88+
:class:`~can.Message` instance. Otherwise, the timestamp value
89+
will be replaced with the current system time.
90+
:param protocol: The protocol implemented by this bus instance. The
91+
value does not affect the operation of the bus instance and can
92+
be set to an arbitrary value for testing purposes.
93+
:param kwargs: Additional keyword arguments passed to the parent
94+
constructor.
95+
"""
6596
super().__init__(
66-
channel=channel, receive_own_messages=receive_own_messages, **kwargs
97+
channel=channel,
98+
receive_own_messages=receive_own_messages,
99+
protocol=protocol,
100+
**kwargs,
67101
)
68102

69103
# the channel identifier may be an arbitrary object

0 commit comments

Comments
 (0)