CAN Bus

Installing the can-utils

candump can0

cansend can0 -i 0x150 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

e.g.

root@b2qt-apalis-imx6:~# candump
interface = can0, family = 29, type = 3, proto = 1
<0x145> [8] ff 00 00 00 02 00 00 00
<0x145> [8] ff 00 00 00 02 00 00 00
<0x145> [8] ff 00 00 00 02 00 00 00
<0x145> [8] ff 00 00 00 02 00 00 00
<0x145> [8] ff 00 00 00 02 00 00 00
<0x145> [8] ff 00 00 00 02 00 00 00

root@b2qt-apalis-imx6:~# cansend can0 -i 0x212 0x3F 0x03 0x02 0x32 0x64 0x96 0x00
interface = can0, family = 29, type = 3, proto = 1

The QtSerialBus Module

For CAN bus, currently supports the following back ends:

  • SocketCAN, which uses Linux sockets and open source drivers.
  • Peak CAN, which supports PCAN adaptors from PEAK-System Technik GmbH.
  • TinyCAN, which support for Tiny-CAN adapters from MHS Elektronik.
  • VectorCAN, supporting Vector Informatik CAN adapters.

Qt APIs

  • C++ only, no QML
  • To add to qmake projects: QT+= serialbus
  • Module include file: #include <QtSerialBus>

Qt APIs - CAN bus

6 classes:

QcanBus - Handles registration and creation of bus backends

QcanBusDevice::Filter - Defines a filter for CAN bus messages

QcanBusDevice - The interface class for CAN bus

QcanBusFactory - Factory class used as the plugin interface

QcanBusFrame - Container class representing a single CAN frame

QcanBusFrame::TimeStamp - Timestamp information with µsec precision

Code Examples - CAN bus

Basic Steps:

  1. Create a device, specifying plugin and device name.
  2. Connect.
  3. Create data frames.
  4. Send data frames.
  5. Disconnect when done.

QCANBusDevice emits signals: errorOccurred, framesReceived, framesWritten, stateChanged.

To receive frames, connect to signal framesReceived.

// Create device.
QCanBusDevice *device = QCanBus::instance() ->createDevice("socketcan", "vcan0");
if (device != nullptr) {
    qDebug() << "Created device, state is:" << device->state();
} else {
    qFatal("Unable to create CAN device.");
}

// Connect.
if (device->connectDevice()) {
    qDebug() << "Connected, state is:" << device->state();
} else {
    qDebug() << "Connect failed, error is:" << device->errorString();
}

// Create a data frame.
QCanBusFrame frame(QCanBusFrame::DataFrame, "12345");

// Send it.
if (device->writeFrame(frame)) {
    qDebug() << "Wrote frame, state is:" << device->state();
} else {
    qDebug() << "Write failed, error is:" << device->errorString();
}

// Disconnect.
device->disconnectDevice();
qDebug() << "Disconnected, state is:" << device->state();

SocketCAN provides several CAN interface types:

  • virtual interfaces like vcan0
  • native (real hardware) interfaces like can0
  • SLCAN based interfaces like slcan0

Virtual Interfaces

Virtual CAN interfaces will be brought up via iproute2 ip utility:

There is a virtual CAN driver for testing purposes which can be loaded and created in Linux with the commands below.

$ modprobe can
$ modprobe can_raw
$ modprobe vcan
$ sudo ip link add dev vcan0 type vcan
$ sudo ip link set up vcan0

Native Interfaces

Installing a CAN device requires loading the can_dev module and configuring the IP link to specify the can bus bitrate, for example:

$ modprobe can_dev
$ modprobe can
$ modprobe can_raw
$ sudo ip link set can0 type can bitrate 125000
$ sudo ip link set up can0

sudo modprobe can

sudo modprobe can_raw

sudo modprobe vcan

sudo ip link add dev vcan0 type vcan

sudo ip link set up vcan0

Reference

Bringing CAN interface up https://elinux.org/Bringing_CAN_interface_up

SocketCAN https://en.wikipedia.org/wiki/SocketCAN

Automobile Hacking, Part 2: The can-utils or SocketCAN https://www.hackers-arise.com/single-post/2017/08/08/Automobile-Hacking-Part-2-The-can-utils-or-SocketCAN

Can-utils https://elinux.org/Can-utils

CAN communication tutorial, using simulated CAN bus http://sgframework.readthedocs.io/en/latest/cantutorial.html

CAN Bus example http://docs.huihoo.com/qt/5.x/qtserialbus-can-example.html

results for ""

    No results matching ""