rskibbe.IO.Ports.Com.System.Windows – Detecting COM port additions & removals in .NET

rskibbe.IO.Ports.Com.System.Windows – Detecting COM port additions & removals in .NET
rskibbe.IO.Ports.Com.System.Windows – Detecting COM port additions & removals in .NET

What is this package about?

This package is trying to provide you the best and easiest helpers, to react to COM port changes, whether it be connections or removals – as well as listing ports. As you can already see by the name, it is a sub-package of „rskibbe.IO.Ports.Com“.

💡 In a hurry?: No problem, just navigate to the points of interest by using the table of contents from above. If you need a quick example, or an example download, scroll to the bottom.

Quick example

Quick example for rskibbe.IO.Ports.Com.System.Windows - detecting COM port changes, but easy
Quick example for rskibbe.IO.Ports.Com.System.Windows – detecting COM port changes, but easy

Without much blabla, we will now dive into a quick usage example of this (sub) package. If you need more details, just hop to the specific sections of this article.

Step 1 – Install the package

Open the NuGet Package Manager and run the following commands (or just use the UI):

Install-Package rskibbe.IO.Ports.Com.System.Windows

// You may need to install this as well
// trying to fix this for the future
Install-Package System.Management

Step 2 – Using the code

In the next step, you need to create an instance of „WindowsSystemComPorts“ like shown in the example below. Still – keep in mind, that this package is a sub package which can be fused together with another one to combine even more functionality.

For sure you can also use it more or less on its own – like only for physical COM ports.

Import

Now go ahead and import the mentioned class by using the corresponding namespace:

using rskibbe.IO.Ports.Com.System.Windows;

Create an instance

In the next step, you can create an instance by using the provided factory method:

var comPorts = await WindowsSystemComPorts.BuildAsync();

Keep an eye on the SynchronizationContext-Parameter of the BuildAsync-Method. It defaults to the current one!

Attach handlers

Now you can easily attach your handlers of desire as usual:

// create some handler methods
private void ComPort_Added(object sender, ComPortEventArgs e)
{
  // access the changed portName "COM3"
  // e.PortName
  // access the changed portId "COM3" -> 3
  // e.PortId
}

private void ComPort_Removed(object sender, ComPortEventArgs e)
{
  // access the changed portName "COM3"
  // e.PortName
  // access the changed portId "COM3" -> 3
  // e.PortId
}

// somewhere..
comPorts.SystemComPortAdded += ComPort_Added;
comPorts.SystemComPortRemoved += ComPort_Removed;

Full code example

I’ve put together some copy and paste ready „Form1“ code for you here. Don’t forget to change the namespaces to your app, if you copy everything

using rskibbe.IO.Ports.Com.System.Windows;
using System.Diagnostics;

namespace YourProjectNamespaceHere;

public partial class Form1 : Form
{

    protected WindowsSystemComPorts _windowsSystemComPorts;

    public Form1()
    {
        InitializeComponent();
        Load += Form1_Load;
    }

    private async void Form1_Load(object? sender, EventArgs e)
    {
        _windowsSystemComPorts = await WindowsSystemComPorts.BuildAsync();
        _windowsSystemComPorts.SystemComPortAdded += (s, e) =>
        {
            Debug.WriteLine($"{nameof(_windowsSystemComPorts.SystemComPortAdded)}: {e.PortName}");
        };
        _windowsSystemComPorts.SystemComPortRemoved += (s, e) =>
        {
            Debug.WriteLine($"{nameof(_windowsSystemComPorts.SystemComPortRemoved)}: {e.PortName}");
        };
    }

}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert