/// <summary>Attempts to connect to the ECU.</summary> /// <param name="protocolType">The protocal type to use.</param> /// <param name="measurementUnitType">The measurement type to use.</param> public void Connect(ElmDriver.ElmObdProtocolType protocolType, ElmDriver.ElmMeasuringUnitType measurementUnitType) { this.elm = new ElmDriver(this.serialPortName, protocolType, measurementUnitType); switch (elm.Connect()) { case ElmDriver.ElmConnectionResultType.NoConnectionToElm: throw new InvalidOperationException("Failed to connect to the ELM327."); case ElmDriver.ElmConnectionResultType.NoConnectionToObd: throw new InvalidOperationException("Failed to connect to the vehicle's ECU."); case ElmDriver.ElmConnectionResultType.Connected: this.connected = true; this.protocalType = this.GetFriendlyObdProtocolModeTypeName(this.elm.ProtocolType); this.fuelType = this.GetFriendlyFuelTypeName(this.elm.ObdMode01.FuelType); this.vin = this.elm.ObdMode09.VehicleIdentificationNumber; break; } }
/// <summary> /// Attempts to connect to the ECU. If successful, will retrieve one time information from the ECU. /// </summary> public void Connect(ElmDriver.ElmObdProtocolType protocolType = ElmDriver.ElmObdProtocolType.Automatic, ElmDriver.ElmMeasuringUnitType measurementUnitType = ElmDriver.ElmMeasuringUnitType.English) { elm = new ElmDriver(serialPortName, protocolType, measurementUnitType); ElmDriver.ElmConnectionResultType connectionResult = elm.Connect(); if (connectionResult == ElmDriver.ElmConnectionResultType.NoConnectionToElm) { throw new Exception("Failed to connect to the ELM327"); } else if (connectionResult == ElmDriver.ElmConnectionResultType.NoConnectionToObd) { throw new Exception("Failed to connect to the vehicle's ECU"); } else { connected = true; // Get the currently used OBD protocol type OBDProtocolType = GetFriendlyObdProtocolModeTypeName(elm.ProtocolType); VehicleFuelType = GetFriendlyFuelTypeName(elm.ObdMode01.FuelType); VIN = elm.ObdMode09.VehicleIdentificationNumber; } }