public bool CheckHexLine(lineofData teststruct) { byte count = 0x00; count += teststruct.numofData; string address = teststruct.address.ToString("X"); while (address.Length < 4) { address = "0" + address; } string upperBofaddress = " "; string lowerBofaddress = " "; upperBofaddress = address.Substring(0, 2); lowerBofaddress = address.Substring(2, 2); // address i hex e çevirmek için count += byte.Parse(upperBofaddress, System.Globalization.NumberStyles.HexNumber); // address i hex e çevirmek için count += byte.Parse(lowerBofaddress, System.Globalization.NumberStyles.HexNumber); count += (byte)teststruct.recordType; for (int i = 0; i < teststruct.dataArray.Length; i++) { count += (byte)teststruct.dataArray[i]; } if (teststruct.checksum == ((0xFF ^ count) + 0x01) % 256) { return(true); } else { return(false); } }
public List <lineofData> ReadHexFile(string path) { if (File.Exists(path)) { Console.WriteLine(" "); } StreamReader sr = new StreamReader(path); List <lineofData> listofdata = new List <lineofData>(); lineofData val = new lineofData(); do { string line = sr.ReadLine(); string temp = " "; val.numofData = Stringtohex(temp = line.Substring(1, 2)); val.address = (UInt16)(256 * Stringtohex(temp = line.Substring(3, 2))); val.address += (UInt16)Stringtohex(temp = line.Substring(5, 2)); temp = line.Substring(7, 2); switch (Stringtohex(temp)) { case 0x00: val.recordType = RecordType.DataRecord; break; case 0x01: val.recordType = RecordType.EOFRecord; break; case 0x02: val.recordType = RecordType.ExtSegAddressRecord; break; case 0x04: val.recordType = RecordType.ExtLinAdressRecord; break; case 0x05: val.recordType = RecordType.StartLinAddressRecord; break; default: break; } val.dataArray = new byte[16]; for (int i = 0; i < val.numofData; i++) { val.dataArray[i] = Stringtohex(temp = line.Substring(9 + 2 * i, 2)); } val.checksum = Stringtohex((temp = line.Substring((9 + val.numofData * 2), 2))); if (CheckHexLine(val)) { Console.WriteLine("TRUE"); } else { Console.WriteLine("ERROR"); } listofdata.Add(val); } while (!(sr.EndOfStream)); return(listofdata); }