コード例 #1
0
        public void Connect()
        {
            if (protocol > 9 || protocol != UnknownProtocol)
            {
                throw new ArgumentOutOfRangeException(protocol.ToString(), "Protocol must be a value between 1 and 9, inclusive.");
            }

            ObdState = new ObdState();
            obdport.Connect();
            FireConnectionChangedEvent(_connected);

            LastResponse = WriteAndCheckResponse("ATZ");  // reset
            LastResponse = WriteAndCheckResponse("ATE0"); // echo off
            LastResponse = WriteAndCheckResponse("ATL0"); // line feeds off

            // no longer allow the ELM's auto detect since we need to know which protocol we're using
            if (protocol == UnknownProtocol || protocol == 0)
            {
                for (protocol = 1; protocol <= 9; protocol++)
                {
                    LastResponse = WriteAndCheckResponse("ATSP" + protocol); // OBD protocol
                    try
                    {
                        LastResponse = WriteAndCheckResponse("01 00");  // send command to initialize comm bus (i.e. get PIDs supported)
                        break;
                    }
                    catch (ObdException)
                    {
                        Trace.WriteLine("It's not protocol " + protocol);
                    }
                }

                if (protocol == 10)
                {
                    throw new ObdException("Could not find compatible protocol. Ensure the cable is securely connected to the OBD port on the vehicle.");
                }
            }
            else
            {
                LastResponse = WriteAndCheckResponse("ATSP" + protocol); // OBD protocol
                LastResponse = WriteAndCheckResponse("01 00");           // send command to initialize comm bus (i.e. get PIDs supported)
            }

            _protocol = protocol;

            LastResponse = WriteAndCheckResponse("ATH1"); // turn on headers (needed for ECU)

            _supportedPids = GetSupportedPids();

            int count = 0;

            foreach (var pidEntry in _supportedPids)
            {
                if (pidEntry.Value.Count > count)
                {
                    _currentEcu = pidEntry.Key;
                    count       = pidEntry.Value.Count;
                }
            }

            Trace.WriteLine("Using ECU " + _currentEcu + " with " + count + " PIDs");

            if (poll)
            {
                _event = new AutoResetEvent(false);

                Thread t = new Thread(PollObd);
                t.IsBackground = true;
                t.Name         = "ObdPoller";
                t.Start();
            }
        }
コード例 #2
0
ファイル: ObdDevice.cs プロジェクト: JJgutierrez/DashBoard
		public void Connect(string comPort, int baud, int protocol, bool poll)
		{
			if(protocol > 9 || protocol != UnknownProtocol)
				throw new ArgumentOutOfRangeException(protocol.ToString(), "Protocol must be a value between 1 and 9, inclusive.");

			ObdState = new ObdState();

			_serial = new SerialPort(comPort, baud);
			_serial.NewLine = ">";		// responses end with the > prompt character
			_serial.Open();

			_errorCount = 0;
			_connected = true;
			FireConnectionChangedEvent(_connected);

			LastResponse = WriteAndCheckResponse("ATZ"); // reset
			LastResponse = WriteAndCheckResponse("ATE0"); // echo off
			LastResponse = WriteAndCheckResponse("ATL0"); // line feeds off

			// no longer allow the ELM's auto detect since we need to know which protocol we're using
			if(protocol == UnknownProtocol || protocol == 0)
			{
				for(protocol = 1; protocol <= 9; protocol++)
				{
					LastResponse = WriteAndCheckResponse("ATSP" + protocol); // OBD protocol
					try
					{
						LastResponse = WriteAndCheckResponse("01 00");	// send command to initialize comm bus (i.e. get PIDs supported)
						break;
					}
					catch(ObdException)
					{
						Trace.WriteLine("It's not protocol " + protocol);
					}
				}

				if(protocol == 10)
					throw new ObdException("Could not find compatible protocol. Ensure the cable is securely connected to the OBD port on the vehicle.");
			}
			else
			{
				LastResponse = WriteAndCheckResponse("ATSP" + protocol); // OBD protocol
				LastResponse = WriteAndCheckResponse("01 00");	// send command to initialize comm bus (i.e. get PIDs supported)
			}

			_protocol = protocol;

			LastResponse = WriteAndCheckResponse("ATH1"); // turn on headers (needed for ECU)

			_supportedPids = GetSupportedPids();

			int count = 0;

			foreach(var pidEntry in _supportedPids)
			{
				if(pidEntry.Value.Count > count)
				{
					_currentEcu = pidEntry.Key;
					count = pidEntry.Value.Count;
				}
			}

			Trace.WriteLine("Using ECU " + _currentEcu + " with " + count + " PIDs");

			if(poll)
			{
				_event = new AutoResetEvent(false);

				Thread t = new Thread(PollObd);
				t.IsBackground = true;
				t.Name = "ObdPoller";
				t.Start();
			}
		}
コード例 #3
0
 public ObdChangedEventArgs(ObdState state)
 {
     ObdState = state;
 }
コード例 #4
0
		public ObdChangedEventArgs(ObdState state)
		{
			ObdState = state;
		}
コード例 #5
0
		private void SetInstrumentClusterValues(ObdState measurement)
		{
			foreach (var item in _skins)
			{
				if(!item.IsVisible)
					continue;

				item.IsMalfunctionVisible = measurement.MilLightOn;

				item.IsLowFuelVisible = item.Fuel < 10;

				item.MPG = measurement.MilesPerGallon;
				item.MPH = measurement.MilesPerHour;
				item.RPM = measurement.Rpm;
				item.Fuel = measurement.FuelLevel;
				item.Temperature = measurement.EngineCoolantTemperature;
			}
		}