private void _device_Inserted() { _timer.Stop(); if (EndPoint == null) { EndPoint = HidLibrary.HidDevices.Enumerate(21324).FirstOrDefault(); } Attached?.Invoke(this, null); }
private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { EndPoint = HidLibrary.HidDevices.Enumerate(21324).FirstOrDefault(); if (EndPoint == null) { return; } EndPoint.Removed += _device_Removed; EndPoint.Inserted += _device_Inserted; EndPoint.MonitorDeviceEvents = true; }
public static (ReadStatus Status, byte[] Output) FastRead(this HidLibrary.HidDevice device) { try { var data = new byte[device.Capabilities.OutputReportByteLength]; uint bytesRead; if (ReadFile(device.Handle, data, (uint)data.Length, out bytesRead, IntPtr.Zero)) { return(ReadStatus.Success, data); } else { return(ReadStatus.NoDataRead, new byte[0]); } } catch (Exception) { return(ReadStatus.ReadError, new byte[0]); } }
public static bool FastWrite(this HidLibrary.HidDevice device, byte[] buffer) { var deviceByteLength = device?.Capabilities.InputReportByteLength ?? 0; var arrayLength = Math.Min(buffer.Length, deviceByteLength); try { if (deviceByteLength > 0) { var outputBuffer = new byte[device.Capabilities.InputReportByteLength]; Array.Copy(buffer, outputBuffer, Math.Min(buffer.Length, device.Capabilities.InputReportByteLength)); var overlapped = new NativeOverlapped(); if (WriteFile(device.Handle, outputBuffer, (uint)outputBuffer.Length, out _, ref overlapped)) { return(true); } } return(false); } catch { return(false); } }
private void _device_Removed() { EndPoint = null; Detached?.Invoke(this, null); }