16
16
from random import randint
17
17
18
18
from can import CanOperationError
19
- from can .bus import BusABC
19
+ from can .bus import BusABC , CANProtocol
20
20
from can .message import Message
21
21
from can .typechecking import AutoDetectedConfig
22
22
34
34
35
35
class VirtualBus (BusABC ):
36
36
"""
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.
38
39
39
40
In this interface, a channel is an arbitrary object used as
40
41
an identifier for connected buses.
@@ -49,9 +50,11 @@ class VirtualBus(BusABC):
49
50
if a message is sent to 5 receivers with the timeout set to 1.0.
50
51
51
52
.. 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.
55
58
"""
56
59
57
60
def __init__ (
@@ -60,10 +63,41 @@ def __init__(
60
63
receive_own_messages : bool = False ,
61
64
rx_queue_size : int = 0 ,
62
65
preserve_timestamps : bool = False ,
66
+ protocol : CANProtocol = CANProtocol .CAN_20 ,
63
67
** kwargs : Any ,
64
68
) -> 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
+ """
65
96
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 ,
67
101
)
68
102
69
103
# the channel identifier may be an arbitrary object
0 commit comments