Multicast IP Interface¶
This module implements transport of CAN and CAN FD messages over UDP via Multicast IPv4 and IPv6. This virtual interface allows for communication between multiple processes and even hosts. This differentiates it from the Virtual interface, which can only passes messages within a single process but does not require a network stack.
It runs on UDP to have the lowest possible latency (as opposed to using TCP), and because normal IP multicast is inherently unreliable, as the recipients are unknown. This enables ad-hoc networks that do not require a central server but is also a so-called unreliable network. In practice however, local area networks (LANs) should most often be sufficiently reliable for this interface to function properly.
For an overview over the different virtual buses in this library and beyond, please refer to the section Other Virtual Interfaces. It also describes important limitations of this interface.
Please refer to the Bus class documentation below for configuration options and useful resources for specifying multicast IP addresses.
It should work on most Unix systems (including Linux with kernel 2.6.22+) but currently not on Windows.
This example should print a single line indicating that a CAN message was successfully sent
import time import can from can.interfaces.udp_multicast import UdpMulticastBus # The bus can be created using the can.Bus wrapper class or using UdpMulticastBus directly with can.Bus(channel=UdpMulticastBus.DEFAULT_GROUP_IPv6, bustype='udp_multicast') as bus_1, \ UdpMulticastBus(channel=UdpMulticastBus.DEFAULT_GROUP_IPv6) as bus_2: # register a callback on the second bus that prints messages to the standard out notifier = can.Notifier(bus_2, [can.Printer()]) # create and send a message with the first bus, which should arrive at the second one message = can.Message(arbitration_id=0x123, data=[1, 2, 3]) bus_1.send(message) # give the notifier enough time to get triggered by the second bus time.sleep(2.0)
Bus Class Documentation¶
UdpMulticastBus(channel='ff15:7079:7468:6f6e:6465:6d6f:6d63:6173', port=43113, hop_limit=1, receive_own_messages=False, fd=True, **kwargs)¶
A virtual interface for CAN communications between multiple processes using UDP over Multicast IP.
It supports IPv4 and IPv6, specified via the channel (which really is just a multicast IP address as a string). You can also specify the port and the IPv6 hop limit/the IPv4 time to live (TTL).
This bus does not support filtering based on message IDs on the kernel level but instead provides it in user space (in Python) as a fallback.
Both default addresses should allow for multi-host CAN networks in a normal local area network (LAN) where multicast is enabled.
The auto-detection of available interfaces (see) is implemented using heuristic that checks if the required socket operations are available. It then returns two configurations, one based on the
DEFAULT_GROUP_IPv6address and another one based on the
The parameter receive_own_messages is currently unsupported and setting it to True will raise an exception.
This interface does not make guarantees on reliable delivery and message ordering, and also does not implement rate limiting or ID arbitration/prioritization under high loads. Please refer to the section Other Virtual Interfaces for more information on this and a comparison to alternatives.
str) – A multicast IPv4 address (in 18.104.22.168/4) or an IPv6 address (in ff00::/8). This defines which version of IP is used. See Wikipedia (“Multicast address”) for more details on the addressing schemes. Defaults to
int) – The IP port to read from and write to.
int) – The hop limit in IPv6 or in IPv4 the time to live (TTL).
bool) – If transmitted messages should also be received by this bus. CURRENTLY UNSUPPORTED.
bool) – If CAN-FD frames should be supported. If set to false, an error will be raised upon sending such a frame and such received frames will be ignored.
can_filters – See
Construct and open a CAN bus instance of the specified type.
Subclasses should call though this method with all given parameters as it handles generic tasks like applying filters.
ValueError – If parameters are out of range
can.CanInterfaceNotImplementedError – If the driver cannot be accessed
can.CanInitializationError – If the bus cannot be initialized
An arbitrary IPv4 multicast address with “administrative” scope, i.e. only to be routed within administrative organizational boundaries and not beyond it. It should allow for multi-host CAN networks in a normal IPv4 LAN. This is provided as a default fallback channel if IPv6 is (still) not supported.
An arbitrary IPv6 multicast address with “site-local” scope, i.e. only to be routed within the local physical network and not beyond it. It should allow for multi-host CAN networks in a normal IPv6 LAN. This is the default channel and should work with most modern routers if multicast is allowed.
Provides the internally used file descriptor of the socket or -1 if not available.
- Return type