- Qt 6.4
- Qt Serial Bus
- C++ Classes
- QCanBusFrame
- Qt 6.4.2 Reference Documentation

# QCanBusFrame Class

QCanBusFrame is a container class representing a single CAN frame. More...

Header: | #include <QCanBusFrame>
qmake: | QT += serialbus
Since: | Qt 5.8

- List of all members, including inherited members

## Public Types

class | TimeStamp
enum | FrameError { NoError, TransmissionTimeoutError, LostArbitrationError, ControllerError, ProtocolViolationError, …, AnyError }
flags | FrameErrors
enum | FrameType { UnknownFrame, DataFrame, ErrorFrame, RemoteRequestFrame, InvalidFrame }

## Public Functions

| QCanBusFrame(QCanBusFrame::FrameId identifier, const QByteArray &data)
| QCanBusFrame(QCanBusFrame::FrameType type = DataFrame)
QCanBusFrame::FrameErrors | error() const
QCanBusFrame::FrameId | frameId() const
QCanBusFrame::FrameType | frameType() const
bool | hasBitrateSwitch() const
bool | hasErrorStateIndicator() const
bool | hasExtendedFrameFormat() const
bool | hasFlexibleDataRateFormat() const
bool | hasLocalEcho() const
bool | isValid() const
QByteArray | payload() const
void | setBitrateSwitch(bool bitrateSwitch)
void | setError(QCanBusFrame::FrameErrors error)
void | setErrorStateIndicator(bool errorStateIndicator)
void | setExtendedFrameFormat(bool isExtended)
void | setFlexibleDataRateFormat(bool isFlexibleData)
void | setFrameId(QCanBusFrame::FrameId newFrameId)
void | setFrameType(QCanBusFrame::FrameType newType)
void | setLocalEcho(bool echo)
void | setPayload(const QByteArray &data)
void | setTimeStamp(QCanBusFrame::TimeStamp ts)
QCanBusFrame::TimeStamp | timeStamp() const
QString | toString() const

## Related Non-Members

QDataStream & | operator<<(QDataStream &out, const QCanBusFrame &frame)
QDataStream & | operator>>(QDataStream &in, QCanBusFrame &frame)

## Detailed Description

QCanBusDevice can use QCanBusFrame for read and write operations. It contains the frame identifier and the data payload. QCanBusFrame contains the timestamp of the moment it was read.

See also QCanBusFrame::TimeStamp.

## Member Type Documentation

### enum QCanBusFrame::FrameError
flags QCanBusFrame::FrameErrors

This enum describes the possible error types.

Constant | Value | Description
--- | --- | ---
`QCanBusFrame::NoError` | `0` | No error has occurred.
`QCanBusFrame::TransmissionTimeoutError` | `(1 << 0)` | The transmission has timed out.
`QCanBusFrame::LostArbitrationError` | `(1 << 1)` | The frame could not be sent due to lost arbitration on the bus.
`QCanBusFrame::ControllerError` | `(1 << 2)` | The controller encountered an error.
`QCanBusFrame::ProtocolViolationError` | `(1 << 3)` | A protocol violation has occurred.
`QCanBusFrame::TransceiverError` | `(1 << 4)` | A transceiver error occurred
`QCanBusFrame::MissingAcknowledgmentError` | `(1 << 5)` | The transmission received no acknowledgment.
`QCanBusFrame::BusOffError` | `(1 << 6)` | The CAN bus is offline.
`QCanBusFrame::BusError` | `(1 << 7)` | A CAN bus error occurred.
`QCanBusFrame::ControllerRestartError` | `(1 << 8)` | The controller restarted.
`QCanBusFrame::UnknownError` | `(1 << 9)` | An unknown error has occurred.
`QCanBusFrame::AnyError` | `0x1FFFFFFFU` | Matches every other error type.

The FrameErrors type is a typedef for QFlags<FrameError>. It stores an OR combination of FrameError values.

### enum QCanBusFrame::FrameType

This enum describes the type of the CAN frame.

Constant | Value | Description
--- | --- | ---
`QCanBusFrame::UnknownFrame` | `0x0` | The frame type is unknown.
`QCanBusFrame::DataFrame` | `0x1` | This value represents a data frame.
`QCanBusFrame::ErrorFrame` | `0x2` | This value represents an error frame.
`QCanBusFrame::RemoteRequestFrame` | `0x3` | This value represents a remote request.
`QCanBusFrame::InvalidFrame` | `0x4` | This value represents an invalid frame. This type is used for error reporting.

