Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

.NET CAN bus dbc file parser

License

NotificationsYou must be signed in to change notification settings

EFeru/DbcParser

Repository files navigation

Continuous IntegrationGitHub

Probablythe first .NET DBC file parser. Includes packing and unpacking functionality for sending and receiving CAN signals.

Below is a quick preview of the extracted data using aTesla dbc file taken fromcommaai/opendbc project:

Preview


Important

DbcParser currently relies on well formatted dbc files as input. Well formatted means that tags are non splitted on several lines or cumulated into a single line.The only multiline support is for comments.This may change in future, but at the moment this is a requirement.As an example of unsupported stuff:

BO_ 1160 DAS_steeringControl: 4 NEO SG_ DAS_steeringControlType : 23|2@0+  (1,0) [0|0] ""  EPAS
VAL_ 1160 DAS_steeringControlType 1 "ANGLE_CONTROL" 3 "DISABLED" 0 "NONE" 2 "RESERVED" ;

Quickstart

Install the library viaNuget Packages and add at the top of your file:

usingDbcParserLib;usingDbcParserLib.Model;

Parsing

Then to parse a dbc file use the static classParser, using one oth the parsing flavours:

Dbcdbc=Parser.ParseFromPath("C:\\your_dbc_file.dbc");Dbcdbc=Parser.ParseFromStream(File.OpenRead("C:\\your_dbc_file.dbc"));// Or a stream from networkDbcdbc=Parser.Parse("a dbc as string");

HandlingDbc object

TheDbc object contains two collections,Messages andNodes, both areIEnumerable<T> so can be accessed, iterated and queried using standard LINQ.

As an example, take all messages with id > 100 and more than 2 signals:

varfilteredSelection=dbc.Messages.Where(m=>m.ID>100&&m.Signals.Count>2).ToArray();

Parsingerrors management

Fromv1.4.0 parsing errors management has been introduced to inform users about syntax errors occurred during parsing procedure.TheIParseFailureObserver interface provides all methods to handle syntax errors, like:

  • Generic syntax error (eg.;,',, missing)
  • Duplicated object definition (eg. messages with same ID; nodes, signals, custom properties with same name etc..)
  • Object definition missing (eg. custom property is assigned before its declaration)
  • Value consistency (eg. custom property value is out of bound with respect to min and max values defined in the property)

The library comes with two different implementations:

  1. SilentFailureObserver: the default one. It silently swallow errors when parsing
  2. SimpleFailureObserver: simple observer that logs any error. SimpleFailureObserver errors list:
    • Unknown sintax: there is no correspondingTAG1sintax
    • [TAG]Syntax error:syntax error for a specific TAG
    • Duplicatedobject2: the parser found (and ignores) a dulicated object
    • ObjectNot found: an object is declared or referenced before its definition
    • Property valueout of bound: the value assigned to a property is lower/greater with respect to the minimum/maximum value declared in the property definition
    • Property valueout of index: the declared index is an unacceptable value (for properties that support access to the value via index, e.g. enum value)

Errors list can be retrieve throughGetErrorList() method, like in the example below

// Comment this two lines to remove errors parsing management (errors will be silent)// You can provide your own IParseFailureObserver implementation to customize errors parsing managementvarfailureObserver=newSimpleFailureObserver();Parser.SetParsingFailuresObserver(failureObserver);vardbc=Parser.ParseFromPath(filePath);varerrors=failureObserver.GetErrorList();

The user is free to create its own implementation to customize error management.

1TAG:BA_,BA_DEF_,BA_DEF_DEF_,BO_,BU_,CM_,EV_,ENVVAR_DATA_,SIG_VALTYPE_SG_,VAL_,VAL_TABLE_
2Object:Node,Message,Signal,Property,Environment variable,Value table


Packing/Unpacking signals

Simple scenario

To pack and unpack signals you can use static classPackerExample for packing/unpacking a signal:14 bits, Min:-61.92, Max:101.91

Signalsig=newSignal{sig.Length=14,sig.StartBit=2,sig.IsSigned=1,sig.ByteOrder=1,// 0 = Big Endian (Motorola), 1 = Little Endian (Intel)sig.Factor=0.01,sig.Offset=20};// This packs the signal for sendingulongTxMsg=Packer.TxSignalPack(-34.3,sig);// This unpacks a received signal and calculates the corresponding engineering valuedoubleval=Packer.RxSignalUnpack(TxMsg,sig);

Multiple signals can be packed before CAN transmission using:

ulongTxMsg=0;TxMsg|=Packer.TxSignalPack(value1,sig1);TxMsg|=Packer.TxSignalPack(value2,sig2);TxMsg|=Packer.TxSignalPack(value3,sig3);// ...// Send TxMsg on CAN

The user needs to make sure that the signals do not overlap with each other by properly specifying theLength andStartBit.

Multiplexing

A message can contain multiplexed data, i.e. layout can change depending on a multiplexor value. ThePacker class is unaware of multiplexing, so it's up to the user to check that the given message actually contains the signal.As an example, consider the following dbc lines:

BO_ 568 UI_driverAssistRoadSign: 8 GTW SG_ UI_roadSign M : 0|8@1+ (1,0) [0|0] ""  DAS SG_ UI_dummyData m0 : 8|1@1+ (1,0) [0|0] "" Vector__XXX SG_ UI_stopSignStopLineDist m1 : 8|10@1+ (0.25,-8) [-8|247.5] "m" Vector__XXX

SignalUI_dummyData will only be available whenUI_roadSign value is 0 whileUI_stopSignStopLineDist will only be available whenUI_roadSign value is 1.You can access multiplexing information calling

varmultiplexingInfo=signal.MultiplexingInfo();if(multiplexingInfo.Role==MultiplexingRole.Multiplexor){// This is a multiplexor!}elseif(multiplexingInfo.Role==MultiplexingRole.Multiplexed){Console.WriteLine($"This signal is multiplexed and will be available when multiplexor value is{multiplexingInfo.Group}");}

You can also check is a message does contain multiplexed signals by calling the extension method

if(message.IsMultiplexed()){// ...}

Obsolete stuff

Below you can find a list of obsolete stuff that are going to be removed in the future releases.

ClassProperty/MethodObsoleteRemovedReplaced byComment
SignalIsSignedv1.3.0v1.5.0ValueTypeByte property replaced byDbcValueType property which provides
more informations about signal type
SignalValueTablev1.3.0v1.5.0ValueTableMapString property replaced by aIDictionary<int,string> property
SignalToPairs()v1.3.0v1.4.2-Extension method used to convert ValueTable into ValueTableMap
MessageCycleTimev1.4.21.6.0bool CycleTime(out int cycleTime)CycleTime is no more a message property (replaced by an extension method)

Useful references

Contributions

Contributions are appreciated! Feel free to create pull requests to improve this library.

Support

This project is supported by JetBrains for Open Source development

JetBrains Logo (Main) logo


[8]ページ先頭

©2009-2025 Movatter.jp