Aggiungere i file di progetto.

This commit is contained in:
andrea
2025-12-24 16:41:31 +01:00
parent 0ff8e6ea52
commit 28c4c7427f
9 changed files with 477 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Timers;
public sealed class SerialPortWatcher : IDisposable
{
private readonly System.Timers.Timer _timer;
private HashSet<string> _last = new(StringComparer.OrdinalIgnoreCase);
public event Action<string[]>? PortsAdded;
public event Action<string[]>? PortsRemoved;
public SerialPortWatcher(double intervalMs = 1000)
{
_timer = new System.Timers.Timer(intervalMs);
_timer.Elapsed += (_, _) => Tick();
_timer.AutoReset = true;
}
public void Start()
{
_last = GetPorts();
_timer.Start();
}
public void Stop() => _timer.Stop();
private void Tick()
{
var current = GetPorts();
var added = current.Except(_last).ToArray();
var removed = _last.Except(current).ToArray();
if (added.Length > 0) PortsAdded?.Invoke(added);
if (removed.Length > 0) PortsRemoved?.Invoke(removed);
_last = current;
}
private static HashSet<string> GetPorts()
=> new HashSet<string>(SerialPort.GetPortNames(), StringComparer.OrdinalIgnoreCase);
public void Dispose()
{
_timer.Stop();
_timer.Dispose();
}
}