/// <summary>
        /// Listener for the connector's ConnectionEstablished event.
        /// </summary>
        /// <param name="port">The SerialPort object wrapping the first port the connector successfully found an ELM327 on.</param>
        private void Connector_ConnectionEstablished(SerialPort port)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                // Try to connect with ELM327 device for IO operations
                ELM327Connection.ConnectionEstablished(port, ConstructConnectionSettings());

                if (ELM327Connection.InOperation)
                {
                    if (_curPortConnectionStatus.Status.Length > 0)
                    {
                        _curPortConnectionStatus.Status += ", ";
                    }

                    _curPortConnectionStatus.Status += "Successful IO Operations!";
                    _curPortConnectionStatus.Success = true;
                }
                else
                {
                    if (_curPortConnectionStatus.Status.Length > 0)
                    {
                        _curPortConnectionStatus.Status += ", ";
                    }

                    _curPortConnectionStatus.Status += "Failed to Start IO Operations!";
                    _curPortConnectionStatus.Success = false;
                }
            }
                                         ));
        }
        /// <summary>
        /// Initiates the connection process.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnOk_Click(object sender, RoutedEventArgs e)
        {
            // Clear the port connection status list
            _portConnectionStatusList.Clear();
            _curPortConnectionStatus = null;

            // Get the correct Connector
            IConnector connector = _conFactory.GetConnector(SelectedConnector, ConstructConnectionSettings());

            // Hook into the connector's events
            connector.CheckingPort          += Connector_NextPort;
            connector.UpdateMessages        += Connector_UpdateMessage;
            connector.ConnectionComplete    += Connector_Complete;
            connector.ConnectionEstablished += Connector_ConnectionEstablished;
            connector.PortSuccess           += Connector_PortSuccess;

            // If this application is already connected on a SerialPort, close the connection
            if (ELM327Connection.Connection != null && ELM327Connection.Connection.IsOpen)
            {
                lblStatus.Content = "Closing existing ELM327 connection on port " + ELM327Connection.Connection.PortName + "...";
                log.Info("Closing existing ELM327 connection on port [" + ELM327Connection.Connection.PortName + "].");
                ELM327Connection.DestroyConnection();
            }

            // Log
            log.Info("Spawning thread for finding an Elm327 port and connecting.");

            // Disable Controls
            btnOk.IsEnabled       = false;
            cmbPortList.IsEnabled = false;
            btnCancel.Content     = "Cancel";

            // Start our Progress Bar
            pbProgressBar.IsIndeterminate = true;

            // Update the Status
            lblStatus.Content = "Connecting...";

            // Store a reference to the new IConnector
            _runningConnector = connector;

            // Spawn a thread
            _getSerialPortThread = new Thread(new ThreadStart(connector.GetSerialPort));
            _getSerialPortThread.SetApartmentState(ApartmentState.STA);
            _getSerialPortThread.Start();
        }
Exemple #3
0
 private void OnApplicationExit(object sender, ExitEventArgs e)
 {
     ApplicationSettings.Default.Save();
     ELM327Connection.DestroyConnection();
 }
 /// <summary>
 /// Handles clean-up tasks before closing.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void _mainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     // Alerts any running threads to exit.
     Variables.ShouldClose = true;
     ELM327Connection.DestroyConnection();
 }
 /// <summary>
 /// Closes a connection to an ELM327.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void FileMenu_Disconnect_Click(object sender, RoutedEventArgs e)
 {
     ELM327Connection.DestroyConnection();
 }