Пример #1
0
        private void StopWorker()
        {
            try
            {
                IsDeviceOpen = false;
                StopTickers().Wait();   // triggers tokenSource.Cancel()
            }
            catch (AggregateException exc)
            {
                Debug.WriteLine("Error: StopWorker(): AggregateException - " + exc);
            }
            catch (CommunicationException exc)
            {
                Debug.WriteLine("Error: StopWorker(): CommunicationException - " + exc);
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error: StopWorker(): " + exc);
            }

            if (shorty != null)
            {
                shorty.Dispose();
                shorty = null;
            }

            if (httpServer != null)
                httpServer.robot = shorty;

            isWorkerRunning = false;
        }
Пример #2
0
        private async Task<bool> doOpenDevice(string deviceId)
        {
            try
            {
                // start the worker process:
                await StartWorker();

                if (!isWorkerRunning || shorty == null || shorty.isCommError)
                {
                    Speak("Hardware brick does not connect");
                    if (shorty != null)
                    {
                        shorty.Close();
                        shorty = null;
                    }
                }
                else
                {
                    // shorty is connected to hardware brick.
                }

                if (httpServer != null)
                    httpServer.robot = shorty;

                return true;
            }
            catch (Exception exc)
            {
                Speak("could not connect");
            }
            return false;
        }
Пример #3
0
        private async Task StartWorker()
        {
            try
            {
                IsDeviceOpen = false;

                tokenSource = new CancellationTokenSource();
                isWorkerRunning = true;

                shorty = new ShortyTheRobot(this, joystick, desiredLoopTimeMs);
                await shorty.Init(tokenSource, new string[] { currentSerialPort });  // may throw exceptions

                if (shorty.isCommError)
                {
                    isWorkerRunning = false;
                }
                else
                {
                    await InitializeTickers();
                    LastConnectionTime = DateTime.Now;
                    IsDeviceOpen = true;
                }
            }
            catch (AggregateException exc)
            {
                isWorkerRunning = false;
                Debug.WriteLine("Error: StartWorker(): AggregateException - " + exc);
                throw;
            }
            catch (CommunicationException exc)
            {
                isWorkerRunning = false;
                Debug.WriteLine("Error: StartWorker(): CommunicationException - " + exc);
                throw;
            }
            catch (Exception exc)
            {
                isWorkerRunning = false;
                Debug.WriteLine("Error: StartWorker(): " + exc);
                throw;
            }
        }
Пример #4
0
        /// <summary>
        /// click on the button that selects the serial port and then starts backround worker, thus starting the robot.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void OpenCloseButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenCloseButton.IsEnabled = false;

                if (isWorkerRunning)
                {
                    Speak("disconnecting");
                    await Task.Delay(100);

                    // cancel the worker process, dispose of shorty - but don't wait for it:
                    Task task = Task.Factory.StartNew(StopWorker);
                    await Task.Delay(1000);

                    ResetOpenCloseButton("");
                }
                else
                {
                    currentSerialPort = "" + SerialPortComboBox.SelectedValue;

                    if (!string.IsNullOrWhiteSpace(currentSerialPort))
                    {
                        Speak("connecting");
                        await Task.Delay(1000);

                        // start the worker process:
                        await StartWorker();

                        if (!isWorkerRunning || shorty.isCommError)
                        {
                            StatusLabel.Text = shorty.ToString() + " cannot connect to hardware brick";
                            ResetOpenCloseButton("");
                            Speak("Hardware brick does not connect");
                            if (shorty != null)
                            {
                                shorty.Close();
                                shorty = null;
                            }
                        }
                        else
                        {
                            // shorty is connected to hardware brick.
                            SerialPortComboBox.IsEnabled = false;
                            OpenCloseButton.Content = "Close";
                            EnableOpenCloseButton("");
                        }

                        if (httpServer != null)
                            httpServer.robot = shorty;
                    }
                    else
                    {
                        Speak("Select serial port");
                        StatusLabel.Text = "Please select serial port to connect to " + shorty + " hardware brick";
                        ResetOpenCloseButton("");
                    }
                }
            }
            catch (Exception exc)
            {
                Speak("Oops");
                StatusLabel.Text = exc.Message;
                Debug.WriteLine("Exception: " + exc);
                ResetOpenCloseButton("");
                shorty = null;
                if (httpServer != null)
                    httpServer.robot = shorty;
            }
        }