/// <summary> /// gets a list of devices /// </summary> /// <returns></returns> protected static List <DeviceListItem> getDeviceList(Guid deviceInterfaceGuid) { IntPtr deviceListHandle = Winusb.listCreate(deviceInterfaceGuid); try { UInt32 deviceListSize = Winusb.listSize(deviceListHandle); var deviceList = new List <DeviceListItem>(); for (Byte i = 0; i < deviceListSize; i++) { // Get all the needed info in an effecient way. This is more efficient than // using listGetSerialNumber and listGetProductId because we only need to // get the device instance once instead of 3 times. Int32 deviceInstance = Winusb.listGetDeviceInstance(deviceListHandle, i); String serialNumber = Winusb.getSerialNumber(deviceInstance); UInt16 productId = Winusb.getProductID(deviceInstance); DeviceListItem item = new DeviceListItem(deviceInstance, deviceInterfaceGuid, "#" + serialNumber, serialNumber, productId); deviceList.Add(item); } return(deviceList); } finally { Winusb.listDestroy(deviceListHandle); } }
/// <summary> /// Connect to the USB device specified by the DeviceListItem. /// </summary> protected UsbDevice(DeviceListItem deviceListItem) { WinUsbDeviceHandles handles; try { handles = Winusb.connect(deviceListItem.guid, deviceListItem.deviceInstance); } catch (Exception exception) { if (exception is Win32Exception && ((Win32Exception)exception).NativeErrorCode == 5) // ERROR_ACCESS_DENIED { throw new Exception("Access was denied when trying to connect to the device. " + "Try closing all programs using the device."); } else { throw new Exception("There was an error connecting to the device.", exception); } } device = new MyWinUsbDevice(handles, deviceListItem.serialNumber); }
/// <summary> /// Registers your form to receive notifications from the OS when one /// of a particular class of devices is removed or attached. See /// Usb.WM_DEVICECHANGE for example code for receiving notifications. /// You should only call this if supportsNotify returns true or your /// application will only be run on Windows. /// </summary> /// <param name="guid">The device interface GUID of the /// device you are interested in (from the INF file).</param> /// <param name="handle">The handle of the form that will receive /// notifications (form.Handle).</param> /// <returns>A handle representing this notification request.</returns> public static IntPtr notificationRegister(Guid guid, IntPtr handle) { return(Winusb.notificationRegister(guid, handle)); }
/// <summary> /// Returns a list of port names (e.g. "COM2", "COM3") for all /// currently-connected devices in the Ports list in the Device Manager /// whose device instance ID begins with the given prefix string. /// /// For example, to get the port names of the umc01a bootloader, /// give a prefix of = USB\PID_1FFB&PID_0082. /// </summary> /// <param name="deviceInstanceIdPrefix"> /// The string that we match against the device instance ID. The device /// instance ID of the device you want must begin with this string. /// </param> /// <returns></returns> public static IList <String> getPortNames(String deviceInstanceIdPrefix) { return(Winusb.getPortNames(deviceInstanceIdPrefix)); }