From 1fa30f76fbc87fb4a2f5ccb2e6c1d8f00457ce34 Mon Sep 17 00:00:00 2001 From: Cheung Date: Tue, 24 Feb 2026 11:28:01 +0800 Subject: [PATCH] add a new overloading function that can select the encoding function and create simple unit test --- DbcFiles/cn_test.dbc | 52 +++++++++++++++++++++++++++++++ DbcParserLib.Tests/ParserTests.cs | 24 ++++++++++++-- DbcParserLib/Parser.cs | 19 ++++++++++- Demo/Form1.cs | 8 ++++- 4 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 DbcFiles/cn_test.dbc diff --git a/DbcFiles/cn_test.dbc b/DbcFiles/cn_test.dbc new file mode 100644 index 0000000..048f256 --- /dev/null +++ b/DbcFiles/cn_test.dbc @@ -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; + diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 8a3b188..3548f92 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -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() @@ -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() { diff --git a/DbcParserLib/Parser.cs b/DbcParserLib/Parser.cs index 482dca6..d8a8611 100644 --- a/DbcParserLib/Parser.cs +++ b/DbcParserLib/Parser.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using DbcParserLib.Parsers; using DbcParserLib.Observers; +using System.Text; namespace DbcParserLib { @@ -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); } diff --git a/Demo/Form1.cs b/Demo/Form1.cs index db8e689..da45493 100644 --- a/Demo/Form1.cs +++ b/Demo/Form1.cs @@ -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; @@ -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) @@ -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;