/// <summary> /// Add a new signal to the last node of the DBC node collection /// </summary> /// <param name="NewSignal">DBC signal to be added</param> private void AddSignalToLastNode(DBCSignal NewSignal) { if (Frames.Count > 0) { Frames[Frames.Count - 1].Signals.Add(NewSignal); } }
/// <summary> /// DBC file reading function /// </summary> /// <param name="FilePath">File path of the DBC</param> /// <returns>Reading result (True: OK; False: Error)</returns> public bool ReadDBC(string FilePath) { if (!(FilePath.Equals(""))) { StreamReader SR = new StreamReader(FilePath); while (!SR.EndOfStream) { string line = SR.ReadLine(); if (line.Length > 4 & !(line.StartsWith("\t"))) { line = line.TrimStart(Char.Parse(" ")); int iEntrySep = line.IndexOf(" "); string EntryId = line.Substring(0, iEntrySep); string EntryData = line.Substring(iEntrySep + 1, line.Length - (iEntrySep + 1)); if (EntryId.Equals("VERSION")) //DBC Version { Version = EntryData.Substring(1, EntryData.Length - 2); //Suppression of " } else if (EntryId.Equals("BU_:")) //Bus components { BusComponents = EntryData.Split(Char.Parse(" ")); } else if (EntryId.Equals("BO_")) //Node { DBCFrame oNode = new DBCFrame(); string[] NodeData = EntryData.Split(Char.Parse(" ")); //oNode.Identifier=Int16.Parse(NodeData[0]); if (int.TryParse(NodeData[0], out oNode.Identifier)) { oNode.Name = NodeData[1].Substring(0, NodeData[1].Length - 1); //Suppression of : oNode.ByteLength = Int16.Parse(NodeData[2]); oNode.Transmitter = NodeData[3]; Frames.Add(oNode); } } else if (EntryId.Equals("SG_")) //Signal { DBCSignal oSignal = new DBCSignal(); EntryData = FilterDoubleSpace(EntryData); string[] SignalData = EntryData.Split(Char.Parse(" ")); int PtOffset = 0; string strTmp; string[] strArray; int i; //Name oSignal.Name = SignalData[0]; //Multiplexer if (SignalData.Length > 7) { PtOffset = 1; oSignal.Multiplexer = new DBCMultiplexer(); if (SignalData[1].Equals("M")) { oSignal.Multiplexer.IsMultiplexerSignal = true; } else { oSignal.Multiplexer.MultiplexerSignal = GetMultiplexerSignalInLastNode(); oSignal.Multiplexer.MultiplexerValue = Int16.Parse(SignalData[1].Substring(1, SignalData[1].Length - 1)); } } //Start bit, length, endianess, signedness i = SignalData[2 + PtOffset].IndexOf("@"); strTmp = SignalData[2 + PtOffset].Substring(0, i); strArray = strTmp.Split(Char.Parse("|")); oSignal.StartBit = Int16.Parse(strArray[0]); oSignal.Length = Int16.Parse(strArray[1]); strTmp = SignalData[2 + PtOffset].Substring(i + 1, 2); if (strTmp.Substring(0, 1).Equals("0")) //Big Endian (MSB) { oSignal.Endianess = DBCEndianess.MSBFirst; } else //Little endian (LSB) { oSignal.Endianess = DBCEndianess.LSBFirst; } if (strTmp.Substring(1, 1).Equals("-")) { oSignal.Signed = true; } else { oSignal.Signed = false; } //Linearization gain and offset strTmp = SignalData[3 + PtOffset].Substring(1, SignalData[3 + PtOffset].Length - 2); strArray = strTmp.Split(Char.Parse(",")); oSignal.Gain = Double.Parse(strArray[0]); oSignal.Zero = Double.Parse(strArray[1]); //Limit values strTmp = SignalData[4 + PtOffset].Substring(1, SignalData[4 + PtOffset].Length - 2); strArray = strTmp.Split(Char.Parse("|")); oSignal.LimitMin = Double.Parse(strArray[0]); oSignal.LimitMax = Double.Parse(strArray[1]); //Unit strTmp = SignalData[5 + PtOffset].Substring(1, SignalData[5 + PtOffset].Length - 2); oSignal.Unit = strTmp; //Transmitter oSignal.Destination = SignalData[6 + PtOffset]; AddSignalToLastNode(oSignal); } else if (EntryId.Equals("CM_")) //Comment { DBCComment oComment = new DBCComment(); int i = EntryData.IndexOf("\"", 0); oComment.Comment = EntryData.Substring(i + 1, EntryData.Length - (i + 3)); string sTmp = EntryData.Substring(0, i); string[] CommentData = sTmp.Split(Char.Parse(" ")); if (int.TryParse(CommentData[1], out oComment.NodeIdentifier)) { if (CommentData[0].Equals("BO_")) { oComment.Object = DBCCommentObject.NodeComment; //oComment.NodeIdentifier=Int16.Parse(CommentData[1]); oComment.SignalName = ""; } else if (CommentData[0].Equals("SG_")) { oComment.Object = DBCCommentObject.SignalComment; //oComment.NodeIdentifier=Int16.Parse(CommentData[1]); oComment.SignalName = CommentData[2]; } else { oComment.Object = DBCCommentObject.Unknown; } } else { oComment = null; } if (!(oComment == null)) { SetCommentToObject(oComment); } } else if (EntryId.Equals("VAL_")) //Bit map values { DBCSignalBitMap oBitMap = new DBCSignalBitMap(); int j = 0; int i = EntryData.IndexOf(" "); oBitMap.NodeIdentifier = Int16.Parse(EntryData.Substring(0, i)); j = EntryData.IndexOf(" ", i + 1); oBitMap.SignalName = EntryData.Substring(i + 1, j - i); while (j < EntryData.Length - 2) { DBCValue oBit = new DBCValue(); i = EntryData.IndexOf("\"", j); string sTmp1 = EntryData.Substring(j + 1, i - j - 1).Trim(); oBit.BitValue = Int64.Parse(sTmp1); j = i + 1; i = EntryData.IndexOf("\"", j); string sTmp2 = EntryData.Substring(j, i - j); oBit.BitDescription = sTmp2; j = i + 1; } SetBitMapToSignal(oBitMap); } } } SR.Close(); SR = null; return(true); } return(false); }