Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions DbcFiles/cn_test.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
VERSION ""


NS_ :
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_

BS_:

BU_:

BO_ 257 CCU_101: 8 SFAE
SG_ CCU_101_Chks_10_27 : 0|16@1+ (1,0) [0|65535] "" SFAE
SG_ CCU_101_Msg_cnt_30_47 : 16|16@1+ (1,0) [0|65535] "" SFAE
SG_ CCU_101_FL_mot_tar_tq_50_67 : 32|16@1- (0.1,0) [-3276.8|3276.7] "" SFAE
SG_ CCU_101_FR_mot_tar_tq_70_87 : 48|16@1- (0.1,0) [-3276.8|3276.7] "" SFAE

CM_ BO_ 257 "CCU";

CM_ SG_ 257 CCU_101_Chks_10_27 "0x101����";
CM_ SG_ 257 CCU_101_Msg_cnt_30_47 "0x101����֡";
CM_ SG_ 257 CCU_101_FL_mot_tar_tq_50_67 "��ǰ���Ŀ��Ť��";
CM_ SG_ 257 CCU_101_FR_mot_tar_tq_70_87 "��ǰ���Ŀ��Ť��";

BA_ "GenMsgCycleTime" BO_ 257 10;

24 changes: 22 additions & 2 deletions DbcParserLib.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using DbcParserLib.Model;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using NUnit.Framework;
using System.Linq;
using DbcParserLib.Model;
using System.Text;

namespace DbcParserLib.Tests
{
public class ParserTests
{
private const string MainDbcFilePath = @"..\..\..\..\DbcFiles\tesla_can.dbc";
private const string CnDbcFilePath = @"..\..\..\..\DbcFiles\cn_test.dbc";

[Test]
public void SimpleParseFileTest()
Expand All @@ -22,6 +24,24 @@ public void SimpleParseFileTest()
Assert.That(dbc.Nodes.Count(), Is.EqualTo(15));
});
}

[Test]
public void SimpleParseFileWithEncodingTest()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc = System.Text.Encoding.GetEncoding("GB18030");
var dbc = Parser.ParseFromPath(CnDbcFilePath, enc);
foreach (var message in dbc.Messages)
{
foreach (var signal in message.Signals)
{
Assert.That(signal.Comment, Is.EqualTo("0x101����"));
break;
}
break;
}
}

[Test]
public void ParseMessageWithStartBitGreaterThan255Test()
{
Expand Down
19 changes: 18 additions & 1 deletion DbcParserLib/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using DbcParserLib.Parsers;
using DbcParserLib.Observers;
using System.Text;

namespace DbcParserLib
{
Expand Down Expand Up @@ -44,10 +45,26 @@ public static Dbc ParseFromPath(string dbcPath)
return ParseFromStream(fileStream);
}
}

public static Dbc ParseFromPath(string dbcPath, Encoding encoding)
{
using (var fileStream = new FileStream(dbcPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return ParseFromStream(fileStream, encoding);
}
}

public static Dbc ParseFromStream(Stream dbcStream)
{
using(var reader = new StreamReader(dbcStream))
using (var reader = new StreamReader(dbcStream))
{
return ParseFromReader(reader);
}
}

public static Dbc ParseFromStream(Stream dbcStream, Encoding encoding)
{
using (var reader = new StreamReader(dbcStream, encoding))
{
return ParseFromReader(reader);
}
Expand Down
8 changes: 7 additions & 1 deletion Demo/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DbcParserLib;
using DbcParserLib.Observers;
Expand All @@ -27,6 +28,8 @@ public partial class Form1 : Form
public Form1()
{
InitializeComponent();
//Needed to support encodings other than UTF8 (e.g. GB18030), which can be the case for some dbc files, especially those from Chinese manufacturers
//Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}

private void Form1_Load(object sender, EventArgs e)
Expand Down Expand Up @@ -73,8 +76,11 @@ public void LoadDbc(string filePath)
// Comment this line to remove parsing failure management (errors will be silent)
// You can provide your own IParseObserver implementation to customize parsing failure management
Parser.SetParsingFailuresObserver(m_failureObserver);

var dbc = Parser.ParseFromPath(filePath);
// Comment the line above and uncomment the lines below to specify encoding (if needed, default is UTF8)
// var enc = Encoding.GetEncoding("GB18030");
// var dbc = Parser.ParseFromPath(filePath, enc);
ShowErrors();

textBoxPath.Text = filePath;
Expand Down