使用 PeakCAN 插件
PeakCAN 插件封装了用于与PEAK-SystemCAN 适配器配合使用的底层 API。
该插件在 Windows 系统上需要 PCAN 设备驱动程序和 PCAN-Basic 库 4.4.0 及以上版本(pcanbasic.dll),在 Linux 系统上则需要 4.0.0 及以上版本(libpcanbasic.so)。 在 macOS 系统上,该插件需要UV Software 提供的 PCBUSB 库,版本需为 0.9 或更高。
注意: Qt 5.14 至 6.4版本 仅与 PCBUSB 库 0.8.1 版本兼容,而 Qt 6.5 及更高版本则需要 PCBUSB 0.9 及以上版本。
注意: QCanBusDeviceInfo::alias() 仅在 Windows 系统上且需配合 PCAN-Basic 4.4.0 或更高版本使用时才受支持。
创建 CAN 总线设备
首先需要确认QCanBus 是否提供了所需的插件:
if (QCanBus::instance()->plugins().contains(QStringLiteral("peakcan"))) {
// plugin available
}其中peakcan是插件名称。
接下来,可以建立与特定接口的连接:
QString errorString;
QCanBusDevice*device =QCanBus::instance()->createDevice(
QStringLiteral("peakcan"), QStringLiteral("usb0"), &errorString);
if(!device) {
// 错误处理代码放在这里
qDebug << errorString;
}else{
device->connectDevice();
}其中usb0是当前活动的 CAN 接口名称。PeakCAN 插件支持 16 个 USB 接口(从usb0到usb15)和 16 个 PCI 接口(从pci0到pci15)。availableDevices() 方法返回当前可用设备的列表。
注意: 目前该插件仅 支持 USB 和 PCI 适配器。
注意:在 macOS 系统上,该插件目前仅支持 USB 适配器。
该设备现已打开,可用于读写 CAN 帧:
QCanBusFrame frame;
frame.setFrameId(8);
QByteArray payload("A36E");
frame.setPayload(payload);
device->writeFrame(frame);可通过readFrame()方法进行读取。当至少有一个新帧可供读取时,会触发framesReceived()信号:
QCanBusFrame frame = device->readFrame();PeakCAN 支持以下配置,这些配置可通过setConfigurationParameter() 进行控制:
| 配置参数键 | 描述 |
|---|---|
| QCanBusDevice::CanFdKey | 此配置选项用于确定是否允许发送或接收 CAN FD 帧。默认情况下,此选项处于禁用状态。启用此选项需要兼容 CAN FD 的硬件。 |
| QCanBusDevice::BitRateKey | 确定 CAN 总线连接的比特率。 支持以下波特率:5000、10000、20000、33000、47000、50000、83000、95000、100000、 125000、250000、500000、800000、1000000。 如果启用了“QCanBusDevice::CanFdKey ”选项,数据速率将限制为 125000、250000、500000 或 1000000。请注意,此配置参数仅可在“QCanBusDevice ”未连接时进行调整。 |
| QCanBusDevice::DataBitRateKey | 确定 CAN FD 总线连接的数据波特率。必须启用“QCanBusDevice::CanFdKey ”选项才能设置此选项。可选的数据波特率为 2000000、4000000、8000000 或 10000000。 请注意,此配置参数仅在QCanBusDevice 未连接时才能调整。 |
PeakCAN 支持以下附加功能:
- QCanBusDevice::resetController()
- QCanBusDevice::busStatus()
- QCanBusDeviceInfo::alias()(仅限 Windows 及 PCAN-Basic 4.4.0 及以上版本): 返回 PCAN_DEVICE_ID,该值可通过 PCAN-View 设置为 0..255 或 0..4294967295 范围内的用户自定义值(具体取决于设备)。 默认值分别为 255 和 4294967295。
© 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.