예제 #1
0
        async Task <bool> TryConnectAsync(string connectionString)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                return(false);
            }

            bool isOpened = false;

            IConnection    con           = null;
            ObdConnection  obdConnection = null;
            IList <string> ecus          = null;

            try
            {
                con = ConnectionFactory.Create(connectionString);

                await con.ConnectAsync();

                if (con.IsConnected)
                {
                    obdConnection = new ObdConnection(con);
                    ecus          = obdConnection.Open();
                    if (ecus != null)
                    {
                        isOpened = true;
                    }
                }
                if (con.IsConnected && isOpened)
                {
                    var list = new List <PID>();

                    foreach (var pidCode in Enum.GetValues(typeof(PIDCode)).Cast <PIDCode>())
                    {
                        if (obdConnection.Command.IsPidSupported(pidCode))
                        {
                            list.Add(PID.GetPID(pidCode));
                        }
                    }

                    AppSettings.Set("SupportedPids", String.Join(", ", list.Select((p) => p.PIDCode)));
                    AppSettings.Set("SupportedPidsCount", list.Count);
                    AppSettings.Set("OBDProtocolDescription", obdConnection.Command.OBDProtocolDescription);
                    AppSettings.Set("OBDVoltage", obdConnection.Command.OBDVoltage);
                    AppSettings.Set("VIN", obdConnection.Command.VIN);
                    AppSettings.Set("FuelType", obdConnection.Command.GetValue(PIDCode.FuelType) as string);
                    AppSettings.Set("ECUs", string.Join(", ", ecus));

                    Refresh();
                }
            }
            catch (ObdException ex)
            {
                throw ex;
            }
            catch (Exception)
            {
                return(false);
            }

            return(con != null && con.IsConnected && isOpened);
        }