Table of Contents

Class Device

Namespace
HidApi
Assembly
HidApi.Net.dll

Represents a HID device.

public sealed class Device : IDisposable
Inheritance
Device
Implements
Inherited Members

Remarks

Call Dispose to free all unmanaged resources.

Constructors

Device(string)

Connects to a given device.

public Device(string path)

Parameters

path string

Path to the device

Exceptions

HidException

Raised on failure

Device(ushort, ushort)

Connects to a given device.

public Device(ushort vendorId, ushort productId)

Parameters

vendorId ushort

Vendor id of target device

productId ushort

product id of target device

Exceptions

HidException

Raised on failure

Device(ushort, ushort, string)

Connects to a given device.

public Device(ushort vendorId, ushort productId, string serialNumber)

Parameters

vendorId ushort

Vendor id of target device

productId ushort

Product id of target device

serialNumber string

Serial number of target device

Exceptions

HidException

Raised on failure

Methods

Dispose()

Frees all unmanaged resources.

public void Dispose()

GetDeviceInfo()

Returns the device info for a device.

public DeviceInfo GetDeviceInfo()

Returns

DeviceInfo

DeviceInfo

Remarks

Available since hidapi 0.13.0

Exceptions

HidException

Raised on failure

GetFeatureReport(byte, int)

Returns the feature report for a given report id.

public ReadOnlySpan<byte> GetFeatureReport(byte reportId, int maxLength)

Parameters

reportId byte

Report id

maxLength int

Max length of the expected data. The value can be greater than the actual report.

Returns

ReadOnlySpan<byte>

The received data of the HID device.

Exceptions

ArgumentOutOfRangeException

Raised if maxLength is smaller than 1

HidException

Raised on failure

GetIndexedString(int, int)

Returns a string from its index.

public string GetIndexedString(int stringIndex, int maxLength = 128)

Parameters

stringIndex int

The index of the string

maxLength int

Max length of the string

Returns

string

The string at the given index. The length is limited to maxLength characters.

Exceptions

ArgumentOutOfRangeException

Raised if maxlength is smaller than 0

HidException

Raised on failure

OverflowException

If the requested string length is too large to be created by the dotnet runtime.

GetInputReport(byte, int)

Returns the input report for a given report id.

public ReadOnlySpan<byte> GetInputReport(byte reportId, int maxLength)

Parameters

reportId byte

Report id

maxLength int

Max length of the expected data. The value can be greater than the actual report.

Returns

ReadOnlySpan<byte>

The received data of the HID device.

Remarks

Available since hidapi 0.10.0

Exceptions

ArgumentOutOfRangeException

Raised if maxLength is smaller than 1

HidException

Raised on failure

GetManufacturer(int)

Returns the manufacturer.

public string GetManufacturer(int maxLength = 128)

Parameters

maxLength int

Max length of the returned manufacturer string

Returns

string

A string containing the name of the manufacturer. The length is limited to maxLength characters.

Exceptions

ArgumentOutOfRangeException

Raised if maxlength is smaller than 0

HidException

Raised on failure

OverflowException

If the requested string length is too large to be created by the dotnet runtime.

GetProduct(int)

Returns the product.

public string GetProduct(int maxLength = 128)

Parameters

maxLength int

Max length of the returned product string

Returns

string

A string containing the name of the product. The length is limited to maxLength characters.

Exceptions

ArgumentOutOfRangeException

Raised if maxlength is smaller than 0

HidException

Raised on failure

OverflowException

If the requested string length is too large to be created by the dotnet runtime.

GetReportDescriptor(int)

Gets the report descriptor of the device.

public ReadOnlySpan<byte> GetReportDescriptor(int bufSize = 4096)

Parameters

bufSize int

Max length of the expected data.

Returns

ReadOnlySpan<byte>

The report descriptor

Remarks

Available since hidapi 0.14.0

Exceptions

ArgumentOutOfRangeException

Raised if bufLength is less than 0

HidException

Raised on failure

GetSerialNumber(int)

Returns the serial number.

public string GetSerialNumber(int maxLength = 128)

Parameters

maxLength int

Max length of the returned serial number string

Returns

string

A string containing the serial number. The length is limited to maxLength characters.

Exceptions

ArgumentOutOfRangeException

Raised if maxlength is smaller than 0

HidException

Raised on failure

OverflowException

If the requested string length is too large to be created by the dotnet runtime.

Read(int)

Returns an input report.

public ReadOnlySpan<byte> Read(int maxLength)

Parameters

maxLength int

Max length of the expected data. The value can be greater than the actual report.

Returns

ReadOnlySpan<byte>

The received data of the HID device. If non-blocking mode is enabled and no data is available an empty result will be returned.

Exceptions

ArgumentOutOfRangeException

Raised if maxlength is smaller than 0

HidException

Raised on failure

Read(Span<byte>)

Returns an input report.

public int Read(Span<byte> buffer)

Parameters

buffer Span<byte>

Buffer to write the data into

Returns

int

The length of the received data in bytes. If non-blocking mode is enabled and no data is available 0 will be returned.

Exceptions

HidException

Raised on failure

ReadTimeout(int, int)

Returns an input report.

public ReadOnlySpan<byte> ReadTimeout(int maxLength, int milliseconds)

Parameters

maxLength int

Max length of the expected data. The value can be greater than the actual report.

milliseconds int

timeout in milliseconds. -1 for blocking mode.

Returns

ReadOnlySpan<byte>

The received data of the HID device. If the timeout is exceeded an empty result is returned.

Exceptions

ArgumentOutOfRangeException

Raised if maxlength is smaller than 0

HidException

Raised on failure

ReadTimeout(Span<byte>, int)

Returns an input report.

public int ReadTimeout(Span<byte> buffer, int milliseconds)

Parameters

buffer Span<byte>

Buffer to write the data into

milliseconds int

timeout in milliseconds. -1 for blocking mode.

Returns

int

The length of the received data in bytes. If the timeout is exceeded 0 is returned.

Exceptions

HidException

Raised on failure

SendFeatureReport(ReadOnlySpan<byte>)

Sends a feature report to the device.

public void SendFeatureReport(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

The data which should be sent to the device. The first byte must contain the report id or 0x0 if the device does not use numbered reports.

Exceptions

HidException

Raised on failure

SetNonBlocking(bool)

In blocking mode a call to Read(int) will block until some data is available. In Non blocking mode Read(int) will return immediately without data.

public void SetNonBlocking(bool setNonBlocking)

Parameters

setNonBlocking bool

true: Enable non blocking mode. false: Disable non blocking mode.

Exceptions

HidException

Raised on failure

Write(ReadOnlySpan<byte>)

Write an output report to the device.

public void Write(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

Data to send. The first byte must contain the report id or 0x00 if the device only supports one report

Exceptions

HidException

Raised on failure