Skip to content

Detect serial port connection and disconnection events in Windows using WMI and C#.

License

Notifications You must be signed in to change notification settings

shubham0x13/SerialPortWatcher

Repository files navigation

SerialPortWatcher

MIT License

Detects serial port connection and disconnection events in Windows using WMI (Windows Management Instrumentation). This library monitors serial port availability in real-time, enabling dynamic response to new port connections and disconnections.

Installation

Install the NuGet package from here.

Usage/Examples

Here's an example of how to use SerialPortWatcher in your application:

namespace SerialPortWatcher.Example
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SerialPortEventWatcher serialPortEventWatcher = new SerialPortEventWatcher();
            serialPortEventWatcher.PortConnected += SerialPortEventWatcher_PortConnected;
            serialPortEventWatcher.PortDisconnected += SerialPortEventWatcher_PortDisconnected;
            serialPortEventWatcher.Start();

            Console.WriteLine("Watching Serial Port Connect/Disconnect Events.");
            Console.ReadKey();

            serialPortEventWatcher.Stop();
            serialPortEventWatcher.Dispose();
        }

        private static void SerialPortEventWatcher_PortConnected(object? sender, SerialPortWatcherEventArgs e)
        {
            Console.WriteLine($"Serial Port Connected : {e.PortName}.");
        }
        private static void SerialPortEventWatcher_PortDisconnected(object? sender, SerialPortWatcherEventArgs e)
        {
            Console.WriteLine($"Serial Port Disconnected : {e.PortName}.");
        }
    }
}

About

Detect serial port connection and disconnection events in Windows using WMI and C#.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages