public async Task <bool> OpenAsync() { var handle = new IntPtr(); NativeMethods.Open(deviceProfile, ref handle); deviceHandle = new LibUsbDeviceHandle(handle); NativeMethods.ClaimInterface(deviceHandle, 0); isOpen = true; pinListenerTask = new Task(async() => { while (isOpen) { var buffer = new byte[41]; var len = 0; try { var res = NativeMethods.BulkTransfer(deviceHandle, pinReportEndpoint, buffer, buffer.Length, out len, 1000); switch (res) { case LibUsbError.Success: PinEventDataReceived?.Invoke(buffer); break; case LibUsbError.ErrorNoDevice: case LibUsbError.ErrorPipe: case LibUsbError.ErrorOverflow: this.Close(); // HACK ALERT: device removal notifications don't seem to work right, so if we get ErrorNoDevice, // request that ConnectionService remove the board that matches our device path. ((LibUsbConnectionService)ConnectionService.Instance).RemoveDevice(this.DevicePath); break; case LibUsbError.ErrorTimeout: case LibUsbError.ErrorIO: break; // normal behavior default: Debug.WriteLine("Pin Data Read Failure: " + res); break; } } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } int rate = (int)Math.Round(1000.0 / UpdateRate); if (rate > 1) { await Task.Delay(rate).ConfigureAwait(false); } } }); pinListenerTask.Start(); return(true); }
public static extern int ClaimInterface([In] LibUsbDeviceHandle deviceHandle, int interfaceNumber);
internal static extern LibUsbError BulkTransfer([In] LibUsbDeviceHandle deviceHandle, byte endpoint, byte[] Data, int length, out int actualLength, int timeout);
public static extern int GetStringDescriptor([In] LibUsbDeviceHandle deviceProfileHandle, [In] byte desc_index, StringBuilder sb, int length);