예제 #1
0
        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);
        }
예제 #2
0
 public static extern int ClaimInterface([In] LibUsbDeviceHandle deviceHandle, int interfaceNumber);
예제 #3
0
 internal static extern LibUsbError BulkTransfer([In] LibUsbDeviceHandle deviceHandle, byte endpoint,
                                                 byte[] Data, int length, out int actualLength, int timeout);
예제 #4
0
 public static extern int GetStringDescriptor([In] LibUsbDeviceHandle deviceProfileHandle, [In] byte desc_index,
                                              StringBuilder sb, int length);