使用 VectorCAN 插件

VectorCAN 插件封装了与Vector InformatikCAN 适配器协同工作的底层应用程序接口。

该插件需要 Vector CAN 设备驱动程序和 vxlapi.dll(64 位版本需要 vxlapi64.dll)。

创建 CAN 总线设备

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

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

其中vectorcan是插件名称。

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

QStringerrorString;QCanBusDevice*设备 =QCanBus::instance()->createDevice(    QStringLiteral("vectorcan")QStringLiteral("can0"), &errorString);if(!device) {// 错误处理到此为止    qDebug << errorString;
}else{ device->connectDevice(); }

其中,can0是活动 CAN 通道名称。VectorCAN 插件提供了从can0can63 的 64 个通道(由 Vector API 中的 XL_CONFIG_MAX_CHANNELS 定义)。其中一些通道可以是虚拟通道,因此可以在没有实际 CAN 硬件的情况下使用。要找出虚拟通道,可以使用 "Vector Hardware Config "程序(vcanconf.exe),它包含在 Vector 的驱动程序包中。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 总线的 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 支持以下附加功能:

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