使用 PeakCAN 插件
PeakCAN 插件封装了与PEAK-SystemCAN 适配器配合使用的底层 API。
该插件需要 PCAN 设备驱动程序和 PCAN-Basic 库,Windows 版本为 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是插件名称。
接下来,可以建立与特定接口的连接:
QStringerrorString;QCanBusDevice*设备 =QCanBus::instance()->createDevice( QStringLiteral("peakcan")、 QStringLiteral("usb0"), &errorString);if(!device) {// 错误处理到此为止 qDebug << errorString; }else{ device->connectDevice(); }
其中usb0是活动 CAN 接口名称。PeakCAN 插件支持从usb0到usb15的 16 个 USB 接口和从pci0到pci15 的 16 个 PCI 接口。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。
© 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.