Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket-CAN virtual bus

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.

like image 384
Shabbir Hussain Avatar asked Nov 06 '15 19:11

Shabbir Hussain


People also ask

What is a virtual 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.

How does SocketCAN work?

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...

CAN interface in Linux?

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.


1 Answers

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
like image 60
yegorich Avatar answered Sep 22 '22 04:09

yegorich