See also setFrameType().

## Member Function Documentation

### QCanBusFrame::QCanBusFrame(QCanBusFrame::FrameId identifier, const QByteArray &data)

Constructs a CAN frame using identifier as the frame identifier and data as the payload.

### QCanBusFrame::QCanBusFrame(QCanBusFrame::FrameType type = DataFrame)

Constructs a CAN frame of the specified type.

### QCanBusFrame::FrameErrors QCanBusFrame::error() const

Returns the error of the current error frame. If the frame is not an ErrorFrame, this function returns NoError.

See also setError().

### QCanBusFrame::FrameId QCanBusFrame::frameId() const

Returns the CAN frame identifier. If the CAN frame uses the extended frame format, the identifier has a maximum of 29 bits; otherwise 11 bits.

If the frame is of ErrorFrame type, this ID is always 0.

See also setFrameId() and hasExtendedFrameFormat().

### QCanBusFrame::FrameType QCanBusFrame::frameType() const

Returns the type of the frame.

See also setFrameType().

### `[since 5.9]` bool QCanBusFrame::hasBitrateSwitch() const

Returns `true` if the CAN uses Flexible Data-Rate with Bitrate Switch, to transfer the payload data at a higher data bitrate.

This function was introduced in Qt 5.9.

See also setBitrateSwitch() and QCanBusDevice::DataBitRateKey.

### `[since 5.9]` bool QCanBusFrame::hasErrorStateIndicator() const

Returns `true` if the CAN uses Flexible Data-Rate with Error State Indicator set.

This flag is set by the transmitter's CAN FD hardware to indicate the transmitter's error state.

This function was introduced in Qt 5.9.

See also setErrorStateIndicator().

### bool QCanBusFrame::hasExtendedFrameFormat() const

Returns `true` if the CAN frame uses a 29bit identifier; otherwise `false`, implying an 11bit identifier.

See also setExtendedFrameFormat() and frameId().

### bool QCanBusFrame::hasFlexibleDataRateFormat() const

Returns `true` if the CAN frame uses Flexible Data-Rate which allows up to 64 data bytes, otherwise `false`, implying at most 8 byte of payload.

See also setFlexibleDataRateFormat() and payload().

### `[since 5.10]` bool QCanBusFrame::hasLocalEcho() const

Returns `true` if the frame is a local echo frame, i.e. a frame that is received as echo when the frame with the same content was successfully sent to the CAN bus. This flag is set for frames sent by the application itself as well as for frames sent by other applications running on the same system.

QCanBusDevice::ReceiveOwnKey must be set to true to receive echo frames.

This function was introduced in Qt 5.10.

See also setLocalEcho(), QCanBusDevice::ReceiveOwnKey, and QCanBusDevice::LoopbackKey.

### bool QCanBusFrame::isValid() const

Returns `false` if the frameType() is InvalidFrame, the hasExtendedFrameFormat() is not set although frameId() is longer than 11 bit or the payload is longer than the maximal permitted payload length of 64 byte if Flexible Data-Rate mode is enabled or 8 byte if it is disabled. If frameType() is RemoteRequestFrame and the Flexible Data-Rate mode is enabled at the same time `false` is also returned.

Otherwise this function returns `true`.

### QByteArray QCanBusFrame::payload() const

Returns the data payload of the frame.

See also setPayload().

### `[since 5.9]` void QCanBusFrame::setBitrateSwitch(bool bitrateSwitch)

Set the Flexible Data-Rate flag Bitrate Switch flag to bitrateSwitch. The data field of frames with this flag is transferred at a higher data bitrate.

This function was introduced in Qt 5.9.

See also hasBitrateSwitch() and QCanBusDevice::DataBitRateKey.

### void QCanBusFrame::setError(QCanBusFrame::FrameErrors error)

Sets the frame's error type. This function does nothing if frameType() is not an ErrorFrame.

See also error().

### `[since 5.9]` void QCanBusFrame::setErrorStateIndicator(bool errorStateIndicator)

Set the Flexible Data-Rate flag Error State Indicator flag to errorStateIndicator.

