QCanBusFrame Class | Qt Serial Bus 5.6

Qt Documentation (Technology Preview) | Qt Serial Bus | C++ Classes | QCanBusFrame

Qt 5.6.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.6

- List of all members, including inherited members

## Public Types

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

## Public Functions

| QCanBusFrame(QCanBusFrame::FrameType type)
| QCanBusFrame(quint32 identifier = 0, const QByteArray &data = QByteArray())
QCanBusFrame::FrameErrors | error() const
quint32 | frameId() const
FrameType | frameType() const
bool | hasExtendedFrameFormat() const
bool | isValid() const
QByteArray | payload() const
void | setError(QCanBusFrame::FrameErrors error)
void | setExtendedFrameFormat(bool isExtended)
void | setFrameId(quint32 newFrameId)
void | setFrameType(FrameType newType)
void | setPayload(const QByteArray &data)
void | setTimeStamp(const TimeStamp &ts)
TimeStamp | timeStamp() const

## Related Non-Members

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

## Detailed Description

QCanBusFrame is a container class representing a single CAN frame.

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::FrameType type)

Constructs a CAN frame of the specified type.

### QCanBusFrame::QCanBusFrame(quint32 identifier = 0, const QByteArray &data = QByteArray())

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

### 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().

### quint32 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().

### FrameType QCanBusFrame::frameType() const

Returns the type of the frame.

See also setFrameType().

### 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::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.

Otherwise this function returns `true`.

### QByteArray QCanBusFrame::payload() const

Returns the data payload of the frame.

See also setPayload().

### 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().

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

Sets the extended frame format flag to isExtended.

See also hasExtendedFrameFormat().

### void QCanBusFrame::setFrameId(quint32 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 adapted to match newFrameId.

See also frameId() and hasExtendedFrameFormat().

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

Sets the type of the frame to newType.

See also frameType().

### 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. 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 length expection 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().

### void QCanBusFrame::setTimeStamp(const 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.

### TimeStamp QCanBusFrame::timeStamp() const

Returns the timestamp of the frame.

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

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