public void Write(string port, EEPROM eeprom, UInt32 offset, FileInfo source) { if (Operation != Operation.Idle) { throw new InvalidOperationException("Operation already in progress."); } Operation = Operation.Writing; try { // Read the data from the file byte[] data = File.ReadAllBytes(source.FullName); // Establish a connection FireConnectionStateChanged(ConnectionState.Connecting); OpenPort(port); FlushInput(); CheckSignature(SendCommand("!")); FireConnectionStateChanged(ConnectionState.Connected); // Set the EEPROM identifier CheckResponse(SendCommand('i', eeprom.ID)); // Write the data FireProgress(ProgressState.Write, 0, data.Length + 1, "Writing data."); while (offset < data.Length) { // Write the next block int chunk = Math.Min(32, data.Length - (int)offset); Response response = CheckResponse(SendCommand("w" + HexString(data, offset, (int)offset, chunk))); offset += (UInt32)chunk; // Update progress FireProgress(ProgressState.Write, (int)offset, data.Length + 1); } // Finish the operation CheckResponse(SendCommand("d")); } catch (ProtocolException ex) { FireError(ex.Message); } catch (Exception ex) { FireError("Unexpected error during operation.", ex); } finally { try { if (m_serial != null) { m_serial.Close(); } m_serial = null; } catch { // Just ignore it } FireConnectionStateChanged(ConnectionState.Disconnected); Operation = Operation.Idle; } }
public void Read(string port, EEPROM eeprom, UInt32 offset, UInt32 size, FileInfo target) { if (Operation != Operation.Idle) { throw new InvalidOperationException("Operation already in progress."); } Operation = Operation.Reading; try { // Establish a connection FireConnectionStateChanged(ConnectionState.Connecting); OpenPort(port); FlushInput(); CheckSignature(SendCommand("!")); FireConnectionStateChanged(ConnectionState.Connected); // Set the EEPROM identifier CheckResponse(SendCommand('i', eeprom.ID)); // Read the data byte[] data = new byte[size]; UInt32 received = 0; FireProgress(ProgressState.Read, 0, (int)size + 1, "Reading data."); while (received < size) { // Ask for the next block Response response = CheckResponse(SendCommand(String.Format("r{0:x6}", offset))); VerifyChecksum(response.Data); // Add it to the array for (int i = 3; (i < (response.Data.Length - 2)) && (received < size); i++, offset++) { data[received++] = response.Data[i]; } // Update progress FireProgress(ProgressState.Read, (int)offset, (int)size + 1); } // Now save the data to the file FireProgress(ProgressState.Read, (int)offset, (int)size + 1, String.Format("Saving to '{0}'", target.FullName)); File.WriteAllBytes(target.FullName, data); } catch (ProtocolException ex) { FireError(ex.Message); } catch (Exception ex) { FireError("Unexpected error during operation.", ex); } finally { try { if (m_serial != null) { m_serial.Close(); } m_serial = null; } catch { // Just ignore it } FireConnectionStateChanged(ConnectionState.Disconnected); Operation = Operation.Idle; } }