Exemple #1
0
        private string GetFriendlyElmConnectionResultTypeName(ElmDriver.ElmConnectionResultType resultType)
        {
            switch (resultType)
            {
            case ElmDriver.ElmConnectionResultType.NoConnectionToElm:
                return("No ELM connection");

            case ElmDriver.ElmConnectionResultType.NoConnectionToObd:
                return("No OBD connection");

            default:
                return("Connected");
            }
        }
        /// <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;
            }
        }