Exemple #1
0
        private async Task Listen(DeviceAndReaderContainer container)
        {
            try
            {
                if (container.SerialDevice != null)
                {
                    container.Reader = new DataReader(container.SerialDevice.InputStream);

                    // keep reading the serial input
                    while (true)
                    {
                        await ReadAsync(container);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType().Name == "TaskCanceledException")
                {
                    CloseDevice(container.SerialDevice);
                }
                else
                {
                }
            }
            finally
            {
                // Cleanup once complete
                if (container.Reader != null)
                {
                    container.Reader.DetachStream();
                    container.Reader = null;
                }
            }
        }
Exemple #2
0
        private async Task ReadAsync(DeviceAndReaderContainer container)
        {
            try
            {
                //Realiza la lectura de puerto serie

                uint ReadBufferLength = 1;

                // If task cancellation was requested, comply
                container.CancellationTokenSource.Token.ThrowIfCancellationRequested();

                // Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
                container.Reader.InputStreamOptions = InputStreamOptions.Partial;

                // Create a task object to wait for data on the serialPort.InputStream
                var loadAsyncTask = container.Reader.LoadAsync(ReadBufferLength).AsTask(container.CancellationTokenSource.Token);

                // Launch the task and wait
                UInt32 bytesRead = await loadAsyncTask;
                if (bytesRead > 0)
                {
                    var data = container.Reader.ReadString(bytesRead);


                    concatCommand = concatCommand + data;
                    if (data == "\n")
                    {
                        ProcessArduinoResults(concatCommand);
                        Debug.WriteLine("L - " + contador + " : " + concatCommand);
                        ++contador;
                        concatCommand = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }
Exemple #3
0
        private DeviceAndReaderContainer CancelAsyncTask(DeviceInformation device)
        {
            var exist = false;

            foreach (var deviceAndReaderContainer in this.containerList)
            {
                if (deviceAndReaderContainer.DeviceInformation != null &&
                    deviceAndReaderContainer.DeviceInformation.Id == device.Id)
                {
                    if (deviceAndReaderContainer.CancellationTokenSource != null)
                    {
                        if (!deviceAndReaderContainer.CancellationTokenSource.IsCancellationRequested)
                        {
                            deviceAndReaderContainer.CancellationTokenSource.Cancel();
                            deviceAndReaderContainer.SerialDevice?.Dispose();
                            deviceAndReaderContainer.SerialDevice = null;
                            exist = true;

                            return(deviceAndReaderContainer);
                        }
                    }
                }
            }
            if (!exist)
            {
                if (this.containerList == null)
                {
                    this.containerList = new List <DeviceAndReaderContainer>();
                }
                var deviceRead = new DeviceAndReaderContainer();
                deviceRead.DeviceInformation = device;
                this.containerList.Add(deviceRead);

                return(deviceRead);
            }

            return(null);
        }