Code Monkey home page Code Monkey logo

Comments (3)

davepruitt avatar davepruitt commented on July 25, 2024

Sorry for such a late reply, I didn't see your comment until just now.

The code for finding serial ports uses the Windows ManagementObjectSearcher, and it searches for anything classified as a WIN32_SerialPort. Here is the full code snippet:

private void Refresh_ports_worker_DoWork(object sender, DoWorkEventArgs e)
{
    lock (_available_devices_lock)
    {
        //Create a list to hold information from USB devices
        List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

        //Query all connected devices
        var searcher = new ManagementObjectSearcher(@"SELECT * FROM WIN32_SerialPort");
        var collection = searcher.Get();

        //Grab the information we need
        foreach (var device in collection)
        {
            string id = (string)device.GetPropertyValue("DeviceID");
            string desc = (string)device.GetPropertyValue("Description");
            USBDeviceInfo d = new USBDeviceInfo(desc, id, false);
            devices.Add(d);
        }

        //Dispose of the collection of queried devices
        collection.Dispose();

        //Set the list of available devices
        _available_devices = devices;
    }
}

This code could theoretically be replaced with a call to SerialPort.GetPortNames(), but then you would have no information as to which device each COM port represents. As far as I know, the above code should find all the same COM ports as SerialPort.GetPortNames(), in addition to providing you extra information about what each COM port is.

from betterserialmonitor.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.