1. 1. Install CAN-Utils
Method 1:
Using the command
sudo apt-get install can-utils
(OR)
Method2:
Download from https://github.com/linux-can/can-utils
2. Afterdownloadinguse the followingcommands
unzipcan-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
3. Kernel modules can be found in /lib/modules/<kernel_version>/net/can/
Sourcecode is available at https://github.com/torvalds/linux/tree/master/net/can
(or) <KERNEL_SRC>/net/can . Kernel sourcecan 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 abovecommands to a virtual CAN node vcan1
To list the details of network interfaces ,use ifconfig
Use ifconfigvcan0 to know details of vcan0
4.UsingCAN-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
4. Frequently used tools are highlighted
a.cansend and candump
To send a CAN message, using cansend there are two requirements 11 bit(0x000 to0x7FF)
/29 bit(0x00000000 to0x1FFFFFFF) identifier and message of length ranging from 0 to 8 bytes
To send a message(0x6789) of 2 bytes with 11bitidentifier (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,usecansendvcan0 0x0123456789ABCDEF#8679
Using Acceptance filtering (Refer https://discuss.cantact.io/t/using-can-utils/24 )
To filter messages whoseID is in the range 0x120 to 0x12F
Use candump vcan0,120:0x7F0
Messages arefiltered 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
5. b.Using the CAN gateway utility (cangw):
Ensurethat kernelmodule 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 sourceas 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 sourceas 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
6. d. using cangen
For more help and information, <utility_name> --help
Eg: cangw --help
cansend --help
candump --help