Esempio n. 1
0
        /// <summary>
        /// Connect to the EV3 brick.
        /// </summary>
        /// <returns></returns>
        public Task ConnectAsync()
        {
            FindEv3();

            _tokenSource = new CancellationTokenSource();

            Task t = Task.Factory.StartNew(async() =>
            {
                while (!_tokenSource.IsCancellationRequested)
                {
                    // if the stream is valid and ready
                    if (_stream == null || !_stream.CanRead)
                    {
                        continue;
                    }

                    await _stream.ReadAsync(_inputReport, 0, _inputReport.Length, _tokenSource.Token);

                    var size = (short)(_inputReport[1] | _inputReport[2] << 8);
                    if (size == 0)
                    {
                        return;
                    }

                    var report = new byte[size];
                    Array.Copy(_inputReport, 3, report, 0, size);
                    ReportReceived?.Invoke(this, new ReportReceivedEventArgs {
                        Report = report
                    });
                }
            }, _tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);

            return(Task.FromResult(true));
        }
Esempio n. 2
0
        void OnReport(HidReport report)
        {
            ReportReceived?.Invoke(report);

            if (Device.IsConnected && Device.IsOpen)
            {
                Device.ReadReport(OnReport);
            }
        }
        public void Start()
        {
            string input = string.Empty;

            //Print();
            while (input.Length == 0)
            {
                GarminDevice baseStation = null;

                var list = GarminDevice.DiscoverDevices();
                list.ForEach(f =>
                {
                    var reader = new GarminReader(f);
                    var info   = reader.ReadInfo();
                    Console.WriteLine(info.Id + ": " + info.Description);

                    if (baseStation == null && info.SupportedProtocols != null && info.SupportedProtocols.Any(p => p.tag == (byte)'A' && p.data == 1100))
                    {
                        baseStation = f;
                    }
                    else
                    {
                        f.Dispose();
                    }
                });


                if (baseStation != null)
                {
                    var reader = new GarminReader(baseStation);

                    while (true)
                    {
                        try
                        {
                            var packet = reader.WaitForPacket(3078);
                            Update(new TrackedAsset(packet.data));
                            ReportReceived?.Invoke(this, new ReportReceivedEventArgs(new TrackedAsset(packet.data)));
                        }
                        catch (Exception e)
                        {
                            LogException(e.Message);
                        }
                    }
                }

                Console.WriteLine("Found " + list.Count + " devices. Enter to go again.");
                input = Console.ReadLine();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Connect to the EV3 brick.
        /// </summary>
        /// <returns></returns>
        public async Task ConnectAsync()
        {
            _client = new TcpClient();
            await _client.ConnectAsync(_address, 5555);

            _stream = _client.GetStream();

            // unlock the brick (doesn't actually need serial number?)
            var buff = Encoding.UTF8.GetBytes(UnlockCommand);
            await _stream.WriteAsync(buff, 0, buff.Length);

            // read the "Accept:EV340\r\n\r\n" response
            var read = await _stream.ReadAsync(buff, 0, buff.Length);

            var response = Encoding.UTF8.GetString(buff, 0, read);

            if (string.IsNullOrEmpty(response))
            {
                throw new Exception("LEGO EV3 brick did not respond to the unlock command.");
            }

            _tokenSource = new CancellationTokenSource();

            Task t = Task.Factory.StartNew(async() =>
            {
                while (!_tokenSource.IsCancellationRequested)
                {
                    // if the stream is valid and ready
                    if (_stream == null || !_stream.CanRead)
                    {
                        continue;
                    }

                    await _stream.ReadAsync(_sizeBuffer, 0, _sizeBuffer.Length);

                    var size = (short)(_sizeBuffer[0] | _sizeBuffer[1] << 8);
                    if (size == 0)
                    {
                        return;
                    }

                    var report = new byte[size];
                    await _stream.ReadAsync(report, 0, report.Length);
                    ReportReceived?.Invoke(this, new ReportReceivedEventArgs {
                        Report = report
                    });
                }
            }, _tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);
        }
        private void ReadReportCallback(IAsyncResult ar)
        {
            var stream = (HidStream)ar.AsyncState;

            try
            {
                var res  = stream.EndRead(ar);
                var data = new byte[res];
                Array.Copy(readReportBuffer, 0, data, 0, res);
                ReportReceived?.Invoke(this, new ReportReceivedEventArgs(data));
            }
            catch (Exception ex) when(IsConnectionError(ex))
            {
                Debug.WriteLine($"EXCEPTION: ({ex.GetType()}) {ex.Message}");
                DisposeConnection();
                return;
            }

            BeginWaitRead(stream);
        }
Esempio n. 6
0
        public async Task ConnectAsync()
        {
            try
            {
                _socket = SelectedDevice.CreateRfcommSocketToServiceRecord(_myUuid);
                await _socket.ConnectAsync();

                await Task.Run(() =>
                {
                    var reader = new BinaryReader(_socket.InputStream);

                    while (true)
                    {
                        try
                        {
                            int size = reader.ReadInt16();
                            var b    = reader.ReadBytes(size);

                            ReportReceived?.Invoke(this, new ReportReceivedEventArgs {
                                Report = b
                            });
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            break;
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Disconnect();
                Console.WriteLine(ex.Message);
            }
        }