- Qt 6.8
- Qt Serial Bus
- Qt CAN Bus
- Qt 6.8.3 Reference Documentation

# Qt CAN Bus

A Controller Area Network (CAN) is a vehicle bus standard designed to allow microcontrollers and devices to communicate with each other in applications without a host computer.

## Overview

It is a message-based protocol, designed originally for multiplex electrical wiring within automobiles, but is also used in many other contexts.

The CAN Bus API provides some common API to access the CAN devices:

- QCanBus provides an API to create a QCanBusDevice from a chosen plugin.
- QCanBusDeviceInfo provides information about available CAN devices.
- QCanBusDevice provides an API for direct access to the CAN device.
- QCanBusFrame defines a CAN frame that can be written and read from QCanBusDevice.

Starting from Qt 6.5, the module provides APIs to decode actual signal values from raw CAN frames and also to encode user data into CAN frames:

- QCanSignalDescription provides rules to process CAN signals.
- QCanMessageDescription provides rules to process CAN messages. A message description usually contains multiple signal descriptions.
- QCanUniqueIdDescription provides rules to process a unique identifier within a CAN frame.
- QCanFrameProcessor uses descriptions provided by QCanMessageDescription, QCanSignalDescription, and QCanUniqueIdDescription classes to encode or decode CAN frames.
- QCanDbcFileParser provides an API to extract message descriptions from DBC files.

Note: All APIs for encoding and decoding CAN frames are experimental and subject to change.

## CAN Bus Plugins

Multiple vendors provide CAN devices with varying APIs for access. The QtSerialBus module supports the following sets of CAN bus plugins:

Vendor | Plugin (key) | Brief description
--- | --- | ---
CAN over Linux sockets | SocketCAN (`socketcan`) | CAN bus plugin using Linux sockets and open source drivers.
CAN via SAE J2534 Pass-Thru | PassThruCAN (`passthrucan`) | CAN bus plugin using the SAE J2534 Pass-Thru interface.
SYS TEC electronic | SystecCAN (`systeccan`) | CAN bus backend using the SYS TEC CAN adapters.
PEAK-System | PeakCAN (`peakcan`) | CAN bus plugin using the PEAK CAN adapters.
MHS Elektronik | TinyCAN (`tinycan`) | CAN bus plugin using the MHS CAN adapters.
Vector Informatik | VectorCAN (`vectorcan`) | CAN bus plugin using the Vector CAN adapters.
Virtual CAN interface | VirtualCAN (`virtualcan`) | CAN bus plugin using a virtual TCP/IP connection.

## Implementing a Custom CAN Plugin

If the plugins provided by Qt are not suitable for the required target platform, a custom CAN bus plugin can be implemented. The implementation follows the standard way of implementing Qt plug-ins. The custom plugin must be deployed to `$QTDIR/plugins/canbus`.

Each plugin must define a key, which is used to load the plugin. This is done via a small json file. For example, the socketcan plugin uses the following `plugin.json`:

     {
         "Key": "socketcan"
     }

This key must be passed to QCanBus::createDevice() together with the interface name of the CAN bus adapter. QCanBus loads and instantiates the plugin using the QCanBusFactoryV2 interface which each plugin must implement as central entry point. The interface acts as a factory and its sole purpose is to return a QCanBusDevice instance. The above mentioned interface name is passed on via the factory's QCanBusFactory::createDevice() method. The following is the factory implementation of the socketcan plugin:

     class SocketCanBusPlugin : public QObject, public QCanBusFactory
     {
         Q_OBJECT
         Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QCanBusFactory" FILE "plugin.json")
         Q_INTERFACES(QCanBusFactory)

     public:
         QList<QCanBusDeviceInfo> availableDevices(QString *errorMessage) const override
         {
             Q_UNUSED(errorMessage);
             return SocketCanBackend::interfaces();
         }

         QCanBusDevice *createDevice(const QString &interfaceName, QString *errorMessage) const override
         {
             Q_UNUSED(errorMessage);
             auto device = new SocketCanBackend(interfaceName);
             return device;
         }
     };

The next step is to provide an implementation of QCanBusDevice. At the very least, the following pure virtual functions must be implemented:

- QCanBusDevice::open()
- QCanBusDevice::close()
- QCanBusDevice::writeFrame()
- QCanBusDevice::interpretErrorFrame()

The open() and close() methods are used in conjunction with QCanBusDevice::connectDevice() and QCanBusDevice::disconnectDevice() respectively. Check the function documentation for implementation details.

QCanBusDevice::writeFrame() is responsible for sanity checks such as the validity of the QCanBusFrame and that the device is still connected. Provided that the checks passed, it writes the frame to the CAN bus. Upon success it emits the QCanBusDevice::framesWritten() signal; otherwise QCanBusDevice::setError() is called with an appropriate error message. This function may also be used to implement an asynchronous write operation. It is the plugin implementors responsibility to emit the appropriate signals at the appropriate time.

Last but not least, QCanBusDevice::interpretErrorFrame provides a convenient way to translate the content of an CAN bus error frame to a human-readable error string.