Class representing a serial connection that can be used by firmata
예제 #1
0
        /// <summary>
        /// Connects to a remote device over firmata
        /// </summary>
        /// <param name="selectedConnection">Connection to connect to </param>
        /// <returns>true if connection succeeded.</returns>
        public async Task Connect(Connection selectedConnection)
        {
            if (this.CurrentConnection != null)
            {
                App.SerialStream.end();
                this.Disconnect();
                await Task.Delay(1000);
            }

            try
            {
                await this.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    App.CurrentAppSettings.CurrentConnectionState = (int)ConnectionState.Connecting;
                });

                this.CurrentConnection = selectedConnection;

                switch (this.CurrentConnection.ConnectionType)
                {
                    case ConnectionType.BluetoothSerial:
                        App.SerialStream = new BluetoothSerial(selectedConnection.Source);
                        break;
                    case ConnectionType.UsbSerial:
                        App.SerialStream = new UsbSerial(selectedConnection.Source);
                        break;
                }

                App.Firmata = new UwpFirmata();
                App.Firmata.FirmataConnectionFailed += this.OnConnectionFailed;
                App.Firmata.FirmataConnectionLost += this.OnConnectionLost;
                App.Firmata.FirmataConnectionReady += this.OnConnectionEstablished;
                App.SerialStream.begin(115200, SerialConfig.SERIAL_8N1);
                App.Firmata.begin(App.SerialStream);
                App.Firmata.startListening();
                App.Arduino = new RemoteDevice(App.Firmata);
            }
            catch (Exception e)
            {
                await this.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    App.CurrentAppSettings.CurrentConnectionState = (int)ConnectionState.CouldNotConnect;
                    Debug.WriteLine("{0} hit while connecting", e.StackTrace);
                });
            }
        }
예제 #2
0
        /// <summary>
        /// Disconnects the current connection and cleans up any related objects
        /// </summary>
        public async void Disconnect()
        {
            if (this.CurrentConnection != null)
            {
                await this.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    App.CurrentAppSettings.CurrentConnectionState = (int)ConnectionState.Disconnecting;
                });

                if (App.SerialStream != null)
                {
                    App.SerialStream.end();
                }

                App.SerialStream = null;
                App.Firmata = null;
                App.Arduino = null;

                this.CurrentConnection = null;

                await this.dispatcher.RunAsync(
                    CoreDispatcherPriority.Normal,
                    () =>
                    {
                        App.CurrentAppSettings.CurrentConnectionState = (int)ConnectionState.NotConnected;
                    });
            }
        }