Exemplo n.º 1
0
        /// <summary>
        /// WriteData
        /// </summary>
        /// <param name="command">command data to send</param>
        /// <param name="ack">true if ack is epected</param>
        /// <param name="retries">number of retries</param>
        /// <returns></returns>
        private byte[] WriteData(byte[] command, bool ack = true, int retries = 5)
        {
            byte[] ackBuffer = new byte[2];
            bool   done      = false;

            while ((!done) && (retries-- > 0))
            {
                SendCommandAsync(command).Wait();

                if (ack)
                {
                    LoadResponseAsync(2).Wait();
                    DataReaderObject.ReadBytes(ackBuffer);
                    done = 0xEE07 != (ackBuffer[0] << 8 | ackBuffer[1]);
                }
                else
                {
                    done = true;
                }
            }

            if (!done)
            {
                throw new Exception("WriteData retries exceeded");
            }
            return(ackBuffer);
        }
Exemplo n.º 2
0
        public async Task LoadResponseAsync(uint length)
        {
            Task <UInt32> loadAsyncTask;

            loadAsyncTask = DataReaderObject.LoadAsync(length).AsTask();
            uint bytesRead = await loadAsyncTask;

            if (length != bytesRead)
            {
                throw new Exception("ReadDataAsync timeout");
            }
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
 /// <summary>
 /// DisconnectFromUART
 /// Disconnects from the UART
 /// </summary>
 public void DisconnectFromUART()
 {
     if (null != DataReaderObject)
     {
         DataReaderObject.Dispose();
     }
     if (null != DataWriterObject)
     {
         DataWriterObject.Dispose();
     }
     if (null != SerialPort)
     {
         SerialPort.Dispose();
         SerialPort = null;
     }
 }
Exemplo n.º 5
0
        private async void Device_PinChanged(SerialDevice sender, PinChangedEventArgs args)
        {
            if (args.PinChange == SerialPinChange.DataSetReady)
            {
                var size = await DataReaderObject.LoadAsync(int.MaxValue);

                if (size > 0)
                {
                    var str = DataReaderObject.ReadString(size);

                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        Content += $"Device: {str}\r\n";
                    });
                }
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// DisconnectFromUART
 /// Disconnects from the UART
 /// </summary>
 public void DisconnectFromUART()
 {
     StopReading();
     if (null != DataReaderObject)
     {
         //DataReaderObject.DetachStream();
         //DataReaderObject.DetachBuffer();
         DataReaderObject.Dispose();
     }
     if (null != DataWriterObject)
     {
         DataWriterObject.Dispose();
     }
     if (null != SerialPort)
     {
         SerialPort.Dispose();
         SerialPort = null;
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// ReadData
        /// </summary>
        /// <param name="data">Array of data read from device</param>
        /// <returns></returns>
        private byte[] ReadData(byte[] command, uint readLength)
        {
            byte[] headerBuffer = new byte[2];
            byte[] returnBuffer = new byte[readLength];

            SendCommandAsync(command).Wait();

            LoadResponseAsync(2).Wait();
            DataReaderObject.ReadBytes(headerBuffer);

            if (headerBuffer[0] != 0xBB)
            {
                throw new Exception(string.Format("ReadData error 0x{0:x2}{0:x2}", headerBuffer[0], headerBuffer[1]));
            }
            if (headerBuffer[1] != readLength)
            {
                throw new Exception(string.Format("ReadData error: failed to read {0} bytes.  Read {1}", readLength, headerBuffer[1]));
            }

            LoadResponseAsync(readLength).Wait();
            DataReaderObject.ReadBytes(returnBuffer);

            return(returnBuffer);
        }
Exemplo n.º 8
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;
        }