How can I create several virtual sockets and link them together to create a virtual bus?
I want to simulate an application in which many nodes communicate to each other via CAN.
Virtual CAN devices, or vcan devices, can be used to simulate a CAN bus without any hardware. This is useful for simulation, testing, and bridging. It also lets you try out can-utils without having an actual CAN device. To create a vcan device run: sudo ip link add name vcan0 type vcan.
Usage. The application first sets up its access to the CAN interface by initialising a socket (much like in TCP/IP communications), then binding that socket to an interface (or all interfaces, if the application so desires). Once bound, the socket can then be used like a UDP socket via read , write , etc...
The CAN network device driver interface provides a generic interface to setup, configure and monitor CAN network devices. The user can then configure the CAN device, like setting the bit-timing parameters, via the netlink interface using the program “ip” from the “IPROUTE2” utility suite.
All you need is cangw tool from can-utils. Create two virtual interfaces:
ip link add dev vcan0 type vcan
ip link add dev vcan1 type vcan
ip link set up vcan0
ip link set up vcan1
Create routing rule, so that all packets coming to vcan0 will be sent to vcan1:
cangw -A -s vcan0 -d vcan1 -e
Listen to vcan1 in one terminal:
candump vcan1
And send packet from another terminal:
cansend vcan0 123#0011
You'll see, that candump will get this CAN packet:
vcan1 123 [2] 00 11
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With