private void GetState() { _state = new ObdState(new List <EcuResponse>()); if (!_targetPids.Any()) { BuildPidList(); } try { foreach (var pid in _targetPids) { var pidVal = _port.SendCommandWaitForString(pid.PidCommand); var parsedResult = PidDecoder.ParsePidCmd(pidVal, _protocol); //raw command - returns ECU dict key, byte array of values, strips ECU header //build the EcuResponse and add it to state foreach (var result in parsedResult) //get each ECU's response. likely only a single on for most calls, I'd assume. { var ecuResponse = new EcuResponse(pid); ecuResponse.ECU = result.Key.ToString(); ecuResponse.ReturnedRawData = result.Value; _state._ecuResponses.Add(ecuResponse); } } } catch (Exception ex) { Logger.DebugWrite($"{ex.Message}: at {ex.StackTrace}"); } }
/// <summary> /// Startup method for asking the ECU for supported PIDs. /// </summary> private void GetSupportedPids() { var pidRequestRanges = new List <string>() { "00", "20", "40" }; var ecus = new List <ObdEcu>(); foreach (var chunk in pidRequestRanges) { var pidGetResult = PidDecoder.ParsePidCmd(_port.SendCommandWaitForString(new ObdPid() { Mode = "01", Pid = chunk }.PidCommand), _protocol).ToList(); foreach (var ecuLineResponse in pidGetResult) { var ecu = new ObdEcu(); var ecuExists = ecus.Any(x => x.Id == ecuLineResponse.Key); if (ecuExists) { ecu = ecus.Single(x => x.Id == ecuLineResponse.Key); } else { ecu.Id = ecuLineResponse.Key; } var pidz = PidDecoder.DecodeSupportedPids(ecuLineResponse.Value, int.Parse(chunk)).Select(x => new ObdPid() { Mode = "01", Pid = x.ToString("X2") }).ToList(); if (ecu.SupportedPids?.Any() ?? false) { ecu.SupportedPids.AddRange(pidz); } else { ecu.SupportedPids = pidz; } if (!ecuExists) { ecus.Add(ecu); } } } _ecus = ecus; }