Linux can usb interface

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Device to connect to a CAN bus from a Linux / Windows / macOS device

License

bmc-labs/usb-can

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

usb-can , a simple and cost effective CAN link

bmc::labs usb-can

Rendering of usb-can , our version of the USBtin by Thomas Fischl

Be it for automation, development or debugging, one often needs a way to inspect a CAN bus. This device allows to connect to a CAN bus via a standard USB type C cable. It is based on the USBtin by Thomas Fischl — there are many great resources behind this link, and we encourage you to buy Thomas’ version from him!

All examplatory output comes from a 2013 MacBook Pro running Debian.

Start by adding yourself to the dialout group if you aren’t already in it:

» sudo usermod -aG dialout $USER

You’ll need can-utils for Linux. If you’re on something Debian-based, you’re best off installing them like so:

» sudo apt install can-utils

If you’re on a different distro, you may find the can-utils in your respective package management; if not go get them from GitHub. Good, now you’re all set.

CAN support via socketcan is upstream in Linux, but it is not necessarily loaded by default. Execute load like so:

» sudo modprobe can » sudo modprobe can-raw » sudo modprobe slcan

You can use the lsmod utility to verify everything has worked:

» lsmod | grep can slcan 16384 0 can_raw 20480 0 can 24576 1 can_raw

You output may look slightly different, but you should see the modules loaded.

Читайте также:  Yandex disk linux setup

Check which serial device is the CAN device

There are a number of ways to do this. First off, you may want to check that the usb-can is actually recognized via lsusb (which you may have to install):

» lsusb . Bus 002 Device 015: ID 04d8:000a Microchip Technology, Inc. CDC RS-232 Emulation Demo .

If the device shows up here, you can look which serial port it is bound to:

» sudo tail /var/log/kern.log Apr 18 14:53:17 rzrgrl kernel: [64635.125815] usb 2-2: new full-speed USB device number 15 using xhci_hcd Apr 18 14:53:17 rzrgrl kernel: [64635.276572] usb 2-2: New USB device found, idVendor=04d8, idProduct=000a, bcdDevice= 1.00 Apr 18 14:53:17 rzrgrl kernel: [64635.276580] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=0 Apr 18 14:53:17 rzrgrl kernel: [64635.276583] usb 2-2: Product: USBtin Apr 18 14:53:17 rzrgrl kernel: [64635.276585] usb 2-2: Manufacturer: Microchip Technology, Inc. Apr 18 14:53:17 rzrgrl kernel: [64635.277670] cdc_acm 2-2:1.0: ttyACM0: USB ACM device

In this case, it is ttyACM0 , which should be similar for you.

Attach the device to a socket-capable interface

The following commands come from can-utils , so if you haven’t installed those so far, do that now.

Next, you should select the bus speed of the CAN you’re trying to connect to. The available speeds are:

Baudrate (kBaud) Number encoding Command paramete
1.000 8 -s8
800 7 -s7
500 6 -s6
250 5 -s5
125 4 -s4
100 3 -s3
50 2 -s2
20 1 -s1
10 0 -s0

Setting up a 500 kBaud CAN interface at serial port ttyACM0 is done like so:

» sudo slcan_attach -f -s6 -o /dev/ttyACM0 attached tty /dev/ttyACM0 to netdevice slcan0 » sudo slcand ttyACM0 slcan0 » sudo ip link set slcan0 up

Congratulations, you’ve done it!

Now you can send and receive CAN messages on the socket interface slcan0 . For example, to dump all incoming messages to the terminal:

The candump utility also allows you to filter for CAN IDs, set up the byte order, and a number of other useful things it will tell you about if you as it nicely ( candump -h ).

Sending a frame to CAN is similarly easy:

» cansend slcan0 7ff#deadbeef

The cansend utility is also very vocal about its capabilities if asked.

Further useful components of can-utils :

    the canplayer utility, which lets you play CAN messages from a log file that looks like this:

(0.100) slcan0 5D1#0000 (0.200) slcan0 271#0100 (0.300) slcan0 289#72027000 (0.400) slcan0 401#081100000000 

Both utilities will also happily tell you about themselves with -h .

Last but not least, here is a snippet for sending the current time in the format to the CAN on ID 0x7ff , i.e. the last and therefore least priority ID, every 5 seconds:

while true do cansend slcan0 7ff#$(printf '%02x%02x' $(date +"%I") $(date +"%M")) sleep 5 done

Please find hints for Windows on Thomas Fischl’s website.

For macOS, there is code floating around the web for socketcan and for can-utils , and once you have those working the above guide for Linux should mostly apply. No idea if it works, though — we don’t do macOS right now.

Читайте также:  Install archive on linux

