Ejemplo n.º 1
0
 /// <summary>
 /// Raises the position changed event
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 protected virtual void OnPositionChanged(object sender, PozyxPositionEventArgs args)
 {
     PositionChanged?.Invoke(sender, args);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Update position
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 private void OnPositionChanged(object sender, PozyxPositionEventArgs args)
 {
     Position = args;
     PositionChanged?.Invoke(this, args);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Open reader
        /// </summary>
        /// <returns></returns>
        private async Task Reader(SerialDevice port, CancellationToken cancel)
        {
            // create reader
            var reader = new DataReader(port.InputStream);

            reader.UnicodeEncoding    = Windows.Storage.Streams.UnicodeEncoding.Utf8;
            reader.InputStreamOptions = InputStreamOptions.Partial;

            // create buffer
            var buffer = new StringBuilder();

            // keep reading
            while (!cancel.IsCancellationRequested)
            {
                try
                {
                    // create read task
                    var read = await reader.LoadAsync(20).AsTask(cancel);

                    // get bytes
                    var bytes = new byte[read];
                    reader.ReadBytes(bytes);

                    // get text
                    var text = Encoding.ASCII.GetString(bytes);
                    // var text = reader.ReadString(read);
                    buffer.Append(text);

                    // find latest position
                    PozyxPositionEventArgs position = null;

                    // keep reading results
                    while (buffer.Length > 0)
                    {
                        // keep processing
                        int i = 0;
                        for (; i < buffer.Length; i++)
                        {
                            if (buffer[i] == 10)
                            {
                                continue;
                            }
                            if (buffer[i] == 13)
                            {
                                break;
                            }
                        }

                        // done processing
                        if (i == buffer.Length)
                        {
                            break;
                        }

                        // remove from buffer
                        var line = buffer.ToString(0, i).Trim('\n', '\r');
                        buffer.Remove(0, i + 1);

                        // write line
                        Debug.WriteLine(line);

                        // POS,0x0,11005,12137,1767
                        var items = line.Split(',');
                        if (items.Length != 5)
                        {
                            break;
                        }
                        if (items[0] != "POS")
                        {
                            break;
                        }

                        // parse X, Y, Z
                        int x, y, z;
                        if (!int.TryParse(items[2], out x))
                        {
                            break;
                        }
                        if (!int.TryParse(items[3], out y))
                        {
                            break;
                        }
                        if (!int.TryParse(items[4], out z))
                        {
                            break;
                        }

                        // set new position
                        position = new PozyxPositionEventArgs(x, y, z);
                    }

                    // process if position received
                    if (position != null)
                    {
                        // store new position
                        Position = position;

                        // signal position changed
                        OnPositionChanged(this, position);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);

                    break;
                }
            }
        }