首页 > 编程知识 正文

c本,c++创建对象

时间:2023-05-05 03:11:32 阅读:172062 作者:1180

最近,需要实现基于Excel的发动机台架自动化测量系统,需要使用dbc通信文件。 特别是这篇文章,提供给需要的人

首先定义存储DBC数据的结构。 首先是信号系统和信息系统的定义。

这个定义必须遵循变量依存关系。 (一个DBC文件中有多个BO_,一个BO_下有多个SG_。 )

请参阅这篇文章。 https://www.vec ll.com/news/10.html是作者提供的思路很清晰,本文中描述的代码就是基于这个思路详细开发的

//信号类public class typedef _ SG { public string name; 公共开始位; 公共长度; 公共代码类型; //0=motorola,1=intel public string DataType; //-表示带符号的公共双因子; 公共双偏移; public double MinValue; 公共双精度最大值; 公共字符串单元; //消息类public class typedef _ bo { publicuintid; 公共字符串名称; 公共单元长度; publiclisttypedef _ sgsg=new list typedef _ SG (; }全局变量的声明和定义

private bool IsEdited=false; public static Dictionarystring,stringdictdbc=newdictionarystring,string(//从信号名称反向查找时,为SG.Name,bo.idbo [ ] SG [ ] //将消息类构建为一个消息列表Typedef_BO BO; 然后定义打开DBC文件的函数,并使用、

private void LoadDBCFile () { dictDBC.Clear ); BO_List.Clear (; dialogresult dialogresult=opendb cfile.showdialog (; if (dialogresult==dialogresult.ok ) stringdbcfilepath=opendb cfile.filename; BottomLabel1.Text=DBCFilePath; Stopwatch sw=new Stopwatch (; sw.Restart (; //逐行读取using (varreader=new streamreader ) DBC文件路径) { int i=0; int SG_index=0; int BO_index=0; //string str_Line=' '; while ((str _ line=reader.readline ) )!=带空数据的行{ i; regexregexsg=new regex (@ ' ^ SG _ [.] * '; //sgregexregexbo=new regex (@ ' ^ bo _ _ d ); //botry{if(regexSG.ismatch ) str_line ) )

//SG length=9 { //MessageBox.Show("in SG_ ,line="+i); string[] SG_Line = str_Line.Split(' '); if (SG_Line.Length != 9) throw new Exception("数据格式错误,Line=" + i); Typedef_SG SG = new Typedef_SG(); SG.Name = SG_Line[2]; //Name string[] SG4 = SG_Line[4].Split(new char[2] { '|', '@' }); //StartBit,Length,CodeType,DataType SG.StartBit = Convert.ToInt16(SG4[0]); SG.Length = Convert.ToInt16(SG4[1]); SG.CodeType = Convert.ToInt16(SG4[2].Substring(0, 1)); SG.DataType = SG4[2].Substring(1, 1); string[] SG5 = SG_Line[5].Split(new char[3] { '(', ',', ')' }); //Factor,Offset SG.Factor = Convert.ToDouble(SG5[1]); SG.Offset = Convert.ToDouble(SG5[2]); string[] SG6 = SG_Line[6].Split(new char[3] { '[', '|', ']' }); //MinValue,MaxValue,Unit SG.MinValue = Convert.ToDouble(SG6[1]); SG.MaxValue = Convert.ToDouble(SG6[2]); SG.Unit = SG_Line[7]; //Unit BO.SG.Add(SG); //SG to List BO.SG dictDBC.Add(SG.Name, BO.ID+ "," + (BO_index - 1) + "," + SG_index); //添加元素key,value同步加入字典,实现根据SG.Name反查出(BO.ID, BO[BO_index], SG[SG_index]) SG_index++; } if (regexBO.IsMatch(str_Line)) //BO length=5 { BO = new Typedef_BO(); //BO实例化 SG_index = 0; string[] BO_Line = str_Line.Split(' '); if (BO_Line.Length != 5) throw new Exception("数据格式错误,Line=" + i); BO.ID = Convert.ToUInt16(BO_Line[1]); BO.Name = BO_Line[2]; BO.Length = Convert.ToUInt16(BO_Line[3]); BO_List.Add(BO); BO_index++; //MessageBox.Show("in BO_ ,line=" + i+ " ,name=" + BO.Name); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } sw.Stop(); } }

根据信号名称查询dbc文件对应行BO_及SG_信息 函数定义如下

public static string dictDBCSearch(String SGname) { string Result = ""; if (!dictDBC.ContainsKey(SGname)) //假如不存在元素 Result = SGname + ",不存在"; else Result = SGname + "的 BO[BO_index] ,SG[SG_index] = " + dictDBC[SGname]; return Result; }

源码链接:https://download.csdn.net/download/qqwertccc/13764355
pudn已传

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。