使用 TinyCAN 插件

TinyCAN 插件封装了与MHS ElektronikCAN 适配器配合使用的底层 API。

注意: TinyCAN 适配器使用虚拟串行端口。要在 Linux 中与 TinyCAN 适配器通信,用户必须拥有适当的访问权限。通常,"dialout "组中的所有用户都有这些权限。

创建 CAN 总线设备

首先需要检查QCanBus 是否提供了所需的插件:

if (QCanBus::instance()->plugins().contains(QStringLiteral("tinycan"))) {
    // plugin available
}

其中tinycan是插件名称。

然后,可以建立与特定接口的连接:

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

其中,can0.0是活动 CAN 接口名称。TinyCAN 目前只提供can0.0 接口,它映射到 TinyCAN 插件中的 INDEX_CAN_KANAL_A。

注: 函数 availableDevices() 目前总是返回一个设备 "can0.0",因为目前还没有办法检测可用的 CAN 适配器。

注: 该插件目前仅支持 USB 适配器。

该设备现在可用于写入和读取 CAN 帧:

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

读取可使用readFrame() 方法完成。当至少有一个新帧可供读取时,就会发出framesReceived() 信号:

QCanBusFrame frame = device->readFrame();

TinyCAN 支持以下配置,可通过setConfigurationParameter() 进行控制:

配置参数键说明
QCanBusDevice::BitRateKey确定 CAN 总线连接的比特率。支持以下比特率:10000, 20000, 50000, 100000, 125000, 250000, 500000, 800000, 1000000.

TinyCAN 支持以下附加功能:

© 2025 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.