linuxvirtualcan-bussocketcancanutils

How to connect two VCAN ports in Linux?


I have set up two vcan devices and I want to treat them as if they were connected together. I want to be able to use can-utils candump to listen on one line, and send can messages from the other. How can I do that without a physical device?

For clarification, I am writing software to communicate over CAN which I am already capable of, but to facilitate the actual data gathering and further development, I need two can enabled devices to communicate with each other. While a simple solution in the real world, solved by physically connecting devices together, I need a programmatic solution that will work on the computer.


Solution

  • Add the can gateway kernel module:

    sudo modprobe can-gw
    

    Then create gateway rules via cangw, which comes with can-utils, for the respective interfaces.
    For example, for routing messages from vcan0 to vcan1:

    sudo cangw -A -s vcan0 -d vcan1 -e 
    

    and the other way around:

    sudo cangw -A -s vcan1 -d vcan0 -e
    

    Now you will be able to see all messages sent on vcan0 also on vcan1 and vice versa.