The contents of this repository which are our original work are released into the public domain unter The Unlicense (see LICENSE). We build heavily on the work of others here and recommend you pay them tribute first if you like the material you’ve found here.

  • 1x usb-can_housing-lower (3d print)
  • 1x usb-can_housing-lower-clean or -icon version (3d print)
  • 4x Socket screw M3x8mm
  • 4x Thread insert M3x4.78mm
  • 1x usb-can PCB board

About

Device to connect to a CAN bus from a Linux / Windows / macOS device

Источник

USBtin and Linux-CAN

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

$ 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

en

Get and compile the 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

Dump can messages

Send sequences of CAN frames

$ cat test.log (0.1) slcan0 5D1#0000 (0.2) slcan0 271#0100 (0.3) slcan0 289#72027000 (0.4) slcan0 401#081100000000
$ ./canplayer -l 20 -I test.log -v -g500

Use Wireshark to capture CAN frames

en

An alternative to the commandline dump tool is 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

en

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: RPi CANBus

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

Источник

Overview

The CANable 2.0 is a small low-cost open source USB to CAN adapter. The CANable enumerates as a virtual serial port on your computer and acts as a serial-line to CAN bus interface. With the alternative candleLight firmware, the CANable enumerates as a native CAN interface on Linux. CANable 2.0 supports both standard CAN and CAN-FD.

CANable adapters are compatible with ARM-based embedded platforms such as the Raspberry Pi, Raspberry Pi Zero, ODROID, BeagleBone, etc. and are well suited for integration into OEM products.

Читайте также:  Эмулятор консоли linux online

CANable 2.0 USB to CAN Adapter

CANable 2.0 Features

  • Supports CAN2.0A and B, baud rates up to 1M
  • Initial slcan support for CAN-FD (beta, 2M/5Mbaud)
  • Compatible with socketcan through slcand
  • USB-C Connector and breakaway mounting holes
  • 4-pin screw terminal: CANH, CANL, 5v (output), GND
  • Button for entering bootloader
  • Switch to enable/disable onboard termination
  • Simple cross-platform Python library
  • Manufactured in the USA
  • Available for purchase at Openlight Labs

For more information on getting up and running with a CANable device, refer to the getting started page.

Questions or comments? Email us at support@canable.io.

Software Support

The CANable is supported by several software applications:

  • Cangaroo (Windows, Linux): Send/receive tandard and FD frames, decode messages from DBC files
  • SocketCAN (Linux) Native Linux support using slcand
  • python-can (Windows, Linux, Mac) Easily interface with the CAN bus using your CANable and Python scripts

Archived Products

CANable USB to CAN Adapter

CANable (Original)

  • Supports CAN2.0A and B, baud rates up to 1M
  • Compatible with socketcan through slcand
  • Native socketcan support with candleLight firmware
  • 4-pin screw terminal: CANH, CANL, 5v (output), GND
  • Jumper to enter bootloader for firmware updates
  • Jumper to enable/disable termination
  • PTC-fused 5v output
  • Simple cross-platform Python library
  • Manufactured in the USA
  • Purchase now on the Openlight Labs store

CANable USB to CAN Adapter

CANable Pro

  • All features of the standard CANable
  • 2.5kV galvanic isolation between USB and CAN bus
  • Additional ESD protection/clamping on CAN bus
  • Additional ESD protection/clamping on USB
  • Breakaway mounting holes
  • Button to enter bootloader for firmware updates
  • Purchase now on the Openlight Labs store

Stock slcan firmware

CANables ship with slcan firmware. This firmware enumerates as a standard serial device on Linux, Mac, and Windows for easy interfacing. The CANable can be easily re-flashed with the candlelight firmware which enumerates as a native CAN device in Linux and a generic USB device in Windows.

On Linux the CANable works natively with slcand, so you can use all of the standard can-utils command-line utilities and even Wireshark to interact with the bus.

On Windows and Linux, the CANable works with Cangaroo which allows transmission/reception of standard and FD frames as well as decoding of messages from loaded DBC files.

candlelight Firmware

If you update to the candlelight firmware, the CANable shows up as a native CAN device with socketcan—no slcand required! Performance is higher than the serial-line firmware as slcand is bypassed entirely. With Linux and socketcan you can use all of the standard can-utils command-line utilities and even Wireshark to interact with the bus. Note: candlelight firmware for CANable 2.0 currently does not support FD frames

Python Support

For even more flexibility, the python-can library allows you to directly talk to the CAN bus from Python. The library is cross-platform and can connect directly to a CANable’s virtual serial port interface or native socketcan interface. With only a couple lines of code you can decode traffic on the bus, send messages, and more.

Check out the getting started page for more information.

Источник

Оцените статью
Adblock
detector