Using SocketCAN Backend

The SocketCAN backend encapsulates the Linux sockets API for accessing the CAN devices. This API is a set of open source CAN drivers and a networking stack contributed by Volkswagen Research to the Linux kernel.

Creating CAN Bus Devices

At first it is necessary to check that QCanBus provides the desired backend:

foreach (const QByteArray &backend, QCanBus::instance()->plugins()) {
    if (backend == "socketcan") {
        // were found
        break;
    }
}

Where socketcan is the backend name.

Next, a connection to a specific interface can be established:

QCanBusDevice *device = QCanBus::instance()->createDevice("socketcan", QStringLiteral("can0"));
device->connectDevice();

Where can0 is the active CAN interface name. CAN interfaces act like regular network interfaces on Linux systems and can be discovered using ifconfig.

The device is now open for writing and reading CAN frames:

QCanBusFrame frame;
frame.setFrameId(8);
QByteArray payload("A36E");
frame.setPayload(payload);
device->writeFrame(frame);

The reading can be done using the readFrame() method. The framesReceived() signal is emitted when a new frame is available for reading:

QCanBusFrame frame = device->readFrame();

SocketCAN supports the following configurations that can be controlled through setConfigurationParameter():

Configuration parameter keyDescription
QCanBusDevice::LoopbackKeyTo meet the multiple-user needs, the local loopback is enabled by default.
QCanBusDevice::ReceiveOwnKeyThe reception of the CAN frames on the same socket that was sending the CAN frame is disabled by default.
QCanBusDevice::ErrorFilterKeyA CAN interface driver can generate so called Error Message Frames that can optionally be passed to the user application in the same way as other CAN frames. The possible errors are divided into different error classes that may be filtered using the appropriate error mask. The values for the error mask are defined in linux/can/error.h.
QCanBusDevice::RawFilterKeyThis configuration can contain multiple filters of type QCanBusDevice::Filter. By default, the connection is configured to accept any CAN bus message.
QCanBusDevice::BitRateKeyThis configuration is not supported by the socketcan backend. However it is possible to set the rate when configuring the CAN network interface using the ip link command.
QCanBusDevice::CanFdKeyThis configuration option determines whether CANFD frames may be sent or received. By default, this option is disabled. It controls controls the CAN_RAW_FD_FRAMES option of the CAN socket.

For example:

QList<QCanBusDevice::Filter> list;
QCanBusDevice::Filter f;

// only accept odd numbered frame id of type remote request
// frame can utilize extended or base format
f.frameId = 0x1;
f.frameIdMask = 0x1;
f.format = QCanBusDevice::Filter::MatchBaseAndExtendedFormat;
f.type = QCanBusFrame::RemoteRequestFrame;
list.append(f);

device->setConfigurationParameter(QCanBusDevice::RawFilterKey, QVariant::fromValue(list));
device->setConfigurationParameter(QCanBusDevice::ErrorFilterKey,
                                  QVariant::fromValue(QCanBusFrame::FrameErrors(QCanBusFrame::AnyError)));

Extended frame format and flexible data-rate are supported in SocketCAN.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.