Added decoding support to runtime decode.
This commit is contained in:
76
main.cpp
76
main.cpp
@@ -1,19 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
#include "dbc_parser.h"
|
||||
#include "dbc_tree_builder.h"
|
||||
#include "tree_node.h"
|
||||
|
||||
static void PrintReceivers (const std::vector<std::string> &receivers) {
|
||||
for (std::size_t index = 0U; index < receivers.size(); ++index) {
|
||||
if (index != 0U)
|
||||
std::cout << ",";
|
||||
|
||||
std::cout << receivers[index];
|
||||
}
|
||||
}
|
||||
#include "dbc_decode_builder.h"
|
||||
#include "dbc_decoder.h"
|
||||
|
||||
static void PrintTree (const TreeNode *node, int indent) {
|
||||
if (node == nullptr)
|
||||
@@ -32,15 +26,12 @@ static void PrintTree (const TreeNode *node, int indent) {
|
||||
std::cout << "[frame] " << node->GetName();
|
||||
|
||||
if (frame != nullptr) {
|
||||
std::cout << " id=" << frame->canId
|
||||
<< " dlc=" << static_cast<unsigned int> (frame->dlc)
|
||||
<< " tx=" << frame->transmitter;
|
||||
std::cout << " id=0x" << std::hex << frame->canId << std::dec
|
||||
<< " ext=" << (frame->isExtended ? "yes" : "no")
|
||||
<< " dlc=" << static_cast<unsigned int> (frame->dlc);
|
||||
|
||||
if (frame->hasPgn)
|
||||
std::cout << " pgn=" << frame->pgn;
|
||||
|
||||
if (!frame->comment.empty())
|
||||
std::cout << " comment=\"" << frame->comment << "\"";
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
@@ -54,13 +45,7 @@ static void PrintTree (const TreeNode *node, int indent) {
|
||||
if (signal != nullptr) {
|
||||
std::cout << " start=" << signal->startBit
|
||||
<< " len=" << signal->length
|
||||
<< " unit=" << signal->unit
|
||||
<< " rx=";
|
||||
|
||||
PrintReceivers (signal->receivers);
|
||||
|
||||
if (!signal->comment.empty())
|
||||
std::cout << " comment=\"" << signal->comment << "\"";
|
||||
<< " unit=" << signal->unit;
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
@@ -76,6 +61,28 @@ static void PrintTree (const TreeNode *node, int indent) {
|
||||
PrintTree (node->GetChild (i), indent + 1);
|
||||
}
|
||||
|
||||
static void PrintDecodedFrame (const DecodedFrameValue &decoded) {
|
||||
if (!decoded.valid || (decoded.definition == nullptr)) {
|
||||
std::cout << "No frame definition found.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Decoded frame: " << decoded.definition->name << "\n";
|
||||
|
||||
for (std::size_t index = 0U; index < decoded.signals.size(); ++index) {
|
||||
const DecodedSignalValue &signal = decoded.signals[index];
|
||||
|
||||
if ((signal.definition == nullptr) || !signal.valid)
|
||||
continue;
|
||||
|
||||
std::cout << " " << signal.definition->name
|
||||
<< " raw=" << signal.rawValue
|
||||
<< " physical=" << signal.physicalValue
|
||||
<< " " << signal.definition->unit
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: dbc_demo <file.dbc>\n";
|
||||
@@ -86,10 +93,29 @@ int main (int argc, char *argv[]) {
|
||||
DbcParser parser;
|
||||
DbcDatabase database = parser.ParseFile (argv[1]);
|
||||
|
||||
DbcTreeBuilder builder;
|
||||
std::unique_ptr<TreeNode> root = builder.Build (database);
|
||||
DbcTreeBuilder treeBuilder;
|
||||
std::unique_ptr<TreeNode> root = treeBuilder.Build (database);
|
||||
|
||||
std::cout << "=== Parsed tree ===\n";
|
||||
PrintTree (root.get(), 0);
|
||||
|
||||
DbcDecodeBuilder decodeBuilder;
|
||||
DecodeDatabase decodeDatabase = decodeBuilder.Build (database);
|
||||
|
||||
/*
|
||||
* Example raw frame.
|
||||
* Replace with live CAN frame or trace record later.
|
||||
*/
|
||||
RawCanFrame rawFrame;
|
||||
rawFrame.canId = decodeDatabase.frames.empty() ? 0U : decodeDatabase.frames[0].canId;
|
||||
rawFrame.isExtended = decodeDatabase.frames.empty() ? false : decodeDatabase.frames[0].isExtended;
|
||||
rawFrame.data.resize (8U, 0U);
|
||||
|
||||
DbcDecoder decoder;
|
||||
DecodedFrameValue decoded = decoder.Decode (decodeDatabase, rawFrame);
|
||||
|
||||
std::cout << "\n=== Decoded frame ===\n";
|
||||
PrintDecodedFrame (decoded);
|
||||
} catch (const std::exception &ex) {
|
||||
std::cerr << "Error: " << ex.what() << "\n";
|
||||
return 2;
|
||||
|
||||
Reference in New Issue
Block a user