/// <summary>
        /// Get Result Of Latest CMD.
        /// </summary>
        /// <param name="inPipe">1 For CMD Response and 0 for Image Data.</param>
        /// <returns></returns>
        private async Task <byte[]> GetResults(int inPipe = 1)
        {
            //Config
            UsbBulkInPipe readPipe = _targetDevice.DefaultInterface.BulkInPipes[inPipe];

            readPipe.ReadOptions |= UsbReadOptions.IgnoreShortPacket;

            //Setup stream.
            var        stream = readPipe.InputStream;
            DataReader reader = new DataReader(stream);

            uint bytesRead = 0;

            try
            {
                bytesRead = await reader.LoadAsync(readPipe.EndpointDescriptor.MaxPacketSize);
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
            }

            //Get buffer
            IBuffer buffer = reader.ReadBuffer(bytesRead);
            var     result = buffer.ToArray();

            //Change result depending on size.
            var size = BitConverter.ToUInt32(result, 1);

            if (size > result.Count())
            {
                //We need to actually get more data.
                while (size >= result.Count())
                {
                    bytesRead = await reader.LoadAsync(readPipe.EndpointDescriptor.MaxPacketSize);

                    buffer = reader.ReadBuffer(bytesRead);
                    var newRes = ConcatArrays(result, buffer.ToArray());
                    result = newRes;
                }
            }

            //Remove padded zeros and return.
            if (size < result.Count())
            {
                return(result.Take((int)size).ToArray());
            }

            return(result);
        }
        private async void OnDeviceAdded(DeviceWatcher watcher, DeviceInformation deviceInformation)
        {
            if (deviceInformation.Name.StartsWith("acA1920-40um") && deviceInformation.IsEnabled)
            {
                _targetDevice = await UsbDevice.FromIdAsync(deviceInformation.Id);
                foreach (var interf in _targetDevice.Configuration.UsbInterfaces)
                {
                    if (interf.InterfaceNumber == 0)
                    {
                        _controlInPipe = interf.BulkInPipes[0];
                        _controlOutPipe = interf.BulkOutPipes[0];
                    }
                    else if (interf.InterfaceNumber == 2)
                        _streamInPipe = interf.BulkInPipes[0];
                }

                OnConnected(EventArgs.Empty);
            }
        }
Example #3
0
        public async Task <bool> OpenAsync()
        {
            if (IsOpen)
            {
                return(true); // we're already open
            }
            // Calling this will "open" the device, blocking any other app from using it.
            // This will return null if another program already has the device open.
            usbDevice = await UsbDevice.FromIdAsync(DevicePath);

            if (usbDevice == null)
            {
                return(false);
            }

            cts = new CancellationTokenSource();

            // https://msdn.microsoft.com/en-us/library/windows/hardware/dn303346(v=vs.85).aspx

            Version = (ushort)usbDevice.DeviceDescriptor.BcdDeviceRevision;

            pinConfigPipe     = usbDevice.DefaultInterface.BulkOutPipes[0];
            peripheralOutPipe = usbDevice.DefaultInterface.BulkOutPipes[1];

            pinConfigWriter  = new DataWriter(pinConfigPipe.OutputStream);
            peripheralWriter = new DataWriter(peripheralOutPipe.OutputStream);

            pinEventPipe   = usbDevice.DefaultInterface.BulkInPipes[0];
            pinEventReader = new DataReader(pinEventPipe.InputStream);

            peripheralInPipe = usbDevice.DefaultInterface.BulkInPipes[1];
            peripheralReader = new DataReader(peripheralInPipe.InputStream);

            IsOpen = true;
            pinEventListerner(cts.Token);

            Debug.WriteLine("Connection opened");

            return(true);
        }
        public async Task<bool> OpenAsync()
        {
            if (IsOpen)
                return true; // we're already open

            // Calling this will "open" the device, blocking any other app from using it.
            // This will return null if another program already has the device open.
            this.usbDevice = await UsbDevice.FromIdAsync(DevicePath);
            if (this.usbDevice == null)
                return false;

            cts = new CancellationTokenSource();

            // https://msdn.microsoft.com/en-us/library/windows/hardware/dn303346(v=vs.85).aspx

            Version = (short)usbDevice.DeviceDescriptor.BcdDeviceRevision;

            pinConfigPipe = usbDevice.DefaultInterface.BulkOutPipes[0];
            pinConfigPipe.WriteOptions |= UsbWriteOptions.ShortPacketTerminate;

            peripheralOutPipe = usbDevice.DefaultInterface.BulkOutPipes[1];
            peripheralOutPipe.WriteOptions |= UsbWriteOptions.ShortPacketTerminate;

            pinConfigWriter = new DataWriter(pinConfigPipe.OutputStream);
            peripheralWriter = new DataWriter(peripheralOutPipe.OutputStream);

            pinEventPipe = usbDevice.DefaultInterface.BulkInPipes[0];
            pinEventReader = new DataReader(pinEventPipe.InputStream);

            peripheralInPipe = usbDevice.DefaultInterface.BulkInPipes[1];
            peripheralReader = new DataReader(peripheralInPipe.InputStream);

            isOpen = true;
            pinEventListerner(cts.Token);

            Debug.WriteLine("Connection opened");

            return true;
        }