/// <summary> /// Enumerates the available COM ports. /// </summary> /// <returns>Returns an ordered array of COM port names.</returns> public static string[] EnumeratePorts() { // Get the list of COM port names List <string> comPorts = SerialPort.GetPortNames().ToList(); // Sort the list of ports comPorts = comPorts.OrderBy(c => c.Length).ThenBy(c => c).ToList(); // Check if we there's a Maple DFU device plugged in and add it to the list if (MapleDevice.FindMaple().DeviceFound) { comPorts.Add("DFU Device"); } // Check if we there's a USBasp device plugged in and add it to the list if (UsbAspDevice.FindUsbAsp().DeviceFound) { comPorts.Add("USBasp"); } // Return an array of COM port names return(comPorts.ToArray()); }
/// <summary> /// Enumerates the available COM ports without using WMI. /// </summary> /// <returns>Returns an ordered list of ports <see cref="ComPort"/>.</returns> public static List <ComPort> EnumeratePortList() { // Get the start time so we can write how long this takes to the debug window DateTime start = DateTime.Now; Debug.WriteLine("Enumerating COM ports"); // Create a list to store the COM port names List <ComPort> comPorts = new List <ComPort>(); // Get all the COM port names string[] comPortNames = SerialPort.GetPortNames().Distinct().ToArray(); // Add all the COM ports to the list foreach (string portName in comPortNames) { // We only know the port name, so write that to all three properties ComPort thisPort = new ComPort { Name = portName, Description = portName, DisplayName = portName, }; comPorts.Add(thisPort); } // Sort the list of ports by name comPorts = comPorts.OrderBy(c => c.Name.Length).ThenBy(c => c.Name).ToList(); // Check if we there's a Maple DFU device plugged in and add it to the list if (MapleDevice.FindMaple().DfuMode) { ComPort dfuPort = new ComPort { Name = "DFU Device", Description = "DFU Device", DisplayName = "DFU Device", }; comPorts.Add(dfuPort); } // Check if we there's a USBasp device plugged in and add it to the list if (UsbAspDevice.FindUsbAsp().DeviceFound) { ComPort usbAsp = new ComPort { Name = "USBasp", Description = "USBasp", DisplayName = "USBasp", }; comPorts.Add(usbAsp); } // Get the time now and write how long that took DateTime end = DateTime.Now; Debug.WriteLine($"COM ports enumerated in {end - start}."); // Return the list of COM ports return(comPorts); }
/// <summary> /// Main method where all the action happens. /// Called by the Upload button. /// </summary> private async void ButtonUpload_Click(object sender, EventArgs e) { // Disable the buttons until this flash attempt is complete Debug.WriteLine("Disabling the controls..."); this.EnableControls(false); // Clear the output box Debug.WriteLine("Clearing the output textboxes..."); this.textActivity.Clear(); this.textVerbose.Clear(); this.progressBar1.Value = 0; this.outputLineBuffer = string.Empty; // Check if the file exists if (!File.Exists(this.textFileName.Text)) { this.AppendLog(string.Format("File {0} does not exist", this.textFileName.Text)); MessageBox.Show("Firmware file does not exist.", "Write Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } // Check the file size if (!FileUtils.CheckFirmwareFileSize(this.textFileName.Text)) { this.EnableControls(true); return; } // Determine if we should use Maple device MapleDevice mapleResult = MapleDevice.FindMaple(); // Determine if we should use a USBasp device UsbAspDevice usbaspResult = UsbAspDevice.FindUsbAsp(); // Determine if the selected file contains USB / bootloader support bool firmwareSupportsUsb = FileUtils.CheckForUsbSupport(this.textFileName.Text); // Get the signature from the firmware file FileUtils.FirmwareFile fileSignature = FileUtils.GetFirmwareSignature(this.textFileName.Text); // Error if flashing non-USB firmware via native USB port if (mapleResult.DeviceFound && !firmwareSupportsUsb) { string msgBoxMessage = "The selected firmware file was compiled without USB support.\r\n\r\nFlashing this firmware would prevent the Multiprotocol module from functioning correctly.\r\n\r\nPlease select a different firmware file."; MessageBox.Show(msgBoxMessage, "Incompatible Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } // Get the selected COM port string comPort = this.comPortSelector.SelectedValue.ToString(); // Do the selected flash using the appropriate method if (mapleResult.DeviceFound == true) { Debug.WriteLine($"Maple device found in {mapleResult.Mode} mode\r\n"); await MapleDevice.WriteFlash(this, this.textFileName.Text, comPort); } else if (usbaspResult.DeviceFound == true && comPort == "USBasp") { if (fileSignature == null) { string msgBoxMessage = "Unable to check the specified firmware file for compatibility with this upload method."; MessageBox.Show(msgBoxMessage, "Incompatible Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } if (fileSignature.ModuleType != "AVR") { string msgBoxMessage = "The selected firmware file is not compatible with this upload method."; MessageBox.Show(msgBoxMessage, "Incompatible Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error); this.EnableControls(true); return; } await UsbAspDevice.WriteFlash(this, this.textFileName.Text, fileSignature.BootloaderSupport); } else { await SerialDevice.WriteFlash(this, this.textFileName.Text, comPort, firmwareSupportsUsb); } }