private void BUT_copy1280_Click(object sender, EventArgs e) { ArduinoSTK port = new ArduinoSTK(); port.BaudRate = 57600; port.DataBits = 8; port.StopBits = StopBits.One; port.Parity = Parity.None; port.DtrEnable = true; try { port.PortName = ArdupilotMega.MainV2.comPortName; log.Info("Open Port"); port.Open(); log.Info("Connect AP"); if (port.connectAP()) { log.Info("Download AP"); byte[] EEPROM = new byte[1024 * 4]; for (int a = 0; a < 4 * 1024; a += 0x100) { port.setaddress(a); port.download(0x100).CopyTo(EEPROM, a); } log.Info("Verify State"); if (port.keepalive()) { StreamWriter sw = new StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"EEPROM1280.bin"); BinaryWriter bw = new BinaryWriter(sw.BaseStream); bw.Write(EEPROM, 0, EEPROM.Length); bw.Close(); log.Info("Download AP"); byte[] FLASH = new byte[1024 * 128]; for (int a = 0; a < FLASH.Length; a += 0x100) { port.setaddress(a); port.downloadflash(0x100).CopyTo(FLASH, a); } sw = new StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"FLASH1280.bin"); bw = new BinaryWriter(sw.BaseStream); bw.Write(FLASH, 0, FLASH.Length); bw.Close(); } else { CustomMessageBox.Show("Communication Error - Bad data"); } } else { CustomMessageBox.Show("Communication Error - no connection"); } port.Close(); } catch (Exception ex) { CustomMessageBox.Show("Port Error? " + ex.ToString()); if (port != null && port.IsOpen) { port.Close(); } } }
private void BUT_copyto1280_Click(object sender, EventArgs e) { ArduinoComms port = new ArduinoSTK(); port.BaudRate = 57600; port.DataBits = 8; port.StopBits = StopBits.One; port.Parity = Parity.None; port.DtrEnable = true; StreamReader sr = new StreamReader(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"EEPROM1280.bin"); BinaryReader br = new BinaryReader(sr.BaseStream); byte[] EEPROM = br.ReadBytes(1024 * 4); br.Close(); sr.Close(); sr = new StreamReader(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"FLASH1280.bin"); br = new BinaryReader(sr.BaseStream); byte[] FLASH = br.ReadBytes(1024 * 128); br.Close(); sr.Close(); try { port.PortName = ArdupilotMega.MainV2.comPortName; port.Open(); if (port.connectAP()) { log.Info("starting"); port.uploadflash(FLASH, 0, FLASH.Length, 0); port.upload(EEPROM, 0, (short)EEPROM.Length, 0); log.Info("Uploaded"); } else { CustomMessageBox.Show("Communication Error - no connection"); } port.Close(); } catch (Exception ex) { CustomMessageBox.Show("Check port settings or Port in use? " + ex.ToString()); port.Close(); } }
public bool WriteCharsetVersion(string version, bool showMsg = true) { // byte[] tempEeprom = new byte[3]; // tempEeprom[0] = (byte)version[0]; // tempEeprom[1] = (byte)version[1]; // tempEeprom[2] = (byte)version[2]; EEPROM tempEeprom = new EEPROM(); ArduinoSTK sp = osd.OpenArduino(); if (sp != null && sp.connectAP()) { // Сначала считать try { for (int i = 0; i < 5; i++) //try to download two times if it fail { byte[] data = sp.download(EEPROM_SIZE); if (sp.down_flag) { tempEeprom.data = data; break; } else { if (sp.keepalive()) { Console.WriteLine("keepalive successful (iter " + i + ")"); } else { Console.WriteLine("keepalive fail (iter " + i + ")"); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } tempEeprom.CS_version = version; try { bool spupload_flag = false; for (int i = 0; i < 10; i++) //try to upload two times if it fail // spupload_flag = sp.upload(tempEeprom, (short)0, (short)tempEeprom.Length, (short)CS_VERSION1_ADDR); { spupload_flag = sp.upload(tempEeprom.data, (short)Settings_offset, (short)Settings_size, (short)Settings_offset); if (!spupload_flag) { if (sp.keepalive()) { Console.WriteLine("keepalive successful (iter " + i + ")"); } else { Console.WriteLine("keepalive fail (iter " + i + ")"); } } else { break; } } if (spupload_flag) { if (showMsg) { MessageBox.Show("Done writing configuration data!"); } } else { MessageBox.Show("Failed to upload new configuration data"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("Failed to talk to bootloader"); } sp.Close(); return(true); }
private void BUT_copy1280_Click(object sender, EventArgs e) { ArduinoSTK port = new ArduinoSTK(); port.BaudRate = 57600; port.DataBits = 8; port.StopBits = StopBits.One; port.Parity = Parity.None; port.DtrEnable = true; try { port.PortName = MissionPlanner.MainV2.comPortName; log.Info("Open Port"); port.Open(); log.Info("Connect AP"); if (port.connectAP()) { log.Info("Download AP"); byte[] EEPROM = new byte[1024 * 4]; for (int a = 0; a < 4 * 1024; a += 0x100) { port.setaddress(a); port.download(0x100).CopyTo(EEPROM, a); } log.Info("Verify State"); if (port.keepalive()) { StreamWriter sw = new StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"EEPROM1280.bin"); BinaryWriter bw = new BinaryWriter(sw.BaseStream); bw.Write(EEPROM, 0, EEPROM.Length); bw.Close(); log.Info("Download AP"); byte[] FLASH = new byte[1024 * 128]; for (int a = 0; a < FLASH.Length; a += 0x100) { port.setaddress(a); port.downloadflash(0x100).CopyTo(FLASH, a); } sw = new StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"FLASH1280.bin"); bw = new BinaryWriter(sw.BaseStream); bw.Write(FLASH, 0, FLASH.Length); bw.Close(); } else { CustomMessageBox.Show("Communication Error - Bad data"); } } else { CustomMessageBox.Show("Communication Error - no connection"); } port.Close(); } catch (Exception ex) { CustomMessageBox.Show("Port Error? " + ex.ToString()); if (port != null && port.IsOpen) { port.Close(); } } }
private void BUT_ReadIOB_Click(object sender, EventArgs e) { toolStripProgressBar1.Style = ProgressBarStyle.Continuous; this.toolStripStatusLabel1.Text = ""; bool fail = false; ArduinoSTK sp; try { if (comPort.IsOpen) { comPort.Close(); } sp = new ArduinoSTK(); sp.PortName = CMB_ComPort.Text; sp.BaudRate = 57600; sp.DtrEnable = true; sp.Open(); } catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (sp.connectAP()) { try { eeprom = sp.download(1024); } catch (Exception ex) { fail = true; MessageBox.Show(ex.Message); } } else { MessageBox.Show("Failed to talk to bootloader"); fail = true; } sp.Close(); int startmbind = 64; if (!fail) { for (int pos = 0; pos < 16; pos++) { patterns[pos] = (ushort)((ushort)(eeprom[pos * 2] << 8) + eeprom[pos * 2 + 1]); Control[] ctls = this.Controls.Find("pattern" + (pos + 1), true); if (ctls.Length > 0) { ((Pattern)ctls[0]).Value = patterns[pos]; ((Pattern)ctls[0]).FlightMode = (ushort)(eeprom[startmbind + pos * 2]); } } } printeeprom(); if (!fail) { MessageBox.Show("Done!"); } }
private void updateFirmwareToolStripMenuItem_Click(object sender, EventArgs e) { toolStripProgressBar1.Style = ProgressBarStyle.Continuous; this.toolStripStatusLabel1.Text = ""; OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "*.hex|*.hex"; ofd.ShowDialog(); if (ofd.FileName != "") { byte[] FLASH; try { toolStripStatusLabel1.Text = "Reading Hex File"; statusStrip1.Refresh(); FLASH = readIntelHEXv2(new StreamReader(ofd.FileName)); } catch { MessageBox.Show("Bad Hex File"); return; } bool fail = false; ArduinoSTK sp; try { if (comPort.IsOpen) { comPort.Close(); } sp = new ArduinoSTK(); sp.PortName = CMB_ComPort.Text; sp.BaudRate = 57600; sp.DtrEnable = true; sp.Open(); } catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } toolStripStatusLabel1.Text = "Connecting to Board"; if (sp.connectAP()) { sp.Progress += new ArduinoSTK.ProgressEventHandler(sp_Progress); try { if (!sp.uploadflash(FLASH, 0, FLASH.Length, 0)) { if (sp.IsOpen) { sp.Close(); } MessageBox.Show("Upload failed. Lost sync. Try using Arduino to upload instead", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { fail = true; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Failed to talk to bootloader"); } sp.Close(); if (!fail) { toolStripStatusLabel1.Text = "Done"; MessageBox.Show("Done!"); } else { toolStripStatusLabel1.Text = "Failed"; } } }
private void BUT_WriteIOB_Click(object sender, EventArgs e) { toolStripProgressBar1.Style = ProgressBarStyle.Continuous; this.toolStripStatusLabel1.Text = ""; foreach (Control ctl in tabPage2.Controls) { if (ctl.GetType() == typeof(Pattern)) { Pattern pat = ctl as Pattern; int no = 0; if (int.TryParse(pat.Name.Substring(pat.Name.Length - 2), out no)) { eeprom[(no - 1) * 2] = (byte)(pat.Value >> 8); eeprom[(no - 1) * 2 + 1] = (byte)(pat.Value & 0xff); eeprom[64 + (no - 1) * 2] = (byte)pat.FlightMode; eeprom[64 + (no - 1) * 2 + 1] = (byte)pat.FlightMode; } else if (int.TryParse(pat.Name.Substring(pat.Name.Length - 1), out no)) { eeprom[(no - 1) * 2] = (byte)(pat.Value >> 8); eeprom[(no - 1) * 2 + 1] = (byte)(pat.Value & 0xff); eeprom[64 + (no - 1) * 2] = (byte)pat.FlightMode; eeprom[64 + (no - 1) * 2 + 1] = (byte)pat.FlightMode; } } } printeeprom(); ArduinoSTK sp; try { if (comPort.IsOpen) { comPort.Close(); } sp = new ArduinoSTK(); sp.PortName = CMB_ComPort.Text; sp.BaudRate = 57600; sp.DtrEnable = true; sp.Open(); } catch { MessageBox.Show("Error opening com port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (sp.connectAP()) { try { if (sp.upload(eeprom, 0, 126, 0)) { MessageBox.Show("Done!"); } else { MessageBox.Show("Failed to upload new settings"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("Failed to talk to bootloader"); } sp.Close(); }