public void conversionToJson(string filePath) {//将dbc文件转换为JSON文件 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312")); ToXml tx = new ToXml(); tx.cmarr = new List <CanMessageAndSignal>(); CanMessage tm; CanMessageAndSignal cms = new CanMessageAndSignal(); string s; List <CanSignal> cs = new List <CanSignal>(); //信号为ts,与之相对应 int[] signalnum = new int[100];// 每条CAN信息中的CAN Signal的数目 不能够超过99,这个数组熊1开始 while ((s = sr.ReadLine()) != null) { string[] arr = s.Split(' '); int cnt = 0; int snum = 0; // 每条CAN信息中的CAN Signal的数目 if (string.IsNullOrWhiteSpace(s)) { cs = new List <CanSignal>(); continue; } else if (arr.Length == 5) //CAN message { // cs = new List<CanSignal>(); cms = new CanMessageAndSignal(); tm = new CanMessage(); cs = new List <CanSignal>(); cms.canm = tm; cms.cansarr = cs; tx.cmarr.Add(cms); tm.CANmessageTAG = arr[0]; tm.mId = Convert.ToUInt32(arr[1]); int n;//表示“:”在数组中的位置; n = arr[2].IndexOf(':'); tm.messageName = arr[2].Substring(0, n); tm.mSeparator = ":"; tm.DLC = Convert.ToInt32(arr[3]); tm.mNodeName = arr[4]; signalnum[cnt] = snum; // 这个数组从1开始 cnt++; snum = 0; } else { string[] arr1 = s.Trim().Split(' '); CanSignal ts = new CanSignal(); ts.CANsignalTAG = arr1[0]; //Console.Write(arr1[0]); //Console.Write(arr1[1]); ts.signalName = arr1[1]; //Console.Write(arr1[2]); ts.sSeparator = arr1[2]; ts.startToEnd = arr1[3]; ts.AB = arr1[4]; ts.CD = arr1[5]; ts.unit = arr1[6]; ts.sNodeName = arr1[8]; cs.Add(ts); snum++; } } if (!File.Exists(ConversiontoJsonPath)) { string fn = ConversiontoJsonPath; FileStream fs1 = new FileStream(ConversiontoJsonPath, FileMode.Create, FileAccess.Write);//创建写入文件 string tmp; StreamWriter sw1 = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312")); if (tx != null && !string.IsNullOrEmpty(fn)) { tmp = JsonConvert.SerializeObject(tx.cmarr, Newtonsoft.Json.Formatting.Indented); sw1.WriteLine(tmp + "\n"); } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 sw1.Close(); fs1.Close(); } else { string fn = ConversiontoJsonPath; FileStream fs1 = new FileStream(ConversiontoJsonPath, FileMode.Create, FileAccess.Write);//创建写入文件 string tmp; StreamWriter sw1 = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312")); if (tx != null && !string.IsNullOrEmpty(fn)) { tmp = JsonConvert.SerializeObject(tx.cmarr, Newtonsoft.Json.Formatting.Indented); sw1.WriteLine(tmp + "\n"); /* 另一种方法,不过转换成的格式都集中在一行,不太智能 * System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(tx.cmarr.GetType()); * using (MemoryStream ms = new MemoryStream()) * { * serializer.WriteObject(ms, tx.cmarr); * tmp = Encoding.UTF8.GetString(ms.ToArray()); * } * sw1.WriteLine(tmp + "\n");*/ } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 sw1.Close(); fs1.Close(); } }
public void dbcPreprocess(string filePath) { //将dbc文件转换为XML文件 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312")); ToXml tx = new ToXml(); tx.cmarr = new List <CanMessageAndSignal>(); CanMessage tm; CanMessageAndSignal cms = new CanMessageAndSignal(); string s; List <CanSignal> cs = new List <CanSignal>(); //信号为ts,与之相对应 int[] signalnum = new int[100]; // 每条CAN信息中的CAN Signal的数目 不能够超过99,这个数组熊1开始 int snum = 0; // 每条CAN信息中的CAN Signal的数目 int cnt = 0; while ((s = sr.ReadLine()) != null) { string[] arr = s.Split(' '); if (string.IsNullOrWhiteSpace(s)) { cs = new List <CanSignal>(); continue; } else if (arr.Length == 5) //CAN message { // cs = new List<CanSignal>(); cms = new CanMessageAndSignal(); tm = new CanMessage(); cs = new List <CanSignal>(); cms.canm = tm; cms.cansarr = cs; tx.cmarr.Add(cms); tm.CANmessageTAG = arr[0]; tm.mId = Convert.ToUInt32(arr[1]); int n;//表示“:”在数组中的位置; n = arr[2].IndexOf(':'); tm.messageName = arr[2].Substring(0, n); tm.mSeparator = ":"; tm.DLC = Convert.ToInt32(arr[3]); tm.mNodeName = arr[4]; signalnum[cnt] = snum; // 这个数组从1开始 cnt++; snum = 0; } else { string[] arr1 = s.Trim().Split(' '); CanSignal ts = new CanSignal(); ts.CANsignalTAG = arr1[0]; //Console.Write(arr1[0]); //Console.Write(arr1[1]); ts.signalName = arr1[1]; //Console.Write(arr1[2]); ts.sSeparator = arr1[2]; ts.startToEnd = arr1[3]; ts.AB = arr1[4]; ts.CD = arr1[5]; ts.unit = arr1[6]; ts.sNodeName = arr1[8]; cs.Add(ts); snum++; } } signalnum[cnt] = snum; // 这个数组从1开始 /*int cancnt = 0; * for(int i = 0; i < 100; i++) * { * if(signalnum[i] != 0) * { * cancnt++; * } * }*/ if (!File.Exists(ConversionPathEndConvert)) { string fn = ConversionPathEndConvert; FileStream fs1 = new FileStream(ConversionPathEndConvert, FileMode.Create, FileAccess.Write);//创建写入文件 StreamWriter sw = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312")); //StreamWriter sw1 = new StreamWriter(fs1); int pointer = 1; foreach (CanMessageAndSignal cmas in tx.cmarr) { CanMessage canm1 = new CanMessage(); canm1 = cmas.canm; string ss; ss = canm1.messageName + canm1.mSeparator + " " + canm1.mId + " " + canm1.DLC; sw.WriteLine(ss); sw.WriteLine(signalnum[pointer]); sw.WriteLine(); pointer++; foreach (CanSignal cs1 in cmas.cansarr) { string s1; int m = cs1.AB.IndexOf(","); int i = cs1.AB.Length; int n = cs1.CD.IndexOf("|"); int j = cs1.CD.Length; string a; string b; a = cs1.AB; b = cs1.CD; int p = cs1.startToEnd.IndexOf("|"); int q = cs1.startToEnd.IndexOf("@"); int r = cs1.startToEnd.Length; s1 = cs1.signalName + " " + cs1.startToEnd.Substring(0, p) + " " + cs1.startToEnd.Substring(p + 1, q - p - 1) + " " + cs1.startToEnd.Substring(q + 1, r - q - 2) + " " + a.Substring(1, m - 1) + " " + a.Substring(m + 1, i + -m - 2) + " " + b.Substring(1, n - 1) + " " + b.Substring(n + 1, j - n - 2) + " " + cs1.unit; sw.WriteLine(s1); } sw.WriteLine(); } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 sw.Close(); fs1.Close(); } else { string fn = ConversionPathEndConvert; FileStream fs1 = new FileStream(ConversionPathEndConvert, FileMode.Create, FileAccess.Write);//创建写入文件 StreamWriter sw = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312")); //StreamWriter sw1 = new StreamWriter(fs1); int pointer = 1; foreach (CanMessageAndSignal cmas in tx.cmarr) { CanMessage canm1 = new CanMessage(); canm1 = cmas.canm; string ss; ss = canm1.messageName + canm1.mSeparator + " " + canm1.mId + " " + canm1.DLC; sw.WriteLine(ss); sw.WriteLine(signalnum[pointer]); sw.WriteLine(); pointer++; foreach (CanSignal cs1 in cmas.cansarr) { string s1; int m = cs1.AB.IndexOf(","); int i = cs1.AB.Length; int n = cs1.CD.IndexOf("|"); int j = cs1.CD.Length; string a; string b; a = cs1.AB; b = cs1.CD; int p = cs1.startToEnd.IndexOf("|"); int q = cs1.startToEnd.IndexOf("@"); int r = cs1.startToEnd.Length; s1 = cs1.signalName + " " + cs1.startToEnd.Substring(0, p) + " " + cs1.startToEnd.Substring(p + 1, q - p - 1) + " " + cs1.startToEnd.Substring(q + 1, r - q - 2) + " " + a.Substring(1, m - 1) + " " + a.Substring(m + 1, i + -m - 2) + " " + b.Substring(1, n - 1) + " " + b.Substring(n + 1, j - n - 2) + " " + cs1.unit; sw.WriteLine(s1); } sw.WriteLine(); } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 sw.Close(); fs1.Close(); } }
public void conversionToXml(string fileName) { //将dbc文件转换为XML文件 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312")); ToXml tx = new ToXml(); tx.cmarr = new List <CanMessageAndSignal>(); CanMessage tm; CanMessageAndSignal cms = new CanMessageAndSignal(); string s; List <CanSignal> cs = new List <CanSignal>(); //信号为ts,与之相对应 int[] signalnum = new int [100]; // 每条CAN信息中的CAN Signal的数目 不能够超过99,这个数组熊1开始 int snum = 0; // 每条CAN信息中的CAN Signal的数目 int cnt = 0; while ((s = sr.ReadLine()) != null) { string[] arr = s.Split(' '); if (string.IsNullOrWhiteSpace(s)) { cs = new List <CanSignal>(); continue; } else if (arr.Length == 5) //CAN message { // cs = new List<CanSignal>(); cms = new CanMessageAndSignal(); tm = new CanMessage(); cs = new List <CanSignal>(); cms.canm = tm; cms.cansarr = cs; tx.cmarr.Add(cms); tm.CANmessageTAG = arr[0]; tm.mId = Convert.ToUInt32(arr[1]); int n;//表示“:”在数组中的位置; n = arr[2].IndexOf(':'); tm.messageName = arr[2].Substring(0, n); tm.mSeparator = ":"; tm.DLC = Convert.ToInt32(arr[3]); tm.mNodeName = arr[4]; signalnum[cnt] = snum; // 这个数组从1开始 cnt++; snum = 0; } else { string[] arr1 = s.Trim().Split(' '); CanSignal ts = new CanSignal(); ts.CANsignalTAG = arr1[0]; //Console.Write(arr1[0]); //Console.Write(arr1[1]); ts.signalName = arr1[1]; //Console.Write(arr1[2]); ts.sSeparator = arr1[2]; ts.startToEnd = arr1[3]; ts.AB = arr1[4]; ts.CD = arr1[5]; ts.unit = arr1[6]; ts.sNodeName = arr1[8]; cs.Add(ts); snum++; } } signalnum[cnt] = snum; // 这个数组从1开始 if (!File.Exists(ConversionPath)) { string fn = ConversionPath; FileStream fs1 = new FileStream(ConversionPath, FileMode.Create, FileAccess.Write);//创建写入文件 //StreamWriter sw1 = new StreamWriter(fs1); if (tx != null && !string.IsNullOrEmpty(fn)) { using (StreamWriter sw1 = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312"))) { XmlSerializer xs = string.IsNullOrEmpty(null) ? new XmlSerializer(tx.GetType()) : new XmlSerializer(tx.GetType(), new XmlRootAttribute(null)); xs.Serialize(sw1, tx); sw1.Close(); } } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 fs1.Close(); } else { string fn = ConversionPath; FileStream fs1 = new FileStream(ConversionPath, FileMode.Create, FileAccess.Write);//创建写入文件 //StreamWriter sw1 = new StreamWriter(fs1); if (tx != null && !string.IsNullOrEmpty(fn)) { using (StreamWriter sw1 = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312"))) { XmlSerializer xs = string.IsNullOrEmpty(null) ? new XmlSerializer(tx.GetType()) : new XmlSerializer(tx.GetType(), new XmlRootAttribute(null)); xs.Serialize(sw1, tx); sw1.Close(); } } //sr.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 fs1.Close(); } //int a = signalnum[1]; }