使用Qt库解析DBC文件:
结构体:
struct SignalInfo_S
{
QString SignalName;
QString MsgName;
int msgid{};
double scale{};
double offset{};
int startbit{};
int lenth{};
int codetype{};
};
struct MsgInfo_S
{
int MsgID{};
QString MsgName;
QString SendNodeName;
int msgSendType{};
int msgCycleTime{};
};
//解析文件函数
void CCanManager::LoadSignals(QString filename, QList<SignalInfo_S>& inputsiglist, QList<MsgInfo_S>& inputmsglist, \
QList<SignalInfo_S>& outputsiglist, QList<MsgInfo_S>& outputmsglist)
{
#if 1
//打开文件
QFile dbc_file(filename);
if (!dbc_file.open(QIODevice::ReadOnly))
{
return;
}
//每行读取数据
QString linestr;
int linecounter = 0;
//记录当前解析msg信息
MsgInfo_S msginfo = { 0 };
bool isoutput = false;
while (!dbc_file.atEnd())
{
linestr = dbc_file.readLine();
bool ok;
QString msgname;
int msgid;
if (linestr.startsWith("BO_"))
{
QStringList linelist = linestr.split(" ");
msginfo.MsgID = (linelist[1].toLongLong(&ok)) & 0x7FFFFFFF;
msginfo.MsgName = linelist[2].remove(":");
msginfo.SendNodeName = linelist[4].remove(":").trimmed();
QString strlast = linelist.last();
if (strlast.contains(NODENAME))
{
outputmsglist.append(msginfo);
isoutput = true;
}
else
{
isoutput = false;
}
}
else if (linestr.startsWith(" SG_ "))
{
QStringList linelist = linestr.trimmed().split(" ");
bool contain = false;
for (int i = 0; i < inputmsglist.count(); i++)
{
if (inputmsglist[i].MsgID == msginfo.MsgID && \
inputmsglist[i].SendNodeName == msginfo.SendNodeName && \
inputmsglist[i].MsgName == msginfo.MsgName)
{
contain = true;
break;
}
}
for (int i = 0; i < outputmsglist.count(); i++)
{
if (outputmsglist[i].MsgID == msginfo.MsgID && \
outputmsglist[i].SendNodeName == msginfo.SendNodeName && \
outputmsglist[i].MsgName == msginfo.MsgName)
{
contain = true;
break;
}
}
if ((linelist.last().contains(NODENAME)|| linelist.last().contains("Vector__XXX")) && (!contain))
{
inputmsglist.append(msginfo);
}
SignalInfo_S siginfo;
QStringList offsesacle = linelist[4].remove("(").remove(")").split(",");
siginfo.scale = offsesacle[0].toDouble();
siginfo.offset = offsesacle[1].toDouble();
QStringList sblenmi = linelist[3].replace("|", ".").replace("@", ".").remove("+").split(".");
siginfo.startbit = sblenmi[0].toInt();
siginfo.lenth = sblenmi[1].toInt();
siginfo.codetype = sblenmi[2].toInt();
siginfo.msgid = msginfo.MsgID;
siginfo.MsgName = msginfo.MsgName;
siginfo.SignalName = linelist[1];
if (isoutput)
{
outputsiglist.append(siginfo);
}
else
{
if (linelist.last().contains(NODENAME) || linelist.last().contains("Vector__XXX"))
inputsiglist.append(siginfo);
}
}
else if (linestr.startsWith("BA_ "))
{
QStringList linelist = linestr.split(" ");
if (linestr.contains("\"GenMsgSendType\""))
{
int msgid = (linelist[3].toLongLong(&ok, 10)) & 0x7FFFFFFF;
for (int i = 0; i < inputmsglist.count(); i++)
{
if (inputmsglist[i].MsgID == msgid)
{
inputmsglist[i].msgSendType = linelist[4].remove(";").trimmed().toInt();
break;
}
}
}
else if (linestr.contains("\"GenMsgCycleTime\""))
{
int msgid = (linelist[3].toLongLong(&ok, 10)) & 0x7FFFFFFF;
for (int i = 0; i < inputmsglist.count(); i++)
{
if (inputmsglist[i].MsgID == msgid)
{
inputmsglist[i].msgCycleTime = linelist[4].remove(";").trimmed().toInt();
break;
}
}
}
}
}