QCanBusDevice 類

QCanBusDevice 類是用於 CAN 總綫的接口類。 更多...

頭: #include <QCanBusDevice>
qmake: QT += serialbus
Since: Qt 5.8
繼承: QObject

該類在 Qt 5.8 引入。

公共類型

struct Filter
enum CanBusDeviceState { UnconnectedState, ConnectingState, ConnectedState, ClosingState }
enum CanBusError { NoError, ReadError, WriteError, ConnectionError, ConfigurationError, …, TimeoutError }
枚舉類 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)
QCanBusDevice::CanBusStatus busStatus () const
void clear (QCanBusDevice::Directions direction = Direction::AllDirections)
QVector<int> configurationKeys () const
QVariant configurationParameter (int key ) const
bool connectDevice ()
void disconnectDevice ()
QCanBusDevice::CanBusError error () const
QString errorString () const
qint64 framesAvailable () const
qint64 framesToWrite () const
bool hasBusStatus () const
virtual QString interpretErrorFrame (const QCanBusFrame & frame ) = 0
QVector<QCanBusFrame> readAllFrames ()
QCanBusFrame readFrame ()
void resetController ()
virtual void setConfigurationParameter (int 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 QVector<QCanBusFrame> & newFrames )
bool hasOutgoingFrames () const
virtual bool open () = 0
void setCanBusStatusGetter (std::function<CanBusStatus ()> busStatusGetter )
void setError (const QString & errorText , QCanBusDevice::CanBusError errorId )
void setResetControllerFunction (std::function<void ()> resetter )
void setState (QCanBusDevice::CanBusDeviceState newState )

詳細描述

QCanBusDevice communicates with a CAN plugin providing users with a convenient API. The CAN plugin must be specified during the object creation.

成員類型文檔編製

enum QCanBusDevice:: CanBusDeviceState

此枚舉描述所有可能的設備狀態。

常量 描述
QCanBusDevice::UnconnectedState 0 設備已斷開連接。
QCanBusDevice::ConnectingState 1 設備正在連接。
QCanBusDevice::ConnectedState 2 設備已連接到 CAN Bus。
QCanBusDevice::ClosingState 3 設備正在關閉。

enum QCanBusDevice:: CanBusError

此枚舉描述所有可能的錯誤條件。

常量 描述
QCanBusDevice::NoError 0 沒有齣現錯誤。
QCanBusDevice::ReadError 1 在讀取操作期間齣現錯誤。
QCanBusDevice::WriteError 2 在寫入操作期間齣現錯誤。
QCanBusDevice::ConnectionError 3 An error occurred when attempting to open the plugin.
QCanBusDevice::ConfigurationError 4 An error occurred when attempting to set a configuration parameter.
QCanBusDevice::UnknownError 5 齣現未知錯誤。
QCanBusDevice::OperationError 6 An operation was attempted while the device was in a state that did not permit it. This enum was introduced in Qt 5.14.
QCanBusDevice::TimeoutError 7 An timeout occurred while waiting for frames written or received. This enum was introduced in Qt 5.14.

enum class QCanBusDevice:: CanBusStatus

此枚舉描述可能的 CAN Bus 狀態值。

常量 描述
QCanBusDevice::CanBusStatus::Unknown 0 The CAN bus status is unknown (e.g. not supported by the CAN plugin).
QCanBusDevice::CanBusStatus::Good 1 The CAN controller is fully operational
QCanBusDevice::CanBusStatus::Warning 2 The CAN controller is in warning status
QCanBusDevice::CanBusStatus::Error 3 The CAN controller is in error status (no longer sending CAN frames)
QCanBusDevice::CanBusStatus::BusOff 4 The CAN controller is in bus off status (disconnected from the CAN bus)

該枚舉在 Qt 5.14 引入或被修改。

enum QCanBusDevice:: ConfigurationKey

This enum describes the possible configuration options for the CAN bus connection.

