private void WriteHex(object sender, DoWorkEventArgs args) { List <MemoryRegion> regions = (List <MemoryRegion>)(((object[])(args.Argument))[0]); string path = (string)(((object[])(args.Argument))[1]); args.Result = args.Argument; StreamWriter file = new StreamWriter(path); try { foreach (MemoryRegion region in regions) { ushort lowAddr = (ushort)(region.Address & 0x0000FFFF); ushort highAddr = (ushort)(region.Address >> 16); if (highAddr > 0) { String temp = "02000004" + HexStringConverter.UInt16ToHS4(highAddr); temp = ":" + temp + HexStringConverter.ByteToHS2(CalculateCheckSum(temp)); file.WriteLine(temp); } uint count = 16; uint addr = region.Address; while ((count == 16) && (addr < (region.Address + region.Size))) { byte[] data = new byte[16]; count = region.ReadData(addr, data); string temp = ""; for (int i = 0; i < count; i++) { temp = temp + HexStringConverter.ByteToHS2(data[i]); } temp = HexStringConverter.ByteToHS2((byte)count) + HexStringConverter.UInt16ToHS4(lowAddr) + "00" + temp; temp = ":" + temp + HexStringConverter.ByteToHS2(CalculateCheckSum(temp)); file.WriteLine(temp); addr += 16; lowAddr += 16; } } file.WriteLine(":00000001FF"); } finally { file.Close(); } args.Result = args.Argument; }
public void Write(string path, List <MemoryRegion> regions, Action <FileWorkerIOCompleteInfo> complete) { AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new Guid()); IDictionary <string, Action> writers = new Dictionary <string, Action>(); FileWorkerIOCompleteInfo completeInfo = new FileWorkerIOCompleteInfo(); completeInfo.Path = path; writers.Add("hex", #region hexdelegate delegate() { using (StreamWriter file = new StreamWriter(path)) { foreach (MemoryRegion region in regions) { ushort lowAddr = (ushort)(region.Address & 0x0000FFFF); ushort highAddr = (ushort)(region.Address >> 16); if (highAddr > 0) { String temp = "02000004" + HexStringConverter.UInt16ToHS4(highAddr); temp = ":" + temp + HexStringConverter.ByteToHS2(CalculateCheckSum(temp)); file.WriteLine(temp); } uint count = 16; uint addr = region.Address; while ((count == 16) && (addr < (region.Address + region.Size))) { byte[] data = new byte[16]; count = (uint)region.ReadData(addr, data); string temp = ""; for (int i = 0; i < count; i++) { temp = temp + HexStringConverter.ByteToHS2(data[i]); } temp = HexStringConverter.ByteToHS2((byte)count) + HexStringConverter.UInt16ToHS4(lowAddr) + "00" + temp; temp = ":" + temp + HexStringConverter.ByteToHS2(CalculateCheckSum(temp)); file.WriteLine(temp); addr += 16; lowAddr += 16; } } file.WriteLine(":00000001FF"); } } #endregion ); writers.Add("bin", #region bindelegate delegate() { using (BinaryWriter file = new BinaryWriter(File.OpenWrite(path))) { file.Write(regions[0].Data); } } #endregion ); ThreadStart start = delegate() { try { writers[path.Substring(path.Length - 3, 3).ToLower()](); completeInfo.Info = "Write complete"; } catch (KeyNotFoundException) { completeInfo.Error = new Exception("Not supported file type"); } catch (Exception e) { completeInfo.Error = e; } asyncOp.PostOperationCompleted(delegate(object arg) { complete(completeInfo); }, null); }; (new Thread(start)).Start(); }