When sending CAN FD frames, this flag is automatically set by the CAN FD hardware. `QCanBusFrame::setErrorStateIndicator()` should only be used for application testing, e.g. on virtual CAN FD busses.

This function was introduced in Qt 5.9.

See also hasErrorStateIndicator().

### void QCanBusFrame::setExtendedFrameFormat(bool isExtended)

Sets the extended frame format flag to isExtended.

See also hasExtendedFrameFormat().

### void QCanBusFrame::setFlexibleDataRateFormat(bool isFlexibleData)

Sets the Flexible Data-Rate flag to isFlexibleData. Those frames can be sent using a higher speed on supporting controllers. Additionally the payload length limit is raised to 64 byte.

See also hasFlexibleDataRateFormat().

### void QCanBusFrame::setFrameId(QCanBusFrame::FrameId newFrameId)

Sets the identifier of the CAN frame to newFrameId.

The maximum size of a CAN frame identifier is 11 bits, which can be extended up to 29 bits by supporting the CAN extended frame format. The CAN extended frame format setting is automatically set when a newFrameId with more than 11 bits in size is given.

When the format is extended and a newFrameId with up to 11 bits or less is passed, the CAN extended frame format setting is not changed.

See also frameId() and hasExtendedFrameFormat().

### void QCanBusFrame::setFrameType(QCanBusFrame::FrameType newType)

Sets the type of the frame to newType.

See also frameType().

### `[since 5.10]` void QCanBusFrame::setLocalEcho(bool echo)

Set the Local Echo flag to echo.

When sending CAN bus frames with QCanBusDevice::ReceiveOwnKey enabled, all successfully sent frames are echoed to the receive queue and marked as local echo frames. `QCanBusFrame::setLocalEcho` should therefore only be used for application testing, e.g. on virtual CAN busses.

This function was introduced in Qt 5.10.

See also hasLocalEcho().

### void QCanBusFrame::setPayload(const QByteArray &data)

Sets data as the payload for the CAN frame. The maximum size of payload is 8 bytes, which can be extended up to 64 bytes by supporting Flexible Data-Rate. If data contains more than 8 byte the Flexible Data-Rate flag is automatically set. Flexible Data-Rate has to be enabled on the QCanBusDevice by setting the QCanBusDevice::CanFdKey.

Frames of type RemoteRequestFrame (RTR) do not have a payload. However they have to provide an indication of the responses expected payload length. To set the expected length it is necessary to set a fake payload whose length matches the expected payload length of the response. One way of doing this might be as follows:

     QCanBusFrame frame(QCanBusFrame::RemoteRequestFrame);
     int expectedResponseLength = ...;
     frame.setPayload(QByteArray(expectedResponseLength, 0));

See also payload() and hasFlexibleDataRateFormat().

### void QCanBusFrame::setTimeStamp(QCanBusFrame::TimeStamp ts)

Sets ts as the timestamp for the CAN frame. Usually, this function is not needed, because the timestamp is created during the read operation and not needed during the write operation.

See also timeStamp() and QCanBusFrame::TimeStamp.

### QCanBusFrame::TimeStamp QCanBusFrame::timeStamp() const

Returns the timestamp of the frame.

See also QCanBusFrame::TimeStamp and QCanBusFrame::setTimeStamp().

### QString QCanBusFrame::toString() const

Returns the CAN frame as a formatted string.

The output contains the CAN identifier in hexadecimal format, right adjusted to 32 bit, followed by the data length in square brackets and the payload in hexadecimal format.

Standard identifiers are filled with spaces while extended identifiers are filled with zeros.

Typical outputs are:

     (Error)                                  - error frame
          7FF   [1]  01                       - data frame with standard identifier
     1FFFFFFF   [8]  01 23 45 67 89 AB CD EF  - data frame with extended identifier
          400  [10]  01 23 45 67 ... EF 01 23 - CAN FD frame
          123   [5]  Remote Request           - remote frame with standard identifier
     00000234   [0]  Remote Request           - remote frame with extended identifier

## Related Non-Members

### QDataStream &operator<<(QDataStream &out, const QCanBusFrame &frame)

Writes a frame to the stream (out) and returns a reference to the it.

### QDataStream &operator>>(QDataStream &in, QCanBusFrame &frame)

Reads a frame from the stream (in) and returns a reference to the it.