常量 描述
QCanBusDevice::RawFilterKey 0 This configuration determines the type of CAN bus frames that the current device accepts. The expected value is QList<QCanBusDevice::Filter> . Passing an empty list clears all previously set filters including default filters. For more details see QCanBusDevice::Filter .
QCanBusDevice::ErrorFilterKey 1 This key defines the type of error that should be forwarded via the current connection. The associated value should be of type QCanBusFrame::FrameErrors .
QCanBusDevice::LoopbackKey 2 This key defines whether the CAN bus device should operate in loopback mode. Loopback means, whenever a CAN frame is transmitted on the CAN bus, a local echo of this frame is sent to all applications connected to this CAN device. The expected value for this key is bool .
QCanBusDevice::ReceiveOwnKey 3 This key defines whether this CAN device receives its own send frames. This can be used to check if the transmission was successful. The expected value for this key is bool .
QCanBusDevice::BitRateKey 4 This key defines the CAN bitrate in bits per second. With CAN FD, the payload can be transmitted at a higher data bitrate, if QCanBusFrame::hasBitrateSwitch () is set. In this case, QCanBusDevice::BitRateKey is only used for the CAN ID arbitration phase. See also QCanBusDevice::DataBitRateKey
QCanBusDevice::CanFdKey 5 This key defines whether sending and receiving of CAN FD frames should be enabled. The expected value for this key is bool .
QCanBusDevice::DataBitRateKey 6 This key defines the CAN FD payload bitrate in bits per second. CAN FD allows to transmit the payload of frames with QCanBusFrame::hasBitrateSwitch () flag at a higher data bitrate, after the arbitration phase at the nominal bitrate is finished. This enum value was introduced in Qt 5.9. See also QCanBusDevice::BitRateKey
QCanBusDevice::ProtocolKey 7 This key allows to specify another protocol. For now, this parameter can only be set and used in the SocketCAN plugin. This enum value was introduced in Qt 5.14.
QCanBusDevice::UserKey 30 This key defines the range where custom keys start. Its most common purpose is to permit platform-specific configuration options.

另請參閱 configurationParameter ().

enum QCanBusDevice:: Direction
flags QCanBusDevice:: Directions

此枚舉描述可能的數據傳輸方嚮。

常量 描述
QCanBusDevice::Input 1 輸入方嚮。
QCanBusDevice::Output 2 輸齣方嚮。
QCanBusDevice::AllDirections Input | Output 雙嚮,輸入和輸齣。

該枚舉在 Qt 5.12 引入或被修改。

Directions 類型是 typedef 對於 QFlags <Direction>。它存儲 Direction 值的 OR 組閤。

成員函數文檔編製

QCanBusDevice:: QCanBusDevice ( QObject * parent = nullptr)

構造串行總綫設備采用指定 parent .

[signal] void QCanBusDevice:: errorOccurred ( QCanBusDevice::CanBusError )

此信號被發射,當齣現錯誤時。

[signal] void QCanBusDevice:: framesReceived ()

This signal is emitted when one or more frames have been received. The frames should be read using readFrame () 和 framesAvailable ().

[signal] void QCanBusDevice:: framesWritten ( qint64 framesCount )

This signal is emitted every time a payload of frames has been written to the CAN bus. The framesCount argument is set to the number of frames that were written in this payload.

[signal] void QCanBusDevice:: stateChanged ( QCanBusDevice::CanBusDeviceState state )

This signal is emitted every time the state of the device changes. The new state is represented by state .

另請參閱 setState () 和 state ().

QCanBusDevice::CanBusStatus QCanBusDevice:: busStatus () const

Returns the current CAN bus status. If the status cannot be requested, QCanBusDevice::UnknownStatus is returned.

注意: This function may not be implemented in all CAN plugins. Please refer to the plugins help pages for more information. The function hasBusStatus () can be used at runtime to check if the used CAN plugin has support for requesting the CAN bus status.

該函數在 Qt 5.14 引入。

另請參閱 hasBusStatus () 和 resetController ().

void QCanBusDevice:: clear ( QCanBusDevice::Directions direction = Direction::AllDirections)

清零設備輸入 (或輸齣) 緩衝,從屬 direction .

This function only operates on QCanBusDevice buffers. Frames that are already written to the CAN driver or CAN hardware layer, or that are not yet read from these layers, are not cleared by this function.

注意: Clearing the output buffers is only possible for buffered devices.

該函數在 Qt 5.12 引入。

另請參閱 framesAvailable (), readFrame (), framesToWrite (),和 writeFrame ().

[protected] void QCanBusDevice:: clearError ()

Clears the error id and the human readable description of the last device error.

CAN bus implementations must use this function to update the device's error state.

該函數在 Qt 5.14 引入。

另請參閱 error (), errorOccurred (),和 setError ().

[pure virtual protected] void QCanBusDevice:: close ()

This function is responsible for closing the CAN bus connection. The implementation must ensure that the instance's state () 被設為 QCanBusDevice::UnconnectedState .

