QCanBusDevice Class
QCanBusDevice 类是 CAN 总线的接口类。更多
头文件: | #include <QCanBusDevice> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS SerialBus) target_link_libraries(mytarget PRIVATE Qt6::SerialBus) |
qmake: | QT += serialbus |
继承: | QObject |
公共类型
struct | Filter |
enum | CanBusDeviceState { UnconnectedState, ConnectingState, ConnectedState, ClosingState } |
enum | CanBusError { NoError, ReadError, WriteError, ConnectionError, ConfigurationError, …, TimeoutError } |
enum class | CanBusStatus { Unknown, Good, Warning, Error, BusOff } |
enum | ConfigurationKey { RawFilterKey, ErrorFilterKey, LoopbackKey, ReceiveOwnKey, BitRateKey, …, UserKey } |
enum | Direction { Input, Output, AllDirections } |
flags | Directions |
公共函数
QCanBusDevice(QObject *parent = nullptr) | |
virtual QCanBusDevice::CanBusStatus | busStatus() |
void | clear(QCanBusDevice::Directions direction = Direction::AllDirections) |
QList<QCanBusDevice::ConfigurationKey> | configurationKeys() const |
QVariant | configurationParameter(QCanBusDevice::ConfigurationKey key) const |
bool | connectDevice() |
(since 6.2) virtual QCanBusDeviceInfo | deviceInfo() const |
void | disconnectDevice() |
QCanBusDevice::CanBusError | error() const |
QString | errorString() const |
qint64 | framesAvailable() const |
qint64 | framesToWrite() const |
virtual bool | hasBusStatus() const |
virtual QString | interpretErrorFrame(const QCanBusFrame &frame) = 0 |
QList<QCanBusFrame> | readAllFrames() |
QCanBusFrame | readFrame() |
virtual void | resetController() |
virtual void | setConfigurationParameter(QCanBusDevice::ConfigurationKey key, const QVariant &value) |
QCanBusDevice::CanBusDeviceState | state() const |
virtual bool | waitForFramesReceived(int msecs) |
virtual bool | waitForFramesWritten(int msecs) |
virtual bool | writeFrame(const QCanBusFrame &frame) = 0 |
信号
void | errorOccurred(QCanBusDevice::CanBusError) |
void | framesReceived() |
void | framesWritten(qint64 framesCount) |
void | stateChanged(QCanBusDevice::CanBusDeviceState state) |
保护函数
void | clearError() |
virtual void | close() = 0 |
QCanBusFrame | dequeueOutgoingFrame() |
void | enqueueOutgoingFrame(const QCanBusFrame &newFrame) |
void | enqueueReceivedFrames(const QList<QCanBusFrame> &newFrames) |
bool | hasOutgoingFrames() const |
virtual bool | open() = 0 |
void | setError(const QString &errorText, QCanBusDevice::CanBusError errorId) |
void | setState(QCanBusDevice::CanBusDeviceState newState) |
成员类型文档
enum QCanBusDevice::CanBusDeviceState
该枚举描述了所有可能的设备状态。
常量 | 值 | 描述 |
---|---|---|
QCanBusDevice::UnconnectedState | 0 | 设备断开连接。 |
QCanBusDevice::ConnectingState | 1 | 设备正在连接。 |
QCanBusDevice::ConnectedState | 2 | 设备已连接 CAN 总线。 |
QCanBusDevice::ClosingState | 3 | 设备正在关闭。 |
enum QCanBusDevice::CanBusError
该枚举描述了所有可能的错误条件。
常量 | 值 | 说明 |
---|---|---|
QCanBusDevice::NoError | 0 | 未发生错误。 |
QCanBusDevice::ReadError | 1 | 读取操作过程中发生错误。 |
QCanBusDevice::WriteError | 2 | 写入操作时发生错误。 |
QCanBusDevice::ConnectionError | 3 | 尝试打开插件时发生错误。 |
QCanBusDevice::ConfigurationError | 4 | 尝试设置配置参数时发生错误。 |
QCanBusDevice::UnknownError | 5 | 发生未知错误。 |
QCanBusDevice::OperationError | 6 | 当设备处于不允许操作的状态时尝试了操作。此枚举在 Qt 5.14 中引入。 |
QCanBusDevice::TimeoutError | 7 | 在等待写入或接收帧时发生超时。此枚举在 Qt 5.14 中引入。 |
enum class QCanBusDevice::CanBusStatus
该枚举描述了可能的 CAN 总线状态值。
常量 | 值 | 说明 |
---|---|---|
QCanBusDevice::CanBusStatus::Unknown | 0 | CAN 总线状态未知(例如 CAN 插件不支持)。 |
QCanBusDevice::CanBusStatus::Good | 1 | CAN 控制器完全正常 |
QCanBusDevice::CanBusStatus::Warning | 2 | CAN 控制器处于警告状态 |
QCanBusDevice::CanBusStatus::Error | 3 | CAN 控制器处于错误状态(不再发送 CAN 帧) |
QCanBusDevice::CanBusStatus::BusOff | 4 | CAN 控制器处于总线关闭状态(与 CAN 总线断开连接) |
enum QCanBusDevice::ConfigurationKey
该枚举描述了 CAN 总线连接的可能配置选项。
常数 | 值 | 说明 |
---|---|---|
QCanBusDevice::RawFilterKey | 0 | 该配置决定当前设备接受的 CAN 总线帧类型。预期值为QList<QCanBusDevice::Filter> 。传递空列表将清除所有先前设置的过滤器,包括默认过滤器。有关详细信息,请参阅QCanBusDevice::Filter 。 |
QCanBusDevice::ErrorFilterKey | 1 | 该键定义了应通过当前连接转发的错误类型。相关值的类型应为QCanBusFrame::FrameErrors 。 |
QCanBusDevice::LoopbackKey | 2 | 该键定义 CAN 总线设备是否应在环回模式下运行。环回是指,每当 CAN 总线上传输 CAN 帧时,都会向连接到该 CAN 设备的所有应用程序发送该帧的本地回声。该键的预期值为bool 。 |
QCanBusDevice::ReceiveOwnKey | 3 | 该键定义 CAN 设备是否接收自己的发送帧。这可用于检查传输是否成功。该键的预期值为bool 。 |
QCanBusDevice::BitRateKey | 4 | 该键定义 CAN 比特率,单位为比特/秒。使用 CAN FD 时,如果设置了QCanBusFrame::hasBitrateSwitch() ,有效载荷可以更高的数据比特率传输。在这种情况下,QCanBusDevice::BitRateKey 仅用于 CAN ID 仲裁阶段。另请参见QCanBusDevice::DataBitRateKey |
QCanBusDevice::CanFdKey | 5 | 该键定义是否启用 CAN FD 帧的发送和接收。该键的预期值为bool 。 |
QCanBusDevice::DataBitRateKey | 6 | 该键定义 CAN FD 有效载荷比特率(比特/秒)。CAN FD 允许在标称比特率的仲裁阶段结束后,以更高的数据比特率传输带有QCanBusFrame::hasBitrateSwitch() 标志的帧的有效载荷。该枚举值在 Qt 5.9 中引入。另请参见QCanBusDevice::BitRateKey |
QCanBusDevice::ProtocolKey | 7 | 此键允许指定另一种协议。目前,该参数只能在 SocketCAN 插件中设置和使用。此枚举值在 Qt 5.14 中引入。 |
QCanBusDevice::UserKey | 30 | 此键定义了自定义键的起始范围。它最常见的用途是允许特定平台的配置选项。 |
另请参阅 configurationParameter().
枚举 QCanBusDevice::Direction
flags QCanBusDevice::Directions
该枚举描述了可能的数据传输方向。
常量 | 值 | 描述 |
---|---|---|
QCanBusDevice::Input | 1 | 输入方向。 |
QCanBusDevice::Output | 2 | 输出方向。 |
QCanBusDevice::AllDirections | Input | Output | 输入和输出两个方向。 |
Directions 类型是QFlags<Direction> 的类型定义。它存储方向值的 OR 组合。
成员函数文档
[explicit]
QCanBusDevice::QCanBusDevice(QObject *parent = nullptr)
使用指定的parent 构建一个串行总线设备。
[virtual]
QCanBusDevice::CanBusStatus QCanBusDevice::busStatus()
返回当前 CAN 总线状态。如果无法请求状态,则返回 QCanBusDevice::UnknownStatus。
注意: 并非所有 CAN 插件都能实现该函数。更多信息请参阅插件帮助页面。运行时可使用函数hasBusStatus() 检查所用 CAN 插件是否支持请求 CAN 总线状态。
另请参见 hasBusStatus() 和resetController()。
void QCanBusDevice::clear(QCanBusDevice::Directions direction = Direction::AllDirections)
清除设备的输入或输出缓冲区(取决于direction )。
该函数仅对QCanBusDevice 缓冲区起作用。已经写入 CAN 驱动程序或 CAN 硬件层或尚未从这些层读取的帧不会被该函数清除。
注意: 清除输出缓冲区仅适用于缓冲设备。
另请参阅 framesAvailable()、readFrame()、framesToWrite() 和writeFrame()。
[protected]
void QCanBusDevice::clearError()
清除上次设备错误的错误 ID 和人可读描述。
CAN 总线实施必须使用该函数更新设备的错误状态。
另请参阅 error()、errorOccurred() 和setError()。
[pure virtual protected]
void QCanBusDevice::close()
该函数负责关闭 CAN 总线连接。执行时必须确保实例的state() 设置为QCanBusDevice::UnconnectedState 。
该函数最重要的任务是关闭 CAN 设备的套接字,并调用QCanBusDevice::setState() 。
另请参阅 disconnectDevice()。
QList<QCanBusDevice::ConfigurationKey> QCanBusDevice::configurationKeys() const
返回 CAN 总线连接使用的键值列表。
键的含义等同于ConfigurationKey 。如果没有明确提及键,则使用平台对相关键的默认设置。
QVariant QCanBusDevice::configurationParameter(QCanBusDevice::ConfigurationKey key) const
返回分配给ConfigurationKey key 的当前值;否则返回无效的QVariant 。
另请参阅 setConfigurationParameter() 和configurationKeys()。
bool QCanBusDevice::connectDevice()
将设备连接到 CAN 总线。成功时返回true
,否则返回false
。
该函数调用open() 作为其实现的一部分。
另请参阅 disconnectDevice()。
[protected]
QCanBusFrame QCanBusDevice::dequeueOutgoingFrame()
返回传出帧内部列表中的下一个QCanBusFrame ,否则返回无效的QCanBusFrame 。返回的帧将从内部列表中删除。
[virtual, since 6.2]
QCanBusDeviceInfo QCanBusDevice::deviceInfo() const
返回当前QCanBusDevice 的QCanBusDeviceInfo 。如果QCanBusDevice 的子类未实现该函数,则返回默认构造的对象。
此函数在 Qt 6.2 中引入。
void QCanBusDevice::disconnectDevice()
断开设备与 CAN 总线的连接。
该函数调用close() 作为其实现的一部分。
注意: 只有在connectDevice() 返回true
时,才能调用该函数。
另请参见 connectDevice()。
[protected]
void QCanBusDevice::enqueueOutgoingFrame(const QCanBusFrame &newFrame)
将newFrame 添加到可通过writeFrame() 访问的出帧内部列表中。
子类在编写新帧时必须调用该函数。
[protected]
void QCanBusDevice::enqueueReceivedFrames(const QList<QCanBusFrame> &newFrames)
将newFrames 添加到可使用readFrame() 访问的帧内部列表中,并发出framesReceived() 信号。
子类在接收帧时必须调用该函数。
QCanBusDevice::CanBusError QCanBusDevice::error() const
返回上次发生的错误。错误值始终设置为上次发生的错误,且永不重置。
另请参阅 setError() 和errorString()。
[signal]
void QCanBusDevice::errorOccurred(QCanBusDevice::CanBusError)
发生错误时会发出该信号。
QString QCanBusDevice::errorString() const
返回对上次发生的设备错误的可读描述。
另请参阅 error()。
qint64 QCanBusDevice::framesAvailable() const
返回可用帧数。如果没有可用帧,该函数将返回 0。
另请参阅 clear()、readFrame() 和readAllFrames()。
[signal]
void QCanBusDevice::framesReceived()
当收到一个或多个帧时,就会发出该信号。应使用readFrame() 和framesAvailable() 读取帧。
qint64 QCanBusDevice::framesToWrite() const
对于缓冲设备,该函数返回等待写入的帧数。对于无缓冲的设备,该函数总是返回零。
注意: CAN 驱动程序和 CAN 硬件层可能有额外的缓冲。因此,如果该函数返回零,并不意味着所有 CAN 帧都已写入 CAN 总线。
另请参阅 clear() 和writeFrame()。
[signal]
void QCanBusDevice::framesWritten(qint64 framesCount)
每次向 CAN 总线写入有效帧负载时,都会发出该信号。framesCount 参数设置为写入该有效载荷的帧数。
[virtual]
bool QCanBusDevice::hasBusStatus() const
如果 CAN 插件支持请求 CAN 总线状态,则返回 true。
另请参见 busStatus().
[protected]
bool QCanBusDevice::hasOutgoingFrames() const
如果传出帧的内部列表不为空,则返回true
;否则返回false
。
[pure virtual]
QString QCanBusDevice::interpretErrorFrame(const QCanBusFrame &frame)
将frame 解释为错误框架,并返回可读的错误描述。
如果frame 不是错误框架,则返回空字符串。
[pure virtual protected]
bool QCanBusDevice::open()
该函数由connectDevice() 调用。子类必须提供一个实现,如果 CAN 总线连接可以建立,则返回true
;否则返回false
。QCanBusDevice 实现确保在进入此函数时,设备的state() 已设置为QCanBusDevice::ConnectingState 。
实现必须确保在成功后将实例的state() 设为QCanBusDevice::ConnectedState ;否则QCanBusDevice::UnconnectedState 。setState() 必须用于设置新的设备状态。
自定义实现负责打开套接字、启动可能需要的QSocketNotifier 以及应用自定义和默认的QCanBusDevice::configurationParameter()。
另请参阅 connectDevice()。
QList<QCanBusFrame> QCanBusDevice::readAllFrames()
返回队列中所有QCanBusFrames;否则返回空QList 。返回的帧将从队列中删除。
队列根据先进先出原则运行。
另请参阅 clear()、framesAvailable() 和readFrame()。
QCanBusFrame QCanBusDevice::readFrame()
从队列中返回下一个QCanBusFrame ;否则返回空QCanBusFrame 。返回的帧将从队列中删除。
队列根据先进先出原则运行。
另请参阅 clear()、framesAvailable() 和readAllFrames()。
[virtual]
void QCanBusDevice::resetController()
执行 CAN 控制器复位,尽可能使 CAN 控制器脱离总线关闭状态。
注意: CAN 控制器复位会干扰正在运行的通信,可能需要一秒钟才能完成。只有在出现总线错误时才调用该功能。
注意: 并非所有 CAN 插件都能实现该功能。更多信息请参阅插件帮助页面。
另请参见 busStatus()。
[virtual]
void QCanBusDevice::setConfigurationParameter(QCanBusDevice::ConfigurationKey key, const QVariant &value)
将 CAN 总线连接的配置参数key 设置为value 。电位键由ConfigurationKey 表示。
可以通过设置无效的QVariant 来取消参数设置。取消参数设置意味着将配置重置为默认设置。
注意: 在大多数情况下,配置更改只有在重新连接后才会生效。
另请参阅 configurationParameter() 。
[protected]
void QCanBusDevice::setError(const QString &errorText, QCanBusDevice::CanBusError errorId)
将最后一次设备错误的人可读描述设置为errorText 。errorId 对错误类型进行分类。
CAN 总线实施必须使用该函数更新设备的错误状态。
另请参阅 error()、errorOccurred() 和clearError()。
[protected]
void QCanBusDevice::setState(QCanBusDevice::CanBusDeviceState newState)
将设备状态设置为newState 。CAN 总线实施必须使用该函数更新设备状态。
另请参阅 state() 。
QCanBusDevice::CanBusDeviceState QCanBusDevice::state() const
返回设备的当前状态。
另请参阅 setState() 和stateChanged()。
[signal]
void QCanBusDevice::stateChanged(QCanBusDevice::CanBusDeviceState state)
每次设备状态发生变化时,都会发出该信号。新状态由state 表示。
[virtual]
bool QCanBusDevice::waitForFramesReceived(int msecs)
阻塞,直至有新帧可供读取,且framesReceived() 信号已发出,或msecs 毫秒已过。如果msecs 是-1
,则此函数不会超时。
如果有新帧可供读取,且framesReceived() 信号已发出,则返回true
;否则返回false
(如果操作超时或发生错误)。
注意: 此函数将启动本地事件循环。这可能会导致在此函数作用域执行阻塞时调用其他应用程序槽。为避免出现问题,该类的信号不应连接到槽。同样,也绝不能在响应framesReceived() 或errorOccurred() 信号时调用此函数。
另请参阅 waitForFramesWritten()。
[virtual]
bool QCanBusDevice::waitForFramesWritten(int msecs)
对于缓冲设备,该函数将等待直到所有缓冲帧都已写入设备且framesWritten() 信号已发出,或msecs 毫秒已过。如果msecs 为-1,则此函数不会超时。对于无缓冲区的设备,由于writeFrame() 不需要写缓冲区,因此会立即返回false
。
如果发出framesWritten() 信号,则返回true
;否则返回false
(即如果操作超时或发生错误)。
注意: 此函数将启动本地事件循环。这可能会导致在此函数作用域执行阻塞时调用其他应用程序槽。为避免出现问题,该类的信号不应连接到槽。同样,在响应framesWritten() 或errorOccurred() 信号时,也绝不能调用此函数。
另请参阅 waitForFramesReceived()。
[pure virtual]
bool QCanBusDevice::writeFrame(const QCanBusFrame &frame)
将frame 写入 CAN 总线,成功时返回true
,否则返回false
。
在某些平台上,帧可能被放入队列,返回值可能仅表示成功插入队列。实际帧将在稍后发送。因此,framesWritten() 信号是帧被移交给传输层的最终确认。如果出现错误,则会发出errorOccurred() 信号。
根据 CAN 总线规范,remote transfer request (RTR) 类型的帧没有有效载荷,但有 0 至 8(包括)的长度。该长度表示远端预期的响应有效载荷长度。因此,在使用该功能发送 RTR 帧时,可能仍需要在frame 上设置任意有效载荷。任意有效载荷的长度就是 RTR 帧的预期大小。
另请参阅 QCanBusFrame::setPayload()。
© 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.