using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Ports;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Matrix_App.adds;
namespace Matrix_App.forms
{
public partial class Settings : Form
{
private readonly SerialPort port;
private readonly PortCommandQueue queue;
private readonly Regex comRegex = new Regex(@"COM([\d]+)");
public Settings(ref PortCommandQueue queue, ref SerialPort port)
{
InitializeComponent();
this.queue = queue;
this.port = port;
GatherPortNames(WritePortCombobox, port.PortName);
UpdateRealtimeCheckbox.Checked = queue.GetRealtimeUpdates();
Show();
}
private void UpdateRealtimeCheckbox_CheckedChanged(object sender, EventArgs e)
{
queue.SetRealtimeUpdates(UpdateRealtimeCheckbox.Checked);
}
private bool disableValidation;
///
/// Gathers all available ports and sets them to the combobox
///
[SuppressMessage("ReSharper", "CoVariantArrayConversion", Justification = "Never got an exception, so seems to be just fine")]
private void GatherPortNames(ComboBox portBox, string initialCom)
{
disableValidation = true;
var ports = SerialPort.GetPortNames();
// save previously selected
var selected = portBox.SelectedItem ?? initialCom;
if (!port.IsOpen)
{
selected = "None";
}
// get device names from ports
var newPorts = GetDeviceNames(ports);
// add virtual port
newPorts.AddLast("None");
// search for new port
foreach (var newPort in newPorts)
{
// find any new port
var found = portBox.Items.Cast