public int readFile(string filePath) { int indexLine = 0; List <S19Line> s19LineTmp = new List <S19Line>(); strLineTmp = new List <string>(); string tmp; using (FileStream fs = File.OpenRead(filePath)) { StreamReader sr = new StreamReader(fs, Encoding.Default); while (!sr.EndOfStream) { strLineTmp.Add(sr.ReadLine()); } } for (int i = 0; i < strLineTmp.Count; i++) { byte checkSum = 0; tmp = strLineTmp[i].Substring(indexLine, 2); indexLine += 2; if (tmp.Equals("S0") || tmp.Equals("S7") || tmp.Equals("S8") || tmp.Equals("S9")) { strLineTmp.Remove(strLineTmp[i]); i--; } else if (tmp.Equals("S1") || tmp.Equals("S2") || tmp.Equals("S3")) { byte tmpAddressLen = 0; s19LineTmp.Add(new S19Line()); S19Line tmpS19 = s19LineTmp[i]; tmpS19.num = Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16); checkSum += tmpS19.num; switch (tmp) { case "S1": tmpAddressLen = S1AddressLen; break; case "S2": tmpAddressLen = S2AddressLen; break; case "S3": tmpAddressLen = S3AddressLen; break; } tmpS19.num = (byte)(tmpS19.num - tmpAddressLen - 1); indexLine += 2; tmpS19.lineAddress = new byte[4]; tmpS19.lineAddress.Initialize(); for (int a = 0; a < 4; a++) { int indexTmp = 4 - tmpAddressLen + a; if (indexTmp < 4) { tmpS19.lineAddress[indexTmp] = Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16); tmpS19.lineAddressAll += (uint)(tmpS19.lineAddress[indexTmp] * (Math.Pow(0x100, tmpAddressLen - a - 1))); indexLine += 2; checkSum += tmpS19.lineAddress[indexTmp]; } else { break; } } //for (int a = 0; a < tmpAddressLen; a++) //{ // tmpS19.lineAddress[a] = Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16); // tmpS19.lineAddressAll += (uint)(tmpS19.lineAddress[a] * (Math.Pow(0x100, tmpAddressLen - a - 1))); // indexLine += 2; // checkSum += tmpS19.lineAddress[a]; //} tmpS19.date = new byte[tmpS19.num]; for (int j = 0; j < tmpS19.date.Length; j++) { tmpS19.date[j] = Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16); checkSum += tmpS19.date[j]; indexLine += 2; } if ((0xff - checkSum) == Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16)) { tmpS19.sumCheck = (byte)(0xff - checkSum); } else { return(0); } } else { return(-1); } indexLine = 0; } s19Line = s19LineTmp.ToArray(); return(1); }
private void hex() { int indexLine = 0; List <S19Line> s19LineTmp = new List <S19Line>(); strLineTmp = new List <string>(); string tmp; for (int i = 0; i < strLineTmp.Count; i++) { byte checkSum = 0; tmp = strLineTmp[i].Substring(indexLine, 2); indexLine += 2; if (tmp.Equals("S0") || tmp.Equals("S7") || tmp.Equals("S8") || tmp.Equals("S9")) { strLineTmp.Remove(strLineTmp[i]); i--; } if (tmp.Equals("S1") || tmp.Equals("S2") || tmp.Equals("S3")) { byte tmpAddressLen = 0; s19LineTmp.Add(new S19Line()); S19Line tmpS19 = s19LineTmp[i]; tmpS19.num = Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16); checkSum += tmpS19.num; switch (tmp) { case "S1": tmpAddressLen = S1AddressLen; break; case "S2": tmpAddressLen = S2AddressLen; break; case "S3": tmpAddressLen = S3AddressLen; break; } tmpS19.num = (byte)(tmpS19.num - tmpAddressLen - 1); indexLine += 2; tmpS19.lineAddress = new byte[tmpAddressLen]; for (int a = 0; a < tmpAddressLen; a++) { tmpS19.lineAddress[a] = Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16); tmpS19.lineAddressAll += (ushort)(tmpS19.lineAddress[a] * (Math.Pow(0x100, tmpAddressLen - a - 1))); indexLine += 2; checkSum += tmpS19.lineAddress[a]; } tmpS19.date = new byte[tmpS19.num]; for (int j = 0; j < tmpS19.date.Length; j++) { tmpS19.date[j] = Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16); checkSum += tmpS19.date[j]; indexLine += 2; } if ((0xff - checkSum) == Convert.ToByte(strLineTmp[i].Substring(indexLine, 2), 16)) { tmpS19.sumCheck = (byte)(0xff - checkSum); } else { return; } } indexLine = 0; } s19Line = s19LineTmp.ToArray(); }