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
stringPath to the device
Exceptions
- HidException
Raised on failure
Device(ushort, ushort)
Connects to a given device.
public Device(ushort vendorId, ushort productId)
Parameters
Exceptions
- HidException
Raised on failure
Device(ushort, ushort, string)
Connects to a given device.
public Device(ushort vendorId, ushort productId, string serialNumber)
Parameters
vendorId
ushortVendor id of target device
productId
ushortProduct id of target device
serialNumber
stringSerial 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
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
byteReport id
maxLength
intMax 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
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
byteReport id
maxLength
intMax 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
intMax 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
intMax 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
intMax 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
intMax 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
intMax 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
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
intMax length of the expected data. The value can be greater than the actual report.
milliseconds
inttimeout 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
inttimeout 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
SendOutputReport(ReadOnlySpan<byte>)
Sends an output report over the control endpoint as a Set_Report transfer.
public void SendOutputReport(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.
Remarks
Available since hidapi 0.15.0
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
booltrue: 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