private void btnUpload_Click(object sender, EventArgs e) { if (__Port == null) { return; } btnUpload.Enabled = false; btnCancel.Enabled = false; ComProgrammer.enBoardType bt = GetBoardType(); new Task(() => { byte[] firmware = null; if (cbUploadFirmware.Checked) { if (!File.Exists(tbFirmware.Text)) { MessageBox.Show(this, "Firmware file is not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { firmware = File.ReadAllBytes(tbFirmware.Text); } catch (Exception exx) { MessageBox.Show(this, exx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } try { if (firmware != null) { if (!__Port.WriteMainProgram(firmware)) { MessageBox.Show(this, "Firmware upload failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (ProgramCode != null) { if (!__Port.WriteByteCode(bt, ProgramCode, firmware == null)) { MessageBox.Show(this, "Configuration upload failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (Exception exx) { MessageBox.Show(this, exx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }).Start(); }
private void btnUpload_Click(object sender, EventArgs e) { if (__Port == null) { return; } const ComProgrammer.enBoardType bt = ComProgrammer.enBoardType.STM429IZ; if (!string.IsNullOrEmpty(tbFirmware.Text) && !File.Exists(tbFirmware.Text)) { ShowError("Firmware file is not found"); return; } byte[] firmware = null; if (!String.IsNullOrEmpty(tbFirmware.Text)) { try { firmware = File.ReadAllBytes(tbFirmware.Text); } catch (Exception exx) { ShowError(exx.Message); return; } } btnUpload.Enabled = false; btnCancel.Enabled = false; new Task(() => { try { if (firmware != null) { TotalLength = firmware.Length; if (!__Port.WriteMainProgram(bt, firmware, cbFullFlashErase.Checked)) { ShowError("Firmware upload failed"); return; } } if (cbFullFlashErase.Checked) { return; } if (cbEraseProgram.Checked && !__Port.EraseProgramArea(bt)) { ShowError("Program erase failed"); return; } // ReSharper disable once RedundantJumpStatement if (cbEraseVariables.Checked && !__Port.EraseVariablesArea(bt)) { ShowError("Variables erase failed"); return; } } catch (Exception exx) { ShowError(exx.Message); } finally { Invoke(new Action(() => { btnUpload.Enabled = true; btnCancel.Enabled = true; })); } }).Start(); }