QCanBus 类

QCanBus 类处理总线插件的注册和创建。 更多...

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

公共函数

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 Bus 插件。将串行总线插件的所有权转移给加载程序。

成员函数文档编制

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

返回可用接口,为 plugin . In case of failure, the optional parameter errorMessage returns a textual error description.

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

For example, the following call returns a list of all available SocketCAN interfaces (which can be used for createDevice ()):

QString errorString;
const QList<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

Creates a CAN bus device. plugin is the name of the plugin as returned by the plugins () 方法。 interfaceName is the CAN bus interface name. In case of failure, the optional parameter errorMessage returns a textual error description.

Ownership of the returned plugin is transferred to the caller. Returns nullptr if no suitable device can be found.

For example, the following call would connect to the SocketCAN interface vcan0:

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

注意: The interfaceName is plugin-dependent. See the corresponding plugin documentation for more information: CAN 总线插件 . To get a list of available interfaces, availableDevices () 可以使用。

另请参阅 availableDevices ().

[static] QCanBus *QCanBus:: instance ()

返回指针指向 QCanBus 类。对象会被加载 (若有必要)。 QCanBus 使用 Singleton (单例) 设计模式。

QStringList QCanBus:: plugins () const

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