C# Class LibUsbDotNet.UsbDevice

Contains non-driver specific USB device communication members.
This class is compatible with WinUSB, LibUsb-Win32, and linux libusb v1.x. Platform independent applications should only use usb device members from this class. If more functionality is required, it is up to the application to handle multi-driver and/or multi-platfrom requirements.
Mostra file Open project: arvydas/BlinkStickDotNet Class Usage Examples

Public Properties

Property Type Description
ForceLibUsbWinBack bool

Private Properties

Property Type Description
FireUsbError void
GetDeviceConfigs List
UsbDevice System

Public Methods

Method Description
Close ( ) : bool

Closes the UsbDevice and disposes any ActiveEndpoints.

ControlTransfer ( UsbSetupPacket &setupPacket, IntPtr buffer, int bufferLength, int &lengthTransferred ) : bool

Transmits control data over a default control endpoint.

ControlTransfer ( UsbSetupPacket &setupPacket, object buffer, int bufferLength, int &lengthTransferred ) : bool

Transmits control data over a default control endpoint.

Exit ( ) : void

De-initializes the USB driver.

If this method is not called before the application exits, it can cause it to hang indefinitely.

Calling this method multiple times will have no effect.

GetAltInterfaceSetting ( byte interfaceID, byte &selectedAltInterfaceID ) : bool

Gets the selected alternate interface of the specified interface.

GetConfiguration ( byte &config ) : bool

Gets the USB devices active configuration value.

GetDescriptor ( byte descriptorType, byte index, short langId, IntPtr buffer, int bufferLength, int &transferLength ) : bool

Gets a descriptor from the device. See DescriptorType for more information.

GetDescriptor ( byte descriptorType, byte index, short langId, object buffer, int bufferLength, int &transferLength ) : bool

Gets a descriptor from the device. See DescriptorType for more information.

GetLangIDs ( short &langIDs ) : bool

Asking for the zero'th index is special - it returns a string descriptor that contains all the language IDs supported by the device. Typically there aren't many - often only one. The language IDs are 16 bit numbers, and they start at the third byte in the descriptor. See USB 2.0 specification, section 9.6.7, for more information on this.

GetString ( string &stringData, short langId, byte stringIndex ) : bool

Gets a DescriptorType.String descriptor from the device.

Open ( ) : bool

Opens the USB device handle.

OpenEndpointReader ( ReadEndpointID readEndpointID ) : UsbEndpointReader

Opens a EndpointType.Bulk endpoint for reading

OpenEndpointReader ( ReadEndpointID readEndpointID, int readBufferSize ) : UsbEndpointReader

Opens a EndpointType.Bulk endpoint for reading

OpenEndpointReader ( ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType ) : UsbEndpointReader

Opens an endpoint for reading

OpenEndpointWriter ( WriteEndpointID writeEndpointID ) : UsbEndpointWriter

Opens a EndpointType.Bulk endpoint for writing

OpenEndpointWriter ( WriteEndpointID writeEndpointID, EndpointType endpointType ) : UsbEndpointWriter

Opens an endpoint for writing

OpenUsbDevice ( Predicate findDevicePredicate ) : UsbDevice

Opens the usb device that matches the find predicate.

OpenUsbDevice ( UsbDeviceFinder usbDeviceFinder ) : UsbDevice

Opens the usb device that matches the UsbDeviceFinder.

OpenUsbDevice ( System.Guid &devInterfaceGuid, UsbDevice &usbDevice ) : bool

Opens a WinUsb device by its DeviceInterfaceGUID.

This is the Microsoft-recommended way for opening a WinUsb device. LibUsb device can be opened in this way as well. In order to open LibUsb devices in this manner, an entry must be added to the driver inf file:

[Install.HW]

Addreg=Add_LibUsb_Guid_Reg

[Add_LibUsb_Guid_Reg]

HKR,,LibUsbInterfaceGUIDs,0x10000,"{Your-Unique-Guid-String}"

Private Methods

Method Description
FireUsbError ( object sender, UsbError usbError ) : void
GetDeviceConfigs ( UsbDevice usbDevice ) : List
UsbDevice ( UsbApiBase usbApi, SafeHandle usbHandle ) : System

Method Details

Close() public abstract method

Closes the UsbDevice and disposes any ActiveEndpoints.
public abstract Close ( ) : bool
return bool

ControlTransfer() public method

Transmits control data over a default control endpoint.
public ControlTransfer ( UsbSetupPacket &setupPacket, IntPtr buffer, int bufferLength, int &lengthTransferred ) : bool
setupPacket LibUsbDotNet.Main.UsbSetupPacket An 8-byte setup packet which contains parameters for the control request. /// See section 9.3 USB Device Requests of the Universal Serial Bus Specification Revision 2.0 for more information.
buffer System.IntPtr Data to be sent/received from the device.
bufferLength int Length of the buffer param.
lengthTransferred int Number of bytes sent or received (depends on the direction of the control transfer).
return bool

ControlTransfer() public method

Transmits control data over a default control endpoint.
public ControlTransfer ( UsbSetupPacket &setupPacket, object buffer, int bufferLength, int &lengthTransferred ) : bool
setupPacket LibUsbDotNet.Main.UsbSetupPacket An 8-byte setup packet which contains parameters for the control request. /// See section 9.3 USB Device Requests of the Universal Serial Bus Specification Revision 2.0 for more information.
buffer object Data to be sent/received from the device.
bufferLength int Length of the buffer param.
lengthTransferred int Number of bytes sent or received (depends on the direction of the control transfer).
return bool

Exit() public static method

De-initializes the USB driver.
If this method is not called before the application exits, it can cause it to hang indefinitely.

Calling this method multiple times will have no effect.

public static Exit ( ) : void
return void

