can in linux
TRANSCRIPT
1. Install CAN-UtilsMethod 1:
Using the commandsudo apt-get install can-utils
(OR)Method 2:Download from https://github.com/linux-can/can-utils
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 commandssudo 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/
Source code is available at https://github.com/torvalds/linux/tree/master/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
https://www.kernel.org/doc/Documentation/networking/can.txt
For working on real CAN hardwarehttps://harrisonsand.com/can-on-the-raspberry-pi/
( http://www.ebay.in/itm/MCP2515-CAN-Bus-Module-TJA1050-receiver-SPI-For-51-MCU-ARM-controller-/131725452389 )
Author : Jabez Winston
www.linkedin.com/in/jabezwinston