USB port current value

江枫思渺然 提交于 2019-12-23 05:13:31

问题


Is it possible to get the USB port current value when I plug the device in it?

The value should be available programmatically using C#. The second parameter I need is 'USB save power mode'. The correct name of the property is "Allow the computer to turn off this device to save power".

I managed to detect the device (if it is connected or disconnected) and read data from it correctly.

Do these two properties belong to USB device or to USB port?!

WinUSB Api did not give me clear answers to my questions.


回答1:


you can use the WMI the get the USB port properties

public class MyClass
{
    public static void Main()
    {
        var usbDevices = GetUSBDevices();

        foreach (var usbDevice in usbDevices)
        {
            Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
                usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
        }
    }

    static List<USBDeviceInfo> GetUSBDevices()
    {
        List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

        var searcher = new ManagementObjectSearcher(@"Select * From Win32_SerialPort");

        foreach (var device in searcher.Get())
        {
            devices.Add(new USBDeviceInfo(
            (string)device.GetPropertyValue("DeviceID"),
            (string)device.GetPropertyValue("PNPDeviceID"),
            (string)device.GetPropertyValue("Description"),
            (string)device.GetPropertyValue("Name")
            ));
        }

        return devices;
    }

    class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name)
        {
            this.DeviceID = deviceID;
            this.PnpDeviceID = pnpDeviceID;
            this.Description = description;
            this.Name = name;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
        public string Name { get; private set; }

    }
}

have a look at the below for the properties list

Availability should contain the info you are looking for

http://msdn.microsoft.com/en-us/library/aa394413%28v=vs.85%29.aspx




回答2:


I think that most of what you want doesn't have anything to do with WinUSB from WinDDK but more with how the OS handles devices:

http://msdn.microsoft.com/en-us/library/aa394504%28v=VS.85%29.aspx - WMI class to access USB related info
http://www.acpi.info/DOWNLOADS/ACPIspec40a.pdf - ACPI could provide some help

IF you need to dig deeprer into USB see
http://www.usb.org/developers - all relevant USB standards documents
http://www.beyondlogic.org/usbnutshell/usb1.shtml - some usefull information
http://www.libusb.org/ - a library to handle USB at a very low level



来源:https://stackoverflow.com/questions/7028671/usb-port-current-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!