GetAltInterfaceSetting() public method

Gets the selected alternate interface of the specified interface.
public GetAltInterfaceSetting ( byte interfaceID, byte &selectedAltInterfaceID ) : bool
interfaceID byte The interface settings number (index) to retrieve the selected alternate interface setting for.
selectedAltInterfaceID byte The alternate interface setting selected for use with the specified interface.
return bool

GetConfiguration() public method

Gets the USB devices active configuration value.
public GetConfiguration ( byte &config ) : bool
config byte The active configuration value. A zero value means the device is not configured and a non-zero value indicates the device is configured.
return bool

GetDescriptor() public method

Gets a descriptor from the device. See DescriptorType for more information.
public GetDescriptor ( byte descriptorType, byte index, short langId, IntPtr buffer, int bufferLength, int &transferLength ) : bool
descriptorType byte The descriptor type ID to retrieve; this is usually one of the enumerations.
index byte Descriptor index.
langId short Descriptor language id.
buffer System.IntPtr Memory to store the returned descriptor in.
bufferLength int Length of the buffer parameter in bytes.
transferLength int The number of bytes transferred to buffer upon success.
return bool

GetDescriptor() public method

Gets a descriptor from the device. See DescriptorType for more information.
public GetDescriptor ( byte descriptorType, byte index, short langId, object buffer, int bufferLength, int &transferLength ) : bool
descriptorType byte The descriptor type ID to retrieve; this is usually one of the enumerations.
index byte Descriptor index.
langId short Descriptor language id.
buffer object Memory to store the returned descriptor in.
bufferLength int Length of the buffer parameter in bytes.
transferLength int The number of bytes transferred to buffer upon success.
return bool

GetLangIDs() public method

Asking for the zero'th index is special - it returns a string descriptor that contains all the language IDs supported by the device. Typically there aren't many - often only one. The language IDs are 16 bit numbers, and they start at the third byte in the descriptor. See USB 2.0 specification, section 9.6.7, for more information on this.
public GetLangIDs ( short &langIDs ) : bool
langIDs short
return bool

GetString() public method

Gets a DescriptorType.String descriptor from the device.
public GetString ( string &stringData, short langId, byte stringIndex ) : bool
stringData string Buffer to store the returned string in upon success.
langId short The language ID to retrieve the string in. (0x409 for english).
stringIndex byte The string index to retrieve.
return bool

Open() public abstract method

Opens the USB device handle.
public abstract Open ( ) : bool
return bool

OpenEndpointReader() public method

Opens a EndpointType.Bulk endpoint for reading
public OpenEndpointReader ( ReadEndpointID readEndpointID ) : UsbEndpointReader
readEndpointID ReadEndpointID Endpoint number for read operations.
return UsbEndpointReader

OpenEndpointReader() public method

Opens a EndpointType.Bulk endpoint for reading
public OpenEndpointReader ( ReadEndpointID readEndpointID, int readBufferSize ) : UsbEndpointReader
readEndpointID ReadEndpointID Endpoint number for read operations.
readBufferSize int Size of the read buffer allocated for the event.
return UsbEndpointReader

OpenEndpointReader() public method

Opens an endpoint for reading
public OpenEndpointReader ( ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType ) : UsbEndpointReader
readEndpointID ReadEndpointID Endpoint number for read operations.
readBufferSize int Size of the read buffer allocated for the event.
endpointType EndpointType The type of endpoint to open.
return UsbEndpointReader

OpenEndpointWriter() public method

Opens a EndpointType.Bulk endpoint for writing
public OpenEndpointWriter ( WriteEndpointID writeEndpointID ) : UsbEndpointWriter
writeEndpointID WriteEndpointID Endpoint number for read operations.
return UsbEndpointWriter

OpenEndpointWriter() public method

Opens an endpoint for writing
public OpenEndpointWriter ( WriteEndpointID writeEndpointID, EndpointType endpointType ) : UsbEndpointWriter
writeEndpointID WriteEndpointID Endpoint number for read operations.
endpointType EndpointType The type of endpoint to open.
return UsbEndpointWriter

OpenUsbDevice() public static method

Opens the usb device that matches the find predicate.
public static OpenUsbDevice ( Predicate findDevicePredicate ) : UsbDevice
findDevicePredicate Predicate The predicate function used to find the usb device.
return UsbDevice

OpenUsbDevice() public static method

Opens the usb device that matches the UsbDeviceFinder.
public static OpenUsbDevice ( UsbDeviceFinder usbDeviceFinder ) : UsbDevice
usbDeviceFinder LibUsbDotNet.Main.UsbDeviceFinder The class used to find the usb device.
return UsbDevice

OpenUsbDevice() public static method

Opens a WinUsb device by its DeviceInterfaceGUID.
This is the Microsoft-recommended way for opening a WinUsb device. LibUsb device can be opened in this way as well. In order to open LibUsb devices in this manner, an entry must be added to the driver inf file:

[Install.HW]

Addreg=Add_LibUsb_Guid_Reg

[Add_LibUsb_Guid_Reg]

HKR,,LibUsbInterfaceGUIDs,0x10000,"{Your-Unique-Guid-String}"

public static OpenUsbDevice ( System.Guid &devInterfaceGuid, UsbDevice &usbDevice ) : bool
devInterfaceGuid System.Guid Device Interface GUID of the usb device to open.
usbDevice UsbDevice On success, a new instance.
return bool

Property Details

ForceLibUsbWinBack public_oe static_oe property

Setting this field to will force LibUsbDotNet to use the Libusb-1.0 Windows-backend driver. For platforms other than windows, this setting has no effect.
If this is , AllDevices will return only MonoUsbDevices in the list.
public static bool ForceLibUsbWinBack
return bool