CAN¶
CAN Interfaces¶
Linux provides CAN driver for physical available CAN controller and for virtual created CAN adapter so called vcan.
Physical CAN Interface¶
Physical can interfaces depends on hardware and driver support. To check if physical can interfaces are available do:
ifconfig -a | grep can
Output should be similar to following:
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
can1 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
Virtual CAN Interface - vcan¶
To bring up virtual can interface the kernel module vcan is required. Load vcan module:
modprobe vcan
And controls whether the module is loaded successfully:
lsmod | grep vcan
Output should be similar to following:
vcan 16384 0
Now a virtual can interface vcan0 can be created:
ip link add dev vcan0 type vcan
ip link set vcan0 mtu 16
ip link set up vcan0
To bring up CAN FD interface mtu size must increased to 72:
ip link add dev vcan0 type vcan
ip link set vcan0 mtu 72
ip link set up vcan0
And again control new created virtual can interface:
ifconfig vcan0
Output should be similar to following:
vcan0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP RUNNING NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
From this point the virtual can interface vcan0 can be used e.g. for SocketCAN.
SocketCAN¶
For linux based systems there are severals user space tools available to tunnel virtual can adapter over ethernet e.g. cannelloni or socketcand. Only cannelloni is treated here.
Bind virtual can adapter vcan0 to any counterpart:
cannelloni -I vcan0 -R <remote ip> -r <remote port> -l <local port>
For non blocking console append a &
in the command above.
SocketCAN Between Two Linux Machines¶
Example use of SocketCAN between two machines.
Config | MACHINE 0 | MACHINE 1 |
---|---|---|
IP | 192.168.1.100 | 192.168.1.200 |
Local cannelloni port | 2000 | 2000 |
MACHINE 0
cannelloni -I vcan0 -R 192.168.1.200 -r 2000 -l 2000
MACHINE 1
cannelloni -I vcan0 -R 192.168.1.100 -r 2000 -l 2000
External Physical CAN Interface¶
Ensure that any can participant is on can bus. For communication verification a can PC interface is recommendend. Check also that physical bus is proper terminated with 120 Ohm impedance.
can-utils¶
can-utils provides severals tools to e.g. interact and monitor general can interfaces.
To send can frames to vcan0 command cansend can be used:
cansend <device> <can_frame>
# example
cansend vcan0 5A2#11.2233.445D556677.66
To dump can frames on a can interface use command candump:
candump <device>
# example
candump vcan0