This function's most important task is to close the socket to the CAN device and to call QCanBusDevice::setState ().

另請參閱 disconnectDevice ().

QVector < int > QCanBusDevice:: configurationKeys () const

Returns the list of keys used by the CAN bus connection.

The the meaning of the keys is equivalent to ConfigurationKey . If a key is not explicitly mentioned the platform's default setting for the relevant key is used.

QVariant QCanBusDevice:: configurationParameter ( int key ) const

Returns the current value assigned to the ConfigurationKey key ; otherwise an invalid QVariant .

另請參閱 setConfigurationParameter () 和 configurationKeys ().

bool QCanBusDevice:: connectDevice ()

將設備連接到 CAN (控製器局域網) 總綫。返迴 true 當成功時;否則 false .

此函數調用 open () 作為其實現的一部分。

另請參閱 disconnectDevice ().

[protected] QCanBusFrame QCanBusDevice:: dequeueOutgoingFrame ()

返迴下一 QCanBusFrame from the internal list of outgoing frames; otherwise returns an invalid QCanBusFrame . The returned frame is removed from the internal list.

void QCanBusDevice:: disconnectDevice ()

從 CAN (控製器局域網) 總綫斷開設備連接。

此函數調用 close () 作為其實現的一部分。

注意: This function should only be called, if connectDevice () 返迴 true .

另請參閱 connectDevice ().

[protected] void QCanBusDevice:: enqueueOutgoingFrame (const QCanBusFrame & newFrame )

追加 newFrame to the internal list of outgoing frames which can be accessed by writeFrame ().

Subclasses must call this function when they write a new frame.

[protected] void QCanBusDevice:: enqueueReceivedFrames (const QVector < QCanBusFrame > & newFrames )

追加 newFrames to the internal list of frames which can be accessed using readFrame () and emits the framesReceived () 信號。

Subclasses must call this function when they receive frames.

QCanBusDevice::CanBusError QCanBusDevice:: error () const

Returns the last error that has occurred. The error value is always set to last error that occurred and it is never reset.

另請參閱 setError () 和 errorString ().

QString QCanBusDevice:: errorString () const

返迴人類可讀的最後發生的設備錯誤描述。

另請參閱 error ().

qint64 QCanBusDevice:: framesAvailable () const

Returns the number of available frames. If no frames are available, this function returns 0.

另請參閱 clear (), readFrame (),和 readAllFrames ().

qint64 QCanBusDevice:: framesToWrite () const

For buffered devices, this function returns the number of frames waiting to be written. For unbuffered devices, this function always returns zero.

注意: There may be additional buffering in the CAN driver and CAN hardware layer. Therefore, if this function returns zero, that does not mean all CAN frames are already written to the CAN bus.

另請參閱 clear () 和 writeFrame ().

bool QCanBusDevice:: hasBusStatus () const

Return true, if the CAN plugin supports requesting the CAN bus status.

該函數在 Qt 5.14 引入。

另請參閱 busStatus ().

[protected] bool QCanBusDevice:: hasOutgoingFrames () const

返迴 true if the internal list of outgoing frames is not empty; otherwise returns false .

[pure virtual] QString QCanBusDevice:: interpretErrorFrame (const QCanBusFrame & frame )

Interprets frame as error frame and returns a human readable description of the error.

frame is not an error frame, the returned string is empty.

[pure virtual protected] bool QCanBusDevice:: open ()

此函數被調用通過 connectDevice (). Subclasses must provide an implementation which returns true if the CAN bus connection could be established; otherwise false QCanBusDevice implementation ensures upon entry of this function that the device's state () 被設為 QCanBusDevice::ConnectingState already.

The implementation must ensure that upon success the instance's state () 被設為 QCanBusDevice::ConnectedState ;否則 QCanBusDevice::UnconnectedState . setState () must be used to set the new device state.

The custom implementation is responsible for opening the socket, instanciation of a potentially required QSocketNotifier and the application of custom and default QCanBusDevice::configurationParameter ().

另請參閱 connectDevice ().

QVector < QCanBusFrame > QCanBusDevice:: readAllFrames ()

Returns all QCanBusFrame s from the queue; otherwise returns an empty QVector . The returned frames are removed from the queue.

The queue operates according to the FIFO principle.

該函數在 Qt 5.12 引入。

另請參閱 clear (), framesAvailable (),和 readFrame ().

QCanBusFrame QCanBusDevice:: readFrame ()

返迴下一 QCanBusFrame from the queue; otherwise returns an empty QCanBusFrame . The returned frame is removed from the queue.

The queue operates according to the FIFO principle.

另請參閱 clear (), framesAvailable (),和 readAllFrames ().

void QCanBusDevice:: resetController ()

Performs a CAN controller reset to release the CAN controller from bus off state, if possible.

注意: CAN controller resets disturb the running communication and may take up to one second to complete. Only call this function to recover from bus errors.

注意: This function may not be implemented in all CAN plugins. Please refer to the plugins help pages for more information.

該函數在 Qt 5.14 引入。

另請參閱 busStatus ().

[protected] void QCanBusDevice:: setCanBusStatusGetter ( std::function < CanBusStatus ()> busStatusGetter )

Called from the derived plugin to register a function busStatusGetter which returns the CAN controller bus status when busStatus () 被調用。

該函數在 Qt 5.14 引入。

[虛擬] void QCanBusDevice:: setConfigurationParameter ( int key , const QVariant & value )

Sets the configuration parameter key for the CAN bus connection to value . The potential keys are represented by ConfigurationKey .

A parameter can be unset by setting an invalid QVariant . Unsetting a parameter implies that the configuration is reset to its default setting.

注意: In most cases, configuration changes only take effect after a reconnect.

另請參閱 configurationParameter ().

[protected] void QCanBusDevice:: setError (const QString & errorText , QCanBusDevice::CanBusError errorId )

Sets the human readable description of the last device error to errorText . errorId categorizes the type of error.

CAN bus implementations must use this function to update the device's error state.

另請參閱 error (), errorOccurred (),和 clearError ().

[protected] void QCanBusDevice:: setResetControllerFunction ( std::function < void ()> resetter )

Called from the derived plugin to register a function resetter which performs the CAN controller hardware reset when resetController () 被調用。

該函數在 Qt 5.14 引入。

[protected] void QCanBusDevice:: setState ( QCanBusDevice::CanBusDeviceState newState )

把設備的狀態設為 newState . CAN bus implementations must use this function to update the device state.

另請參閱 state ().

QCanBusDevice::CanBusDeviceState QCanBusDevice:: state () const

返迴設備的當前狀態。

另請參閱 setState () 和 stateChanged ().

[虛擬] bool QCanBusDevice:: waitForFramesReceived ( int msecs )

Blocks until new frames are available for reading and the framesReceived () 信號已發射,或直到 msecs 毫秒已過去。若 msecs is -1 ,此函數不會超時。

返迴 true if new frames are available for reading and the framesReceived () signal is emitted; otherwise returns false (若操作超時或齣現錯誤)。

注意: This function will start a local event loop. This may lead to scenarios whereby other application slots may be called while the execution of this function scope is blocking. To avoid problems, the signals for this class should not be connected to slots. Similarly this function must never be called in response to the framesReceived () 或 errorOccurred () 信號。

另請參閱 waitForFramesWritten ().

[虛擬] bool QCanBusDevice:: waitForFramesWritten ( int msecs )

For buffered devices, this function waits until all buffered frames have been written to the device and the framesWritten () 信號已發射,或直到 msecs 毫秒已過去。若 msecs is -1, this function will not time out. For unbuffered devices, it returns immediately with false as writeFrame () does not require a write buffer.

返迴 true framesWritten () signal is emitted; otherwise returns false (即:若操作超時或發生錯誤)。

注意: This function will start a local event loop. This may lead to scenarios whereby other application slots may be called while the execution of this function scope is blocking. To avoid problems, the signals for this class should not be connected to slots. Similarly this function must never be called in response to the framesWritten () 或 errorOccurred () 信號。

另請參閱 waitForFramesReceived ().

[pure virtual] bool QCanBusDevice:: writeFrame (const QCanBusFrame & frame )

寫入 frame 到 CAN (控製器局域網) 總綫並返迴 true 當成功時;否則 false .

On some platforms, the frame may be put into a queue and the return value may only indicate a successful insertion into the queue. The actual frame will be send later on. Therefore the framesWritten () signal is the final confirmation that the frame has been handed off to the transport layer. If an error occurs the errorOccurred () 發射。

As per CAN bus specification, frames of type remote transfer request (RTR) do not have a payload, but a length from 0 to 8 (including). This length indicates the expected response payload length from the remote party. Therefore when sending a RTR frame using this function it may still be required to set an arbitrary payload on frame . The length of the arbitrary payload is what is set as size expectation for the RTR frame.

另請參閱 QCanBusFrame::setPayload ().