/// <summary> /// Populates the list of COM ports. /// </summary> private void PopulateComPorts() { // Don't refresh if the control is not enabled if (!this.comPortSelector.Enabled) { return; } // Get the current list from the combobox so we can auto-select the new device var oldPortList = this.comPortSelector.Items; // Cache the selected item so we can try to re-select it later object selectedValue = null; selectedValue = this.GetSelectedPort(); // Enumerate the COM ports and bind the COM port selector List <ComPort> comPorts = new List <ComPort>(); comPorts = ComPort.EnumeratePortList(); // Check if we have a Maple device MapleDevice mapleCheck = MapleDevice.FindMaple(); // Populate the COM port selector this.PopulatePortSelector(comPorts); // If we had an old list, compare it to the new one and pick the first item which is new if (oldPortList.Count > 0) { foreach (ComPort newPort in comPorts) { bool found = false; foreach (ComPort oldPort in oldPortList) { if (newPort.Name == oldPort.Name) { found = true; } } if (found == false) { Debug.WriteLine($"{newPort.Name} was added."); selectedValue = newPort.Name; } } } // Re-select the previously selected item this.SelectPort(selectedValue); // Set the width of the dropdown // this.comPortSelector.DropDownWidth = comPorts.Select(c => c.DisplayName).ToList().Max(x => TextRenderer.MeasureText(x, this.comPortSelector.Font).Width); // Make sure the Update button is disabled if there is no port selected this.CheckControls(); }
/// <summary> /// Gets the name of the Maple Device COM port. /// </summary> /// <returns>A string containing the COM port name.</returns> public static string GetMapleComPort() { string portName = null; MapleDevice device = FindMaple(); if (device.DeviceFound) { _ = new List <ComPort>(); List <ComPort> comPorts = ComPort.EnumeratePortList(); foreach (ComPort port in comPorts) { if (device.DeviceName.Contains(port.Name)) { portName = port.Name; } } } return(portName); }
/// <summary> /// Populates the list of COM ports. /// </summary> private void PopulateComPorts() { if (this.InvokeRequired) { this.Invoke(new Action(this.PopulateComPorts)); return; } // Don't refresh if the control is not enabled if (!this.comPortSelector.Enabled) { return; } // Get the current list from the combobox so we can auto-select the new device var oldPortList = this.comPortSelector.Items; // Cache the selected item so we can try to re-select it later object selectedValue = null; selectedValue = this.comPortSelector.SelectedValue; // Enumerate the COM ports and bind the COM port selector List <ComPort> comPorts = new List <ComPort>(); comPorts = ComPort.EnumeratePortList(); // Check if we have a Maple device MapleDevice mapleCheck = MapleDevice.FindMaple(); this.comPortSelector.DataSource = comPorts; this.comPortSelector.DisplayMember = "Name"; this.comPortSelector.ValueMember = "Name"; // If we had an old list, compare it to the new one and pick the first item which is new if (oldPortList.Count > 0) { foreach (ComPort newPort in comPorts) { bool found = false; foreach (ComPort oldPort in oldPortList) { if (newPort.Name == oldPort.Name) { found = true; } } if (found == false) { Debug.WriteLine($"{newPort.Name} was added."); selectedValue = newPort.Name; } } } // Re-select the previously selected item if (selectedValue != null) { this.comPortSelector.SelectedValue = selectedValue; } else { this.comPortSelector.SelectedItem = null; } // Check if we there's a Maple device plugged in if (mapleCheck.DeviceFound) { // Set the Write Bootloader radio button and disable the controls if a Maple device is present // Required so that the firmware size is calculated correctly this.writeBootloader_Yes.Checked = true; this.writeBootloader_Yes.Enabled = false; this.writeBootloader_No.Enabled = false; } else { this.writeBootloader_Yes.Enabled = true; this.writeBootloader_No.Enabled = true; } // Set the width of the dropdown // this.comPortSelector.DropDownWidth = comPorts.Select(c => c.DisplayName).ToList().Max(x => TextRenderer.MeasureText(x, this.comPortSelector.Font).Width); // Make sure the Update button is disabled if there is no port selected this.CheckControls(); }