public static IntelHexFile Create(Byte[] data, int ByteCount) { IntelHexFile file = new IntelHexFile(); IntelHexFileLine current; for (int i = 0; ; ) { current = new IntelHexFileLine(); if (i == data.Length) { goto EndOfFile; } else if ((data.Length - i) < ByteCount) /* Last Record */ { current.ByteCount = (Byte)(data.Length - i); current.Address = (UInt16)i; current.Data = new Byte[data.Length - i]; Buffer.BlockCopy(data, i, current.Data, 0, data.Length - i); current.CheckSum = (Byte)TwosCompliment(current); file.AddLine(current); goto EndOfFile; } else { current.ByteCount = (Byte)ByteCount; current.Address = (UInt16)i; current.Data = new Byte[ByteCount]; Buffer.BlockCopy(data, i, current.Data, 0, ByteCount); current.CheckSum = (Byte)TwosCompliment(current); file.AddLine(current); } i += ByteCount; } EndOfFile: /* End of hex file */ current = new IntelHexFileLine(); current.ByteCount = 0; current.Address = 0; current.RecordType = RecordType.EndOfFile; current.Data = new Byte[0]; current.CheckSum = (Byte)TwosCompliment(current); file.AddLine(current); return file; }
private void VerifyChecksum(IntelHexFileLine line) { if (TwosCompliment(line) != line.CheckSum) throw new ChecksumMismatchException("Checksum mismatch!"); }
private static int TwosCompliment(IntelHexFileLine line) { int sum = 0; sum += line.ByteCount; sum += (line.Address & 0xff) + ((line.Address >> 8) & 0xff); sum += (Byte)line.RecordType; for (int i = 0; i < line.Data.Length; i++) { sum += line.Data[i]; } int twos_comp = (~sum + 1) & 0xff; return twos_comp; }
private IntelHexFileLine ParseLine(MemoryStream fp) { IntelHexFileLine line = new IntelHexFileLine(); fp.Seek(1, SeekOrigin.Current); /* Read Start */ line.ByteCount = (Byte)ReadBytes(fp, 2); line.Address = (UInt16)ReadBytes(fp, 4); line.RecordType = (RecordType)ReadBytes(fp, 2); line.Data = new Byte[line.ByteCount]; for (int i = 0; i < line.ByteCount; i++) line.Data[i] = (Byte)ReadBytes(fp, 2); line.CheckSum = (Byte)ReadBytes(fp, 2); fp.Seek(2, SeekOrigin.Current); /* Read End */ VerifyChecksum(line); return line; }
public void AddLine(IntelHexFileLine line) { _Lines.Add(line); address = line.Address; for (int i = 0; i < line.Data.Length; i++) { dataBuffer[address] = line.Data[i]; address++; } }