USBtin and Linux-CAN

USBtin, a USB-to-CAN-bus interface can be used with the linux-can (SocketCAN), the offical CAN implentation for Linux. There are a few simple steps to get USBtin working under Linux.

Prepare linux-can and CAN utils

Before we can use the USBtin, we have to prepare the linux-can (SocketCAN).

Load kernel modules

Load the kernel modules we need for CAN. On Ubuntu these modules are already available, so we don't need to compile it.
$ sudo modprobe can
$ sudo modprobe can-raw
$ sudo modprobe slcan

Determine serial device

Plug in USBtin and look into log files to get the corresponding CDC device (here ttyACM0):
$ tail /var/log/kern.log
kernel: [ ] usb 1-4.2.3: new full-speed USB device number 8 using ehci-pci
kernel: [ ] usb 1-4.2.3: New USB device found, idVendor=04d8, idProduct=000a
kernel: [ ] usb 1-4.2.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
kernel: [ ] usb 1-4.2.3: Product: USBtin
kernel: [ ] cdc_acm 1-4.2.3:1.0: This device cannot do calls on its own. It is not a modem.
kernel: [ ] cdc_acm 1-4.2.3:1.0: ttyACM0: USB ACM device
kernel: [ ] usbcore: registered new interface driver cdc_acm
kernel: [ ] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

Prepare CAN utils

Get and compile the en CAN utils:
$ git clone https://github.com/linux-can/can-utils.git
$ cd can-utils
$ make

Attach and startup interface

The parameter "s5" stands for baudrate 250k. For non-standard baudrates, please use parameter "b" to set MCP2515 register values CNF1, CNF2, CNF3 (e.g. "-bd1ad06" sets 33333 Baud).
$ sudo ./slcan_attach -f -s5 -o /dev/ttyACM0
attached tty /dev/ttyACM0 to netdevice slcan0
$ sudo ./slcand ttyACM0 slcan0
$ sudo ifconfig slcan0 up
Now, the linux-can and the CAN utils are ready and can be used.

Use the command line CAN utils

Linux-can and utils prepared? It's time to use it!

Dump can messages

Display the messages received with the CAN interface.
./candump slcan0

Send sequences of CAN frames

For some tests its very useful to send sequences. First prepare a file with the messages:
$ cat test.log 
(0.1) slcan0 5D1#0000
(0.2) slcan0 271#0100
(0.3) slcan0 289#72027000
(0.4) slcan0 401#081100000000
And now play it:
$ ./canplayer -l 20 -I test.log -v -g500

Use Wireshark to capture CAN frames

An alternative to the commandline dump tool is en Wireshark, a graphical protocol analyser. Just install it on your system (e.g. "apt-get install wireshark") and choose your CAN device as capture device (eg. "slcan0").

Wireshark capturing CAN messages

Raspberry Pi - Raspbian

Raspbian doesn't have built in support for the CAN bus in the kernel. But it can be enabled. This article describes how to enable CANbus support and how to compile the kernel sources: en RPi CANBus

After installing the new kernel and its modules, the steps described above can be used to activate the converter on the RPi.

Links

USBtin - Simple USB to CAN interface