예제 #1
0
        private async Task <byte> OneWireBitAsync(byte b)
        {
            var bit = b > 0 ? 0xFF : 0x00;

            DataWriteObject.WriteByte((byte)bit);
            await DataWriteObject.StoreAsync();

            await DataReaderObject.LoadAsync(1);

            var data = DataReaderObject.ReadByte();

            return((byte)(data & 0xFF));
        }
예제 #2
0
        /// <summary>
        /// ReadAsync
        ///  Task to read PMTK sentences.
        ///  Performs sentence assembly, calls parsing routine
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns>async Task</returns>
        private async Task ReadAsync(CancellationToken cancellationToken)
        {
            Task <UInt32> loadAsyncTask;

            uint   ReadBufferLength = 128;
            UInt32 bytesRead        = 0;

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

            int retries = 5;

            string rawSentence = "";
            string leftovers   = "";

            while ((!cancellationToken.IsCancellationRequested) && (retries > 0))
            {
                try
                {
                    while (true)
                    {
                        // If task cancellation was requested, comply
                        cancellationToken.ThrowIfCancellationRequested();

                        // Create a task object to wait for data on the serialPort.InputStream
                        loadAsyncTask = DataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
                        // Launch the task and wait
                        bytesRead = await loadAsyncTask;
                        if (bytesRead > 0)
                        {
                            rawSentence = DataReaderObject.ReadString(bytesRead);
                            rawSentence = string.Concat(leftovers, rawSentence);
                            string[] splitSentences = rawSentence.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                            leftovers = "";

                            foreach (string str in splitSentences)
                            {
                                if ((str.Length >= 3) && (str.LastIndexOf('*') == str.Length - 3))
                                {
                                    System.Diagnostics.Debug.WriteLine(str);
                                    DispatchSentence(str);
                                }
                                else
                                {
                                    leftovers = str;
                                }
                            }
                        }
                        retries = 5;
                    }
                }
                catch (OperationCanceledException ex)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Exiting GPS read task"));
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    //do nothing. this tends to sort itself out eventually
                    //System.Diagnostics.Debug.WriteLine(string.Format("ArgumentOutOfRangeException: {0}", ex.Message));
                    for (int b = 0; b < bytesRead; b++)
                    {
                        DataReaderObject.ReadByte();
                        //  bytesRead--;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Error reading from GPS: {0}", ex.Message));
                    leftovers = "";
                    retries--;
                }
            }
            SerialPort.Dispose();
            SerialPort = null;
        }