本页内容

使用 VectorCAN 插件

VectorCAN 插件封装了用于与Vector InformatikCAN 适配器配合使用的底层 API。

该插件需要 Vector CAN 设备驱动程序以及 vxlapi.dll(64 位构建版本需使用 vxlapi64.dll)。

创建 CAN 总线设备

首先,需要确认QCanBus 是否提供了所需的插件:

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

其中vectorcan是插件名称。

接下来,可以建立与特定接口的连接:

QString errorString;
QCanBusDevice*device =QCanBus::instance()->createDevice(
    QStringLiteral("vectorcan"), QStringLiteral("can0"), &errorString);
if(!device) {
    // 错误处理代码写在这里
    qDebug << errorString;
}else{
    device->connectDevice();
}

其中can0是活动 CAN 通道的名称。VectorCAN 插件提供 64 个通道(由 Vector API 中的 XL_CONFIG_MAX_CHANNELS 定义),范围从can0到can63。 其中部分通道可能是虚拟通道,因此无需实际的 CAN 硬件即可使用。要查找虚拟通道,可使用 Vector 驱动程序包中包含的“Vector Hardware Config”(vcanconf.exe)程序。availableDevices() 方法会返回当前可用设备的列表。

现在,该设备已打开,可用于读写 CAN 帧:

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

可通过调用readFrame() 方法进行读取。当至少有一个新帧可供读取时,将触发framesReceived() 信号:

QCanBusFrame frame = device->readFrame();

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

配置参数键描述
QCanBusDevice::BitRateKey确定 CAN 总线连接的波特率。
QCanBusDevice::ReceiveOwnKey默认情况下,同一设备无法接收其自身发送的 CAN 帧。启用此选项后,发送到 CAN 总线的所有 CAN 帧会立即出现在接收缓冲区中。这可用于检查发送是否成功。如果启用此选项,则接收到的帧将通过QCanBusFrame::hasLocalEcho() 进行标记
QCanBusDevice::CanFdKey启用 CAN 总线连接上的 CAN FD 功能。如果启用此选项,则无法接收自己发送的 CAN 帧,因此将 `QCanBusDevice::ReceiveOwnKey ` 设置为 `true` 不会产生效果。自 Qt 5.15 起。
QCanBusDevice::DataBitRateKey确定 CAN 总线连接的数据比特率。仅当QCanBusDevice::CanFdKey 设置为 true 时才可用。自 Qt 5.15 起。

VectorCAN 支持以下附加功能:

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