/// <summary> /// Get an instance of the connector described by the MetaConnector. /// </summary> /// <param name="metaConnector">Object describing a connector.</param> /// <returns>(IConnector) A connector that will attempt to establish a connection with a Serial Port to which an ELM327 device is attached.</returns> public IConnector GetConnector(MetaConnector metaConnector, ConnectionSettings connectionSettings) { IConnector connector = null; // If this MetaConnector represents a particular port (as opposed to the AutoConnector) if (metaConnector.PortName.Length > 0) { connector = new PortConnector(metaConnector.PortName, connectionSettings); } else { log.Info("Returning AutoConnector"); connector = new AutoConnector(connectionSettings); } return connector; }
/// <summary> /// Create a PortConnector for the specified port. /// </summary> /// <param name="portName">The port to create this connector for.</param> public PortConnector(string portName, ConnectionSettings connectionSettings) { this._portName = portName; this._connectionSettings = connectionSettings; }
/// <summary> /// Stores a reference to the SerialPort with the new connection and notifies listeners. /// </summary> /// <param name="connection">SerialPort with the new connection.</param> public static void ConnectionEstablished(SerialPort connection, ConnectionSettings connectionSettings) { ELM327Connection._singleton._connection = connection; lock (ELM327Connection._singleton._connection) { ELM327Connection._singleton._elm327device = new ELM327(ELM327Connection._singleton._connection, connectionSettings); ELM327Connection._singleton._elm327device.ConnectionLost += ELM327Connection.DestroyConnection; // If we could not start operations, stop everything if (!(ELM327Connection._singleton._elm327device.StartOperations())) { // Log an error log.Error("Error occurred when attempting to start operations on ELM327 device."); // Close connection try { ELM327Connection._singleton._connection.Close(); } catch (IOException e) { log.Error("Error occurred while trying to close connection after a failed attempt to start ELM327 operations.", e); } // Clear everything ELM327Connection._singleton._connection = null; ELM327Connection._singleton._elm327device = null; return; } // If a connection has been successfully established, load our protocol handlers // and notify our listeners that the connection is live and read for communication if (ELM327Connection.ConnectionEstablishedEvent != null) { ELM327Connection._singleton._elm327device.ClearHandlers(); foreach (Type nextHandlerType in _loadedHandlerTypes) { ELM327Connection._singleton._elm327device.AddHandler(nextHandlerType); } ELM327Connection.ConnectionEstablishedEvent(connection); } } }
public AutoConnector(ConnectionSettings connectionSettings) { _connectionSettings = connectionSettings; }
/// <summary> /// Default constructor. /// </summary> public ELM327(SerialPort connection, ConnectionSettings connectionSettings) { // Store the connection _connection = connection; // Store the settings _connectionSettings = connectionSettings; }