Samstag, 15. August 2015

Step-by-step guide: Installing the Lawicel CANUSB adapter on Linux

This tutorial explains how to get the Lawicel CANUSB device running and automatically loaded when it’s plugged in. You’ll need access to an operating CAN network with at least one sending node to test. Other CAN interfaces may work as well, as long as they’re using „slcan“ (interfaces not using "slcan" are set up very different from what you'll read here). Tested on Ubuntu 14.04. If you want to run it on a Raspberry Pi, note that you need to use the Raspberry Pi 2, since the RPi1 doesn’t support Ubuntu. Using Raspbian would also be possible, but requires compiling the kernel including the slcan drivers (which means a lot of work).

Follow the following steps carefully:

1. Install the „can-utils“ package (which includes the required slcan daemon):
sudo apt-get install can-utils

2. Check if the kernel modules „can“, „can_raw“ and „slcan“ are already loaded:
If not, load them manually:
sudo modprobe can
sudo modprobe can_raw
sudo modprobe slcan
If modprobe says „Module not found“ at this point, your linux distribution is missing the can modules. If modprobe says nothing, the modules exist. Now let’s make them load automatically:
sudo nano /etc/modules
Add the three modules „can“, „can_raw“, „slcan“ to the list (each goes in one line).

3. Connect the CANUSB to the CAN network, plug it into the USB port and check if it shows up:
It should be listed as „Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC“. Also check that it shows up as device „ttyUSBX“ (in most cases „ttyUSB0“):
ls /dev
If it doesn’t show up, install the FTDI VCP drivers (see

4. Now we will bind the CANUSB device to the slcan interface:
The following table contains all possible CAN bus speed settings:
CAN speed
10 Kbit/s
20 Kbit/s
50 Kbit/s
100 Kbit/s
125 Kbit/s
250 Kbit/s
500 Kbit/s
800 Kbit/s
1000 Kbit/s
Bind the interface (replace the parameters –sX and ttyUSBX with the correct values):
sudo slcand -o -c -f -sX /dev/ttyUSBX slcan0
Now check that the interface has been added successfully (the following command should list „slcan0“ in state „DOWN“):
ip addr
If it’s not in the list, check the system log file for errors:
sudo tail -n 10 -f /var/log/syslog

5. Bring the slcan interface up:
sudo ifconfig slcan0 up
Before proceeding, check the interface state – it should’ve changed from „DOWN“ to „UNKNOWN“:
ip addr

6. Now check for CAN messages (make sure at least one CAN node is sending):
candump slcan0
You should see CAN messages now. If not, check the green LED (it should light up whenever a CAN message is received). Also make sure you chose the correct CAN bus speed setting and the CAN bus has at least one active sending node. Exit the program using CTRL+C.

7. Now we will automate the linking process (steps 3 to 5) so the system will perform it whenever we plug in the CANUSB interface. First create a new udev rule:
sudo nano /etc/udev/rules.d/90-slcan.rules
Add the following content:
# Lawicel CANUSB module
ACTION=="add", ENV{ID_MODEL}=="CANUSB", ENV{SUBSYSTEM}=="tty", RUN+="/usr/bin/logger [udev] Lawicel CANUSB detected - running!", RUN+="/usr/local/bin/ $kernel"
ACTION=="remove", ENV{ID_MODEL}=="CANUSB", ENV{SUBSYSTEM}=="usb", RUN+="/usr/bin/logger [udev] Lawicel CANUSB removed - running!", RUN+="/usr/local/bin/"
Make sure that you removed the line breaks (each ACTION command needs to be in it’s own line).

8. Create the script that will be called by udev when CANUSB is plugged in:
sudo nano /usr/local/bin/
Add the following content:
# Bind the USBCAN device
slcand -o -c -f -sX /dev/$1 slcan0
sleep 2
ifconfig slcan0 up 
Reminder: Fill in the -sX parameter to suit your CAN bus speed!

9. Create the script that will be called by udev when CANUSB is removed:
sudo nano /usr/local/bin/
Add the following content:
# Remove the USBCAN device
pkill slcand

10. Make both scripts executable:
sudo chmod +x /usr/local/bin/
sudo chmod +x /usr/local/bin/
Now remove the CANUSB from the USB port.

11. Reboot to make udev apply the new rule:
sudo reboot
Plug the CANUSB back in.

12. Check slcan for functionality again:
candump slcan0
If it doesn’t work, check the system log for errors and use the udev monitor for hints:
sudo tail -n 10 -f /var/log/syslog
udevadm monitor --property

Done! From now on the Lawicel CANUSB interface can be plugged in and out at any time and it should set up automatically.
- The Lawicel CANUSB interface uses the FTDI Serial-to-USB chip, which comes with two drivers: The D2XX (direct device access) and the VCP (ascii based virtual com port), which we use.
- Newer linux distributions have the required SocketCAN kernel modules included. There are 3 types available: Native CAN („can“), virtual CAN („vcan“) and serial CAN („slcan“). Since we are using the VCP driver, we are using slcan.
- The green LED on the CANUSB indicates that CAN frames have been received or sent (they’re only working if the correct CAN bus speed has been set). The red LED shows if the module is in errornous state – to fix, clear the error bit by software (or replug and reboot).

23 Kommentare:

  1. Nice summary, thanks for providing it! Great help to get started with CANUSB in a Linux environment. Best regards, Oliver

  2. Great tutorial. It is working fine except the automatic plug. In the syslog you can see that everything is OK, but the daemon avahi is making trouble:

    raspberrypi avahi-daemon[426]: Withdrawing workstation service for slcan0

    Any idea to solve this ?
    Cheers Dirk

    1. Thanks! Sorry I never used avahi, but this sounds more like a note to me. Since it wouldn't make sense for avahi to add a can-interface, it just ignores that interface. To make that hint disappear, i think you need to open the avari configuration file (i.e. /etc/avahi/avahi-daemon.conf) and add "slcan0" to the line "deny-interfaces=".

    2. It is the NetworkManager killing the can device.
      NetworkManager[604]: devices removed (path: /sys/devices/virtual/net/slcan0, iface: slcan0

      None of the tips found across the internet seem to work. I found the following:

      Change to contain:
      echo "/usr/local/bin/ $1" | /usr/bin/batch

      then put the former content of in a new script Don't forget to make this one executable too.

      Some other changes I made:
      - modprobe can_raw first. It will pull the module can itself.
      - In the udev script, changed ENV{ID_MODEL}=="CANUSB" to ENV{ID_MODEL_ID}=="6001". Then it will work with other devices based on the same chip, not only Lawicel.

    3. Oh thanks for this tip about the network manager interferring. Worked for me after installing batch through 'apt install at'.

  3. thanks for that can bus speed table. you have no idea how long i've been searching for that

  4. Sorry for the typo previously...

    When the CANUSB is detected as ttyUSB1, it shall be bind as slcan1. I guess socketcan binds ttyUSBx to slcanx. How shall I modify the script for this? Thanks!

    1. The binding process of ttyUSBx to slcanx is not made by socketcan automatically. You have to do it on your own (see step 4). If the CANUSB on your machine is detected as ttyUSB1, and you want the can interface name to be slcan1, the command would be:
      sudo slcand -o -c -f -sX /dev/ttyUSB1 slcan1
      Note: You may also name the can interface "donald_trump" instead of "slcan1". It's up to you :-)
      Note2: Don't forget to replace parameter "-sX" by the according speed parameter from the table (see step 4).

  5. Hi,

    I want to install virtual can driver interface (vcan0) in one of my ARM device. When i give modprobe vcan the responce is : module vcan not found. So can any one suggest where exactly i get the kernel object vcan fro my ARM device

    1. I'm sorry, as a linux beginner I can't help you on this. :-(

  6. Hi,
    I followed your steps, but i m not able to read the data in termninal after giving command as "candump slcan0"
    Please help me with this.

    1. Assuming that you didn't get any error from candump, this could cause the trouble:
      1. The wrong bus speed has been set (see #4).
      2. There is no activity on the CAN bus.
      3. The CAN bus is not terminated properly (this is very important, even if you only connect to one single CAN node).

      You may also try opening up another console, and use the tool "cansend" to send some data. You should immediately see it on candump.

  7. Der Kommentar wurde von einem Blog-Administrator entfernt.

  8. Hello!
    These are great instructions, and I got them to work no problem on my laptop that has Ubuntu 16.04 LTS. I can effectively send CAN frames to the device I wish, and it behaves as expected.

    However, my next step was to put this on a raspberry pi. I have a raspberry pi 3, and loaded ubuntu-mate (version 16.04.1 LTS) onto it. That went well, but when I try to add the slcan module in step 2, I get a 'fatal' error saying that the module cannot be found. Any idea why this is occurring? Why would I need to compile the kernel with the slcan drivers if I am using Ubuntu? Thanks!

    1. Hi, sorry but I'm not very much into linux. As the intro of this tutorial says, I tested all this using Ubuntu 14.04. I can't tell what has been changed since then. Sorry :-(

  9. Thank you very much for this guide. Very helpful! :) *thumbsup*

  10. Hello!
    Thank you very much for the guide. Everything works fine for me till step 6. I can see incoming CAN Data.

    However the automation process described from step 7 doesn't work for me. I am using Tegra-Ubuntu. After rebooting and typing: "candump slcan0", I get "SIOCGIFINDEX: No such device"

    Can u help?

  11. Hello!

    another question: what are "can" and "can_raw" here for? why are we using slcan?

    thank you

  12. The udev will kill all daemon processes after the event handling has finished.
    So This proach maybe not usable any more.—— Tested In Ubuntu 16.04 & 18.04

    1. May work again if you add -F flag to the command .

      -F (stay in foreground; no daemonize)

  13. Hi,
    I have some issues using udev rule
    Udev rule is triggered, script works when run manually but device is remove automatically after few seconds
    Log shows some stuff but I do not get what to do
    Can someone please help?

    Udev rule for device connection:
    #Lawicell CAN USB auto detect
    ACTION=="add", ATTRS{serial}=="LW2ZV78N", RUN+="/etc/udev/scripts/start_slcand"

    Logs I get:
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.356397] usb 6-1: new full-speed USB device number 7 using uhci_hcd
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.571681] usb 6-1: New USB device found, idVendor=0403, idProduct=6001
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.571690] usb 6-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.571695] usb 6-1: Product: CANUSB
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.571700] usb 6-1: Manufacturer: LAWICEL
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.571704] usb 6-1: SerialNumber: LW2ZV78N
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.577727] ftdi_sio 6-1:1.0: FTDI USB Serial Device converter detected
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.577801] usb 6-1: Detected FT232RL
    Sep 11 19:52:02 elliot-Satellite-P300 kernel: [ 1168.579780] usb 6-1: FTDI USB Serial Device converter now attached to ttyUSB0
    Sep 11 19:52:02 elliot-Satellite-P300 mtp-probe: checking bus 6, device 7: "/sys/devices/pci0000:00/0000:00:1d.0/usb6/6-1"
    Sep 11 19:52:03 elliot-Satellite-P300 mtp-probe: bus: 6, device: 7 was not an MTP device
    Sep 11 19:52:04 elliot-Satellite-P300 slcand[4976]: starting on TTY device /dev/ttyUSB0
    Sep 11 19:52:04 elliot-Satellite-P300 slcand[4977]: attached TTY /dev/ttyUSB0 to netdevice slcan0
    Sep 11 19:52:04 elliot-Satellite-P300 NetworkManager[751]: nm_device_get_device_type: assertion 'NM_IS_DEVICE (self)' failed
    Sep 11 19:52:04 elliot-Satellite-P300 NetworkManager[751]: [1568224324.0234] manager: (slcan0): new Generic device (/org/freedesktop/NetworkManager/Devices/10)
    Sep 11 19:52:04 elliot-Satellite-P300 NetworkManager[751]: [1568224324.0520] devices added (path: /sys/devices/virtual/net/slcan0, iface: slcan0)
    Sep 11 19:52:04 elliot-Satellite-P300 NetworkManager[751]: [1568224324.0520] device added (path: /sys/devices/virtual/net/slcan0, iface: slcan0): no ifupdown configuration found.
    Sep 11 19:52:08 elliot-Satellite-P300 slcand[5009]: starting on TTY device /dev/ttyUSB0
    Sep 11 19:52:08 elliot-Satellite-P300 slcand[5010]: attached TTY /dev/ttyUSB0 to netdevice slcan0
    Sep 11 19:52:11 elliot-Satellite-P300 slcand[5019]: starting on TTY device /dev/ttyUSB0
    Sep 11 19:52:11 elliot-Satellite-P300 slcand[5020]: attached TTY /dev/ttyUSB0 to netdevice slcan0
    Sep 11 19:52:14 elliot-Satellite-P300 slcand[5028]: starting on TTY device /dev/ttyUSB0
    Sep 11 19:52:14 elliot-Satellite-P300 slcand[5029]: attached TTY /dev/ttyUSB0 to netdevice slcan0
    Sep 11 19:52:16 elliot-Satellite-P300 NetworkManager[751]: [1568224336.1011] platform-linux: do-change-link[11]: failure changing link: failure 19 (No such device)
    Sep 11 19:52:16 elliot-Satellite-P300 NetworkManager[751]: [1568224336.1011] device (slcan0): failed to disable userspace IPv6LL address handling
    Sep 11 19:52:16 elliot-Satellite-P300 NetworkManager[751]: [1568224336.1108] devices removed (path: /sys/devices/virtual/net/slcan0, iface: slcan0)

  14. This content is simply exciting and creative. I have been deciding on a institutional move and this has helped me with one aspect. curency converter

  15. Online Baccarat in Florida - Play Online in FL
    Learn how to play online baccarat in Florida. Learn to play online baccarat in หารายได้เสริม Florida. Learn choegocasino to play 바카라 online baccarat in Florida. Learn to play online baccarat in Florida. Learn to play online baccarat in Florida.