QCanDbcFileParser Class | Qt Serial Bus | Qt 6.10.2
Description: The QCanDbcFileParser class can be used to parse DBC files.

- Qt 6.10
- Qt Serial Bus
- C++ Classes
- QCanDbcFileParser
- Qt 6.10.2 Reference Documentation

# QCanDbcFileParser Class

The QCanDbcFileParser class can be used to parse DBC files. More...

Header: | `#include <QCanDbcFileParser>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS SerialBus)` `target_link_libraries(mytarget PRIVATE Qt6::SerialBus)`
qmake: | `QT += serialbus`
Since: | Qt 6.5
Status: | Preliminary

This class is under development and is subject to change.

- List of all members, including inherited members

## Public Types

enum class | Error { None, FileReading, Parsing }

## Public Functions

| ~QCanDbcFileParser()
Q_DECL_IMPORT | parse(const int &fileNames)
`(since 6.7)` Q_DECL_IMPORT | parseData(int data)

## Detailed Description

A CAN database or CAN DBC file is an ASCII text file that contains information on how to decode and interpret raw CAN bus data. Some more details about the format can be found here or here.

The QCanDbcFileParser class takes the input DBC file, parses it, and provides a list of QCanMessageDescriptions as an output. These message descriptions can be forwarded to QCanFrameProcessor, and later used as rules to encode or decode QCanBusFrames.

Use one of parse() overloads to specify a file or a list of files that will be processed. Both overloads return `true` if the parsing completes successfully and `false` otherwise.

Call the error() method to get the error which occurred during the parsing. If the parsing completes successfully, this method will return None. Otherwise, you can use an errorString() method to get the string representation of an error.

During the parsing some non-critical problems may occur as well. Such problems will be logged, but the parsing process will not be aborted. You can use the warnings() method to get the full list of such problems after the parsing is completed.

If the parsing completes successfully, call messageDescriptions() to get a list of the message descriptions that were extracted during the last parse() call. Call messageValueDescriptions() to get the textual descriptions of signal raw values, if they are available.

Use the static uniqueIdDescription() function to get a QCanUniqueIdDescription for the DBC format.

     QCanDbcFileParser fileParser;
     const bool result = fileParser.parse(u"path/to/file.dbc"_s);
     // Check result, call error() and warnings() if needed

     // Prepare a QCanFrameProcessor to decode or encode DBC frames
     QCanFrameProcessor frameProcessor;
     frameProcessor.setUniqueIdDescription(QCanDbcFileParser::uniqueIdDescription());
     frameProcessor.setMessageDescriptions(fileParser.messageDescriptions());

Note: The parser is stateful, which means that all the results (like extracted message descriptions, error code, or warnings) are reset once the next parsing starts.

#### Supported Keywords

The current implementation supports only a subset of keywords that you can find in a DBC file:

- `BO_` - message description.
- `SG_` - signal description.
- `SIG_VALTYPE_` - signal type description.
- `SG_MUL_VAL_` - extended multiplexing description.
- `CM_` - comments (only for message and signal descriptions).
- `VAL_` - textual descriptions for raw signal values.

Lines starting from other keywords are simply ignored.

See also QCanMessageDescription and QCanFrameProcessor.

## Member Type Documentation

### enum class QCanDbcFileParser::Error

This enum represents the possible errors that can happen during the parsing of a DBC file.

Constant | Value | Description
--- | --- | ---
`QCanDbcFileParser::Error::None` | `0` | No error occurred.
`QCanDbcFileParser::Error::FileReading` | `1` | An error occurred while opening or reading the file.
`QCanDbcFileParser::Error::Parsing` | `2` | An error occurred while parsing the content of the file.

## Member Function Documentation

### `[noexcept]` QCanDbcFileParser::~QCanDbcFileParser()

Destroys this DBC file parser.

### Q_DECL_IMPORT QCanDbcFileParser::parse(const int &fileNames)

Parses a list of files fileNames. Returns `true` if the parsing completed successfully or `false` otherwise.

If the parsing completed successfully, call the messageDescriptions() method to get the list of all extracted message descriptions.

The parsing stops at the first error. Call the error() and errorString() methods to get the information about the error.

Call the warnings() method to get the list of warnings that were logged during the parsing.

Note: This method expects the file contents to be encoded in UTF-8. If the file has a different encoding, decode it first, and use parseData() to extract the DBC information.

See also messageDescriptions(), error(), warnings(), and parseData().

### `[since 6.7]` Q_DECL_IMPORT QCanDbcFileParser::parseData(int data)

Parses the input data data and returns `true` if the parsing completed successfully or `false` otherwise.

If the parsing completed successfully, call the messageDescriptions() method to get the list of all extracted message descriptions.

If the parsing failed, call the error() and errorString() methods to get the information about the error.

Call the warnings() method to get the list of warnings that were logged during the parsing.

The method expects that data is the content of a valid DBC file, properly converted to QStringView.

Use this method when the input file has an encoding different from UTF-8.

     // Read the data from a DBC file with custom encoding
     const QByteArray initialData = ...;
     // Convert to UTF-16 using QStringDecoder or some other way
     const QString decodedData = ...;
     QCanDbcFileParser parser;
     const bool result = parser.parseData(decodedData);

This function was introduced in Qt 6.7.

See also messageDescriptions(), error(), warnings(), and parse().