Friday, 31 March 2017

Controller Area Netrwork (CAN) in Linux

1. Install CAN-Utils

Method 1:

Using the command
sudo apt-get install can-utils




(OR)



After downloading use the following commands

unzip can-utils-master.zip
cd can-utils-master
make
sudo make install

2.Load CAN drivers (Reference: https://en.wikipedia.org/wiki/SocketCAN )

Use the following commands

sudo modprobe can
sudo modprobe vcan
sudo modprobe can_raw
sudo modprobe can_gw

To list the kernel modules
lsmod



Kernel modules can be found in /lib/modules/<kernel_version>/net/can/
(or) <KERNEL_SRC>/net/can . Kernel source can be downloaded from www.kernel.org


3.Creating virtual CAN node( Reference: https://en.wikipedia.org/wiki/SocketCAN )
Use the following commands to create a virtual CAN node vcan0

sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0

Similarly repeat the above commands to a virtual CAN node vcan1



To list the details of network interfaces ,use ifconfig
Use ifconfig vcan0 to know details of vcan0




4.Using CAN-Utils

CAN Utils contains the following tools
asc2log, bcmserver, canbusload, can-calc-bit-timing, candump, canfdtest, cangen, cangw, canlogserver, canplayer, cansend, cansniffer, isotpdump, isotprecv, isotpperf, isotpsend, isotpserver, isotpsniffer, isotptun, log2asc, log2long, slcan_attach, slcand and slcanpty

Frequently used tools are highlighted

 a.cansend and candump

To send a CAN message , using cansend there are two requirements 11 bit(0x000 to 0x7FF) /29 bit(0x00000000 to 0x1FFFFFFF) identifier and message of length ranging from 0 to 8 bytes

To send a message (0x6789) of 2 bytes with 11bit identifier (0x000) to interface vcan0,use cansend vcan0 000#6789

To send a message (0x8679) of 8 bytes with 29 bit identifier (0x0123456789ABCDEF) to interface vcan0,use cansend vcan0 0x0123456789ABCDEF#8679



Using Acceptance filtering (Refer https://discuss.cantact.io/t/using-can-utils/24 )

To filter messages whose ID is in the range  0x120 to 0x12F
Use candump vcan0,120:0x7F0
Messages are filtered based on this condition
[received_can_id] & [can_mask] == [can_id] & [can_mask]
Here can_id = 0x120 and can_mask =0x7F0
& is a bitwise operator



b.Using the CAN gateway utility (cangw):

Ensure that kernel module can_gw  is loaded.You can check it using the command lsmod

sudo cangw -F                                                            
 #deletes all gateway rules

sudo cangw -A -s vcan0 -d vcan1 -e -f 120:7F0      
 #Adds a new rule with source as vcan0 , destination as vcan1 with echo enabled and  which accepts messages of ID in the range     0x120 to 0x12F

sudo cangw -A -s vcan0 -d vcan1 -e -f 200:7F0       
#Adds a new rule with source as vcan0 destination as vcan1 with echo enabled and   which accepts messages of ID in the range  0x200 to 0x20F

cangw -L           
#Lists all gateway rules



d. using cangen
For more help and information, <utility_name> --help

Eg:       cangw --help
      cansend --help
      candump --help




Linux CAN Subsystem

For working on real CAN hardware
https://harrisonsand.com/can-on-the-raspberry-pi/


No comments:

Post a Comment