private void M24CXXWrite(string subtype, string name, string serialnumtype, string templatefile) { if ((_programmer == null) || !(_programmer is IM24CXXProgrammer)) { SetAlarmText("Connect the compatible resetter"); return; } IM24CXXProgrammer prog = _programmer as IM24CXXProgrammer; if (prog.IsBusy) { SetInfoText("Resetter is busy"); return; } this.FormClosing += AutoForm_FormClosing; thisBusy = true; MemoryRegion region = null; Action <ProgrammingCompleteInfo> programmComplete = delegate(ProgrammingCompleteInfo pcInfo) { progressBar.Value = progressBar.Minimum; if (pcInfo.error != null) { if (pcInfo.error is VerificationException) { VerificationException ve = pcInfo.error as VerificationException; SetAlarmText( "Verification error At address: " + ve.ErrorAddress.ToString() + " write: " + ve.WrittenByte.ToString() + " read: " + ve.ReadByte.ToString()); } else { SetAlarmText(pcInfo.error.Message); } thisBusy = false; this.FormClosing -= AutoForm_FormClosing; } else if (serialnumtype == "No") { SetOkText(pcInfo.Message); thisBusy = false; this.FormClosing -= AutoForm_FormClosing; } else { Action <FileWorkerIOCompleteInfo> writeComplete = delegate(FileWorkerIOCompleteInfo fwiocInfo) { if (fwiocInfo.Error != null) { SetAlarmText(fwiocInfo.Error.Message); } else { SetOkText(pcInfo.Message); } thisBusy = false; this.FormClosing -= AutoForm_FormClosing; }; _dumpCoprrectors[serialnumtype](region.Data); _fileWorker.Write(templatefile, new List <MemoryRegion>() { region }, writeComplete); } }; Action <FileWorkerIOCompleteInfo> readFileComplete = delegate(FileWorkerIOCompleteInfo fwiocInfo) { if (fwiocInfo.Error != null) { SetAlarmText(fwiocInfo.Error.Message); thisBusy = false; this.FormClosing -= AutoForm_FormClosing; } else { if (prog.IsBusy) { SetInfoText("Resetter is busy"); thisBusy = false; this.FormClosing -= AutoForm_FormClosing; return; } region = fwiocInfo.Regions[0]; prog.ProgramChip(subtype, fwiocInfo.Regions[0], programmComplete, Progress); } }; MemoryRegionInfo regionInfo = new MemoryRegionInfo() { Address = 0, Size = 0, Type = 1 }; regionInfo.Size = (uint)s02512[subtype]; _fileWorker.Read(templatefile, new List <MemoryRegionInfo>() { regionInfo }, readFileComplete); }
public int ReadRegion(string path, MemoryRegion region, Action <int, int> progress) { SafeFileHandle asyncWriteHandle = CreateFile(path, Win32HardwareIOSupport.GENERIC_WRITE | Win32HardwareIOSupport.GENERIC_READ, Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, Win32HardwareIOSupport.FILE_FLAG_OVERLAPPED | Win32HardwareIOSupport.FILE_FLAG_NO_BUFFERING, IntPtr.Zero); if (asyncWriteHandle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } SafeFileHandle asyncReadHandle = CreateFile(path, Win32HardwareIOSupport.GENERIC_READ | Win32HardwareIOSupport.GENERIC_WRITE, Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, Win32HardwareIOSupport.FILE_FLAG_OVERLAPPED | Win32HardwareIOSupport.FILE_FLAG_NO_BUFFERING, IntPtr.Zero); if (asyncReadHandle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } int currentPacketLength = 0; uint trBytes; int pendingPackets = 0; int noRequestedBytes = (int)region.Size; int noReadedBytes = (int)region.Size; uint currentAddress = region.Address; byte lastError = 0; EventWaitHandle hWrite1Event = new EventWaitHandle(false, EventResetMode.AutoReset); EventWaitHandle hWrite2Event = new EventWaitHandle(false, EventResetMode.AutoReset); EventWaitHandle hReadEvent = new EventWaitHandle(false, EventResetMode.AutoReset); NativeOverlapped write1Over = new NativeOverlapped(); write1Over.OffsetHigh = 0; write1Over.OffsetLow = 0; write1Over.EventHandle = hWrite1Event.SafeWaitHandle.DangerousGetHandle(); NativeOverlapped write2Over = new NativeOverlapped(); write2Over.OffsetHigh = 0; write2Over.OffsetLow = 0; write2Over.EventHandle = hWrite2Event.SafeWaitHandle.DangerousGetHandle(); NativeOverlapped readOver = new NativeOverlapped(); readOver.OffsetHigh = 0; readOver.OffsetLow = 0; readOver.EventHandle = hReadEvent.SafeWaitHandle.DangerousGetHandle(); MemSafeHandle pWrite1 = new MemSafeHandle(Marshal.AllocHGlobal(65)); MemSafeHandle pWrite2 = new MemSafeHandle(Marshal.AllocHGlobal(65)); MemSafeHandle pRead = new MemSafeHandle(Marshal.AllocHGlobal(65)); currentPacketLength = (int)_bytesPerPacket < noRequestedBytes ? (int)_bytesPerPacket : noRequestedBytes; ArrayToMem(GetDataReq.getCommandPacket(currentAddress, (byte)currentPacketLength), pWrite1); if (!WriteFile(asyncWriteHandle, pWrite1.DangerousGetHandle(), 65, out trBytes, ref write1Over)) { if (Marshal.GetLastWin32Error() != Win32HardwareIOSupport.ERROR_IO_PENDING) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } else { hWrite1Event.Set(); } pendingPackets++; noRequestedBytes -= currentPacketLength; currentAddress += (uint)currentPacketLength; if (noRequestedBytes > 0) { currentPacketLength = (int)_bytesPerPacket < noRequestedBytes ? (int)_bytesPerPacket : noRequestedBytes; ArrayToMem(GetDataReq.getCommandPacket(currentAddress, (byte)currentPacketLength), pWrite2); if (!WriteFile(asyncWriteHandle, pWrite2.DangerousGetHandle(), 65, out trBytes, ref write2Over)) { if (Marshal.GetLastWin32Error() != Win32HardwareIOSupport.ERROR_IO_PENDING) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } else { hWrite2Event.Set(); } pendingPackets++; noRequestedBytes -= currentPacketLength; currentAddress += (uint)currentPacketLength; } byte[] rawdata = new byte[65]; if (!ReadFile(asyncReadHandle, pRead.DangerousGetHandle(), 65, out trBytes, ref readOver)) { if (Marshal.GetLastWin32Error() != Win32HardwareIOSupport.ERROR_IO_PENDING) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } else { hReadEvent.Set(); } while (noReadedBytes > 0) { if (WaitHandle.WaitAny(new WaitHandle[] { hWrite1Event, hWrite2Event, hReadEvent }, 10000) == WaitHandle.WaitTimeout) { throw new Exception("Hardware doesn't answer. Read time out. no readed bytes:" + noReadedBytes.ToString() + " pending packets:" + pendingPackets.ToString() + " no requested bytes:" + noRequestedBytes.ToString() + " current addr:" + currentAddress.ToString()); } if ((write1Over.InternalLow.ToInt32() != 0x103) && (pendingPackets < 10) && (noRequestedBytes > 0)) { write1Over.OffsetHigh = 0; write1Over.OffsetLow = 0; write1Over.InternalLow = IntPtr.Zero; write1Over.InternalHigh = IntPtr.Zero; currentPacketLength = (int)_bytesPerPacket < noRequestedBytes ? (int)_bytesPerPacket : noRequestedBytes; ArrayToMem(GetDataReq.getCommandPacket(currentAddress, (byte)currentPacketLength), pWrite1); if (!WriteFile(asyncWriteHandle, pWrite1.DangerousGetHandle(), 65, out trBytes, ref write1Over)) { if (Marshal.GetLastWin32Error() != Win32HardwareIOSupport.ERROR_IO_PENDING) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } else { hWrite1Event.Set(); } pendingPackets++; noRequestedBytes -= currentPacketLength; currentAddress += (uint)currentPacketLength; } if ((write2Over.InternalLow.ToInt32() != 0x103) && (pendingPackets < 10) && (noRequestedBytes > 0)) { write2Over.OffsetHigh = 0; write2Over.OffsetLow = 0; write2Over.InternalLow = IntPtr.Zero; write2Over.InternalHigh = IntPtr.Zero; currentPacketLength = (int)_bytesPerPacket < noRequestedBytes ? (int)_bytesPerPacket : noRequestedBytes; ArrayToMem(GetDataReq.getCommandPacket(currentAddress, (byte)currentPacketLength), pWrite2); if (!WriteFile(asyncWriteHandle, pWrite2.DangerousGetHandle(), 65, out trBytes, ref write2Over)) { if (Marshal.GetLastWin32Error() != Win32HardwareIOSupport.ERROR_IO_PENDING) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } else { hWrite2Event.Set(); } pendingPackets++; noRequestedBytes -= currentPacketLength; currentAddress += (uint)currentPacketLength; } if ((readOver.InternalLow.ToInt32() != 0x103)) { MemToArray(pRead, rawdata); progress((int)(region.Size - noReadedBytes), (int)region.Size); if (pendingPackets > 0) { readOver.OffsetHigh = 0; readOver.OffsetLow = 0; readOver.InternalLow = IntPtr.Zero; readOver.InternalHigh = IntPtr.Zero; if (!ReadFile(asyncReadHandle, pRead.DangerousGetHandle(), 65, out trBytes, ref readOver)) { if (Marshal.GetLastWin32Error() != Win32HardwareIOSupport.ERROR_IO_PENDING) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } else { hReadEvent.Set(); } } } } asyncWriteHandle.Close(); asyncReadHandle.Close(); if (lastError != 0) { string error = null; try { } catch (KeyNotFoundException) { error = "Unknown hardware error: " + lastError.ToString(); } throw new Exception(error); } return(0); }
public override void ReadEEPROM(Action <ProgrammingCompleteInfo, MemoryRegion> completed, Action <ProgrammingProgressInfo> progress) { MemoryRegionInfo regionInfo = new MemoryRegionInfo() { Address = 0xF00000, Size = 256, Type = 2 }; AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new Guid()); Action <int, int> progressAct = delegate(int val, int max) { asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(new ProgressBarData(0, val, max))); }, null); }; ThreadStart start = delegate() { asyncOp.Post(delegate(object args) { OnBusy(); }, null); MemoryRegion region = null; ProgrammingCompleteInfo pcInfo = new ProgrammingCompleteInfo(); try { SafeFileHandle handle = CreateFile(_hardwarePath, Win32HardwareIOSupport.GENERIC_WRITE | Win32HardwareIOSupport.GENERIC_READ, Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, 0, IntPtr.Zero); if (handle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } using (FileStream harwareStream = new FileStream(handle, FileAccess.ReadWrite, 65)) { UpdateCurrentMemRegConfig(harwareStream); if (_currentMemoryRegionsConfig.Count != 4) { Exception e1 = Marshal.GetExceptionForHR((int)((Convert.ToUInt32(4319) | 0x80070000))); Exception e2 = Marshal.GetExceptionForHR((int)((Convert.ToUInt32(1784) | 0x80070000))); string message = e1.Message.Substring(0, e1.Message.IndexOf("(")) + e2.Message.Substring(0, e2.Message.IndexOf("(")); throw new Exception(message); } region = new MemoryRegion(regionInfo); asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo("Read")); }, null); asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(new ProgressBarData(0, 0, 1))); }, null); ReadRegion(harwareStream, region, progressAct); } } catch (Exception e) { pcInfo.error = e; } pcInfo.Message = "Read complete"; asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(new ProgressBarData(0, 0, 1))); }, null); asyncOp.PostOperationCompleted(delegate(object args) { completed(pcInfo, region); }, null); }; (new Thread(start)).Start(); }
int IRFIDProgrammer.Adjust(Action <ProgrammingCompleteInfo, MemoryRegion> completed, Action <ProgrammingProgressInfo> progress) { _isBusy = true; AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new Guid()); Action <int, int> progressAct = delegate(int val, int max) { asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(new ProgressBarData(0, val, max))); }, null); }; ThreadStart start = delegate() { asyncOp.Post(delegate(object args) { OnBusy(); }, null); ProgrammingCompleteInfo pcInfo = new ProgrammingCompleteInfo(); MemoryRegion region = new MemoryRegion(0, 256, 0); try { SafeFileHandle WriteHandle = CreateFile(_hardwarePath, Win32HardwareIOSupport.GENERIC_WRITE, Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, 0, IntPtr.Zero); if (WriteHandle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } SafeFileHandle ReadHandle = CreateFile(_hardwarePath, Win32HardwareIOSupport.GENERIC_READ, Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, 0, IntPtr.Zero); if (ReadHandle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } uint wrBytes; byte[] request = new byte[65]; byte[] response = new byte[65]; byte maxTime = 0; byte minTime = 0xFF; for (int i = 0; i < 20; i++) { request[1] = Constants.RFID125_TEST; if (!WriteFile(WriteHandle, request, 65, out wrBytes, IntPtr.Zero)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } if (!ReadFile(ReadHandle, response, 65, out wrBytes, IntPtr.Zero)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } if (response[3] > maxTime) { maxTime = response[3]; } if (response[3] < minTime) { minTime = response[3]; } progressAct(i, 20); } response[3] = maxTime; response[4] = minTime; region.WriteData(0, response); } catch (Exception e) { pcInfo.error = e; } pcInfo.Message = "Complete"; asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(new ProgressBarData(0, 0, 1))); }, null); asyncOp.Post(delegate(object args) { completed(pcInfo, region); }, null); _isBusy = false; asyncOp.PostOperationCompleted(delegate(object args) { OnReady(); }, null); }; (new Thread(start)).Start(); return(0); }
int IRFIDProgrammer.Write(List <byte[]> request, Action <ProgrammingCompleteInfo, MemoryRegion> completed, Action <ProgrammingProgressInfo> progress) { _isBusy = true; AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new Guid()); Action <int, int> progressAct = delegate(int val, int max) { asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(new ProgressBarData(0, val, max))); }, null); }; ThreadStart start = delegate() { asyncOp.Post(delegate(object args) { OnBusy(); }, null); ProgrammingCompleteInfo pcInfo = new ProgrammingCompleteInfo(); MemoryRegion region = new MemoryRegion(0, 256, 0); try { SafeFileHandle WriteHandle = CreateFile(_hardwarePath, Win32HardwareIOSupport.GENERIC_WRITE, Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, 0, IntPtr.Zero); if (WriteHandle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } SafeFileHandle ReadHandle = CreateFile(_hardwarePath, Win32HardwareIOSupport.GENERIC_READ, Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, 0, IntPtr.Zero); if (ReadHandle.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } uint wrBytes; byte[] respdata; int step = 1; foreach (byte[] req in request) { if (!WriteFile(WriteHandle, req, 65, out wrBytes, IntPtr.Zero)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } respdata = new byte[65]; if (!ReadFile(ReadHandle, respdata, 65, out wrBytes, IntPtr.Zero)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } if (respdata[1] != 0) { throw new Exception("Write error: " + respdata[1].ToString("X2")); } progressAct(step++, request.Count); } } catch (Exception e) { pcInfo.error = e; } pcInfo.Message = "Complete"; asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(new ProgressBarData(0, 0, 1))); }, null); asyncOp.Post(delegate(object args) { completed(pcInfo, region); }, null); _isBusy = false; asyncOp.PostOperationCompleted(delegate(object args) { OnReady(); }, null); }; (new Thread(start)).Start(); return(0); }
int I921Programmer.ReadChip(string mode, MemoryRegionInfo regionInfo, Action <ProgrammingCompleteInfo, MemoryRegion> completed, Action <ProgrammingProgressInfo> progress) { _isBusy = true; AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new Guid()); string infoString = ""; string errString = ""; DataReceivedEventHandler processOutput = delegate(Object sender, DataReceivedEventArgs drea) { if ((drea.Data != null) && drea.Data.Contains(_readingSequense[_readingStep])) { if (drea.Data.Contains("Done")) { if (infoString == "") { infoString += (_readingSequense[_readingStep] + " Ok"); } else { infoString += ("\r\n" + _readingSequense[_readingStep] + " Ok"); } _readingStep++; asyncOp.Post(delegate(object args) { progress(new ProgrammingProgressInfo(infoString)); }, null); } else { errString = _readingSequense[_readingStep] + " Error"; } } }; ThreadStart start = delegate() { asyncOp.Post(delegate(object args) { OnBusy(); }, null); MemoryRegion region = new MemoryRegion(regionInfo); ProgrammingCompleteInfo pcInfo = new ProgrammingCompleteInfo(); _readingStep = 0; try { string path = Application.ExecutablePath; path = path.Substring(0, path.LastIndexOf("\\")); Directory.SetCurrentDirectory(path); Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = "ps3cc921.exe"; p.StartInfo.CreateNoWindow = true; p.OutputDataReceived += processOutput; p.Start(); p.BeginOutputReadLine(); p.WaitForExit(); p.Close(); if (errString != "") { throw new Exception(errString); } else { pcInfo.Message = "Reading Ok"; using (BinaryReader file = new BinaryReader(File.OpenRead("RS3CC921.BIN"))) { file.BaseStream.Position = 0; for (int i = 0; i < region.Data.Length; i++) { region.Data[i] = file.ReadByte(); } } } } catch (Exception e) { pcInfo.error = e; } asyncOp.Post(delegate(object args) { completed(pcInfo, region); }, null); _isBusy = false; asyncOp.PostOperationCompleted(delegate(object args) { OnReady(); }, null); }; (new Thread(start)).Start(); return(0); }