使用 SystecCAN 插件
SystecCAN 插件封装了与SYS TECCAN 适配器配合使用的底层 API。
该插件需要 SYS TEC CAN 设备驱动程序和 usbcan32.dll(64 位版本需要 usbcan64.dll)。
创建 CAN 总线设备
首先需要检查QCanBus 是否提供所需的插件:
if (QCanBus::instance()->plugins().contains(QStringLiteral("systeccan"))) { // plugin available }
其中systeccan是插件名称。
接下来,可以建立与特定接口的连接:
QStringerrorString;QCanBusDevice*设备 =QCanBus::instance()->createDevice( QStringLiteral("systeccan")、 QStringLiteral("can0.0"), &errorString);if(!device) {// 错误处理到此为止 qDebug << errorString; }else{ device->connectDevice(); }
其中,can0.0是活动 CAN 接口名称(接口 0,通道 0)。SystecCAN 插件支持从can0.0到can63.1 的 64 个 USB 接口(即所谓的模块)。每个模块可以有一个或两个通道,可以通过索引 canX.0 或 canX.1 进行访问。availableDevices() 方法返回当前可用设备的列表。
注: 德国思泰还提供 8 或 16 通道 CAN 接口。这些设备内部由一个 USB 集线器和多个双通道模块组成。
该设备现在可用于写入和读取 CAN 帧:
QCanBusFrame frame; frame.setFrameId(8); QByteArray payload("A36E"); frame.setPayload(payload); device->writeFrame(frame);
读取可以使用readFrame() 方法。当至少有一个新帧可供读取时,就会发出framesReceived() 信号:
QCanBusFrame frame = device->readFrame();
SystecCAN 支持以下配置,可通过setConfigurationParameter() 进行控制:
配置参数键 | 说明 |
---|---|
QCanBusDevice::BitRateKey | 确定 CAN 总线连接的比特率。支持以下比特率:10000、20000、50000、100000、125000、250000、500000、800000 和 1000000。请注意,该配置参数只能在QCanBusDevice 未连接时进行调整。 |
QCanBusDevice::ReceiveOwnKey | 在发送 CAN 帧的同一通道上接收 CAN 帧的功能默认为禁用。如果启用该选项,则接收到的帧将以QCanBusFrame::hasLocalEcho() 标记。 |
SystecCAN 支持以下附加功能:
© 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.