本页内容

QCanBus Class

QCanBus 类负责总线插件的注册和创建。更多内容...

头文件: #include <QCanBus>
CMake: find_package(Qt6 REQUIRED COMPONENTS SerialBus)
target_link_libraries(mytarget PRIVATE Qt6::SerialBus)
qmake: QT += serialbus
继承自: QObject

公共函数

(since 6.8) QList<QCanBusDeviceInfo> availableDevices(QString *errorMessage = nullptr) const
QList<QCanBusDeviceInfo> availableDevices(const QString &plugin, QString *errorMessage = nullptr) const
QCanBusDevice *createDevice(const QString &plugin, const QString &interfaceName, QString *errorMessage = nullptr) const
QStringList plugins() const

静态公共成员

QCanBus *instance()

详细描述

QCanBus 在运行时加载 Qt CAN 总线插件。串行总线插件的所有权将转移给加载器。

成员函数文档

[since 6.8] QList<QCanBusDeviceInfo> QCanBus::availableDevices(QString *errorMessage = nullptr) const

返回所有插件的可用设备,即所有可用的 CAN 接口。

如果操作失败,可选参数errorMessage 将返回一条文本形式的错误描述。

注意:某些 插件可能不支持此函数,或仅部分支持。

此函数于 Qt 6.8 中引入。

另请参阅 createDevice()。

QList<QCanBusDeviceInfo> QCanBus::availableDevices(const QString &plugin, QString *errorMessage = nullptr) const

返回plugin 的可用接口。如果操作失败,可选参数errorMessage 将返回一个文本形式的错误描述。

注意:某些 插件可能不支持此函数,或仅部分支持。

例如,以下调用将返回所有可用 SocketCAN 接口的列表(这些接口可用于createDevice()):

QString errorString;
constQList<QCanBusDeviceInfo>devices=QCanBus::instance()->availableDevices(
    QStringLiteral("socketcan"), &errorString);
if(!errorString.isEmpty())
    qDebug() << errorString;

另请参阅 createDevice()。

QCanBusDevice *QCanBus::createDevice(const QString &plugin, const QString &interfaceName, QString *errorMessage = nullptr) const

创建一个CAN总线设备。plugin 是plugins()方法返回的插件名称。interfaceName 是CAN总线接口名称。如果操作失败,可选参数errorMessage 将返回一条文本形式的错误描述。

返回的插件的所有权将转移给调用方。如果找不到合适的设备,则返回nullptr 。

例如,以下调用将连接到 SocketCAN 接口 vcan0:

QString errorString;
QCanBusDevice*device =QCanBus::instance()->createDevice(
    QStringLiteral("socketcan"), QStringLiteral("vcan0"), &errorString);
if(!device)
    qDebug() << errorString;
else
    device->connectDevice();

注意: interfaceName 取决于插件。更多信息请参阅相应的插件文档:CAN 总线插件。要获取可用接口的列表,可以使用availableDevices()。

另请参阅 availableDevices()。

[static] QCanBus *QCanBus::instance()

返回指向QCanBus 类的指针。如有必要,将加载该对象。QCanBus 采用单例设计模式。

QStringList QCanBus::plugins() const

返回所有已加载插件的标识符列表。

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