コード例 #1
0
ファイル: ELM327.cs プロジェクト: david-x-chen/obd2net-1
        /// <summary>
        ///     Resets the device, and sets all attributes to unconnected states.
        /// </summary>
        public void Close()
        {
            lock (_syncLock)
            {
                Status   = OBDStatus.NotConnected;
                Protocol = null;

                if (_serialPort != null)
                {
                    Write("ATZ");
                    _serialPort.Close();
                    _serialPort = null;
                }
            }
        }
コード例 #2
0
ファイル: ELM327.cs プロジェクト: david-x-chen/obd2net-1
        private bool ConnectToSerialPort(string portname, int baudrate)
        {
            lock (_syncLock)
            {
                _serialPort = new SerialPort(portname, baudrate);
                var timeout = Convert.ToInt32(Config.Timeout.TotalMilliseconds);
                // ------------- open port -------------
                try
                {
                    _logger.Debug($"Opening serial port '{Config.Portname}'");
                    _serialPort = new SerialPort(Config.Portname, Config.Baudrate, Parity.None)
                    {
                        StopBits     = StopBits.One,
                        ReadTimeout  = timeout,
                        WriteTimeout = timeout
                    };
                    _serialPort.Open();
                    _logger.Debug($"Serial port successfully opened on {Config.Portname}");
                }
                catch (Exception e)
                {
                    Error(e.Message);
                    return(false);
                }

                // ---------------------------- ATZ (reset) ----------------------------
                try
                {
                    Send("ATZ", TimeSpan.FromSeconds(1)); // wait 1 second for ELM to initialize
                }
                catch (Exception e)
                {
                    Error(e.Message);
                    return(false);
                }

                // -------------------------- ATE0 (echo OFF) --------------------------
                var r = Send("ATE0");
                if (!IsOk(r, true))
                {
                    Error("ATE0 did not return 'OK'");
                    return(false);
                }

                // ------------------------- ATH1 (headers ON) -------------------------
                r = Send("ATH1");
                if (!IsOk(r))
                {
                    Error("ATH1 did not return 'OK', or echoing is still ON");
                    return(false);
                }
                // ------------------------ ATL0 (linefeeds OFF) -----------------------
                r = Send("ATL0");
                if (!IsOk(r))
                {
                    Error("ATL0 did not return 'OK'");
                    return(false);
                }

                // by now, we've successfuly communicated with the ELM, but not the car
                Status = OBDStatus.ElmConnected;

                // try to communicate with the car, and load the correct protocol parser
                if (CheckProtocol())
                {
                    Status = OBDStatus.CarConnected;
                    _logger.Info("Connection successful");
                    return(true);
                }

                _logger.Info("Connected to the adapter, but failed to connect to the vehicle");
                return(false);
            }
        }