private bool readDataWithProgress(int address, int size, out byte[] data) { byte[] dump = new byte[size]; bool success = false; data = null; ELM327 elm = new ELM327(GetCOMPort()); elm.Open(); if (elm.adapterInit() && elm.startConnection()) { for (int chunksize = 128; (chunksize > 4) && !success; chunksize >>= 1) { bool attemptFailed = false; for (int offset = 0; (offset < size) && !attemptFailed; offset += chunksize) { string resp; if (elm.doRequest("23" + (address + offset).ToString("X6") + chunksize.ToString("X2"), out resp) && resp.StartsWith("63")) { byte[] rba = HexUtils.hexStringToByteArray(resp); Array.Copy(rba, 1, dump, offset, rba.Length - 1); progBar.Value = 100 * (offset + chunksize) / size; } else { attemptFailed = true; } } success = !attemptFailed; } if (success) { data = dump; } elm.closeConnection(); } elm.Close(); return(success); }
private void uploadEEPROMToolStripMenuItem_Click(object sender, EventArgs e) { ELM327 elm = new ELM327(GetCOMPort()); elm.Open(); if (elm.adapterInit() && elm.startConnection()) { //byte[] newEEPROM = byteViewer.GetBytes(); byte[] newEEPROM = ((DynamicByteProvider)hexBox.ByteProvider).Bytes.ToArray(); for (int offset = 0; offset < storedEEPROM.Length; offset++) { if (storedEEPROM[offset] != newEEPROM[offset]) { string resp; do { if (elm.doRequest("3D" + (0x100 + offset).ToString("X6") + "01" + storedEEPROM[offset].ToString("X2"), out resp) && resp.StartsWith("7D")) { storedEEPROM[offset] = newEEPROM[offset]; } } while ( !(elm.doRequest("23" + (0x100 + offset).ToString("X6") + "01", out resp) && resp.StartsWith("63") && HexUtils.hexStringToByteArray(resp)[1] == newEEPROM[offset]) ); } progBar.Value = 100 * offset / storedEEPROM.Length; } //byteViewer.SetBytes(storedEEPROM); hexBox.ByteProvider = new DynamicByteProvider(storedEEPROM); elm.closeConnection(); } elm.Close(); }