本页内容

使用 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。

注意: 由于目前尚无法检测可用的 CAN 适配器,因此 availableDevices()函数 当前始终返回一个设备“can0.0”。

注意: 目前该插件仅 支持 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 还支持以下附加功能:

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