public void TestIsPidSupported() { const uint SupportedPids = 0xBE1FA813; var actual = ObdSupport.IsPidSupported(SupportedPids, 1, 0); Assert.IsTrue(actual); actual = ObdSupport.IsPidSupported(SupportedPids, 2, 0); Assert.IsFalse(actual); actual = ObdSupport.IsPidSupported(SupportedPids, 8, 0); Assert.IsFalse(actual); actual = ObdSupport.IsPidSupported(SupportedPids, 0x1E, 0); Assert.IsFalse(actual); actual = ObdSupport.IsPidSupported(SupportedPids, 0x1F, 0); Assert.IsTrue(actual); actual = ObdSupport.IsPidSupported(SupportedPids, 0x20, 0); Assert.IsTrue(actual); } // TestIsPidSupported()
} // RefreshSensorData() /// <summary> /// Updates the sensor list. /// </summary> /// <param name="supportedPids">The supported PIDs.</param> private void UpdateSensorList(uint supportedPids) { try { this.listViewSensor.Items.Clear(); ListViewItem item; if (ObdSupport.IsPidSupported(supportedPids, 4, 0)) { item = new ListViewItem("Engine load"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 5, 0)) { item = new ListViewItem("Engine coolant temperature"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if item = new ListViewItem("Battery Voltage"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); if (ObdSupport.IsPidSupported(supportedPids, 0x0c, 0)) { item = new ListViewItem("Engine RPM"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x0d, 0)) { item = new ListViewItem("Vehicle Speed"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x0f, 0)) { item = new ListViewItem("Intake air temperature"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x11, 0)) { item = new ListViewItem("Throttle position"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x1F, 0)) { item = new ListViewItem("Run time since engine start"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x2F, 0x20)) { item = new ListViewItem("Fuel Level Input"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x33, 0x20)) { item = new ListViewItem("Barometric pressure"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x43, 0x40)) { item = new ListViewItem("Absolute load value"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x45, 0x40)) { item = new ListViewItem("Relative throttle position"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x46, 0x40)) { item = new ListViewItem("Ambient air temperature"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x47, 0x40)) { item = new ListViewItem("Absolute throttle position B"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if if (ObdSupport.IsPidSupported(supportedPids, 0x4C, 0x40)) { item = new ListViewItem("Commanded throttle actuator"); item.SubItems.Add("<No Value>"); item.Checked = true; this.listViewSensor.Items.Add(item); } // if } catch (Exception ex) { log.Error("Error updating sensor list", ex); } // catch } // UpdateSensorList()
} // GetStatisticsHeader() /// <summary> /// Refreshes the sensor data. /// </summary> private async void RefreshSensorData() { if (this.obdManager == null) { return; } // if // Torque seems to write only // - Speed, Coolant, Throttle, RPM // => proposed: real time stamp, engine load // NOT: Battery Voltage, Throttle position, Fuel Level Input, Barometric pressure // Absolute load value, Relative throttle position, Absolute throttle position B, // Commanded throttle actuator var watch = new Stopwatch(); watch.Start(); try { var sb = new StringBuilder(200); sb.AppendFormat("{0};", DateTime.Now.ToString("u")); var supportedPids = await this.obdManager.GetSupportedPids20(); if (this.listViewSensor.Items.Count == 0) { this.UpdateSensorList(supportedPids); } // if var itemCount = 0; if ((ObdSupport.IsPidSupported(supportedPids, 4, 0)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetCalculatedEngineLoad(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0}%", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 5, 0)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetEngineCoolantTemperature(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0}°C", val); sb.AppendFormat("{0};", val); } // if itemCount++; if (this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetBatteryVoltage(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0}V", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x0C, 0)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetEngineRpm(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0} rpm", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x0d, 0)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetVehicleSpeed(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0} km/h", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x0f, 0)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetIntakeAirTemperature(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0}°C", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x11, 0)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetThrottlePosition(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0:N}%", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x1f, 0)) && this.listViewSensor.Items[itemCount].Checked) { var totalseconds = await this.obdManager.GetRuntimeSinceEngineStart(); var hours = totalseconds / 3600; var minutes = (totalseconds - (hours * 3600)) / 60; var seconds = totalseconds - (hours * 3600) - (minutes * 60); var time = string.Format( CultureInfo.CurrentCulture, "{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds); this.listViewSensor.Items[itemCount].SubItems[1].Text = time; sb.AppendFormat("{0};", time); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x2f, 0x20)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetFuelLevelInput(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0:N2}%", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x33, 0x20)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetBarometricPressure(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0} kPa", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x43, 0x40)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetAbsoluteLoadValue(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0:N}%", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x45, 0x40)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetRelativeThrottlePosition(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0:N}%", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x46, 0x40)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetAmbientAirTemperature(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0}°C", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x47, 0x40)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetAbsoluteThrottlePositionB(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0:N}%", val); sb.AppendFormat("{0};", val); } // if itemCount++; if ((ObdSupport.IsPidSupported(supportedPids, 0x4C, 0x40)) && this.listViewSensor.Items[itemCount].Checked) { var val = await this.obdManager.GetCommandedThrottleActuator(); this.listViewSensor.Items[itemCount].SubItems[1].Text = string.Format( CultureInfo.CurrentCulture, "{0:N}%", val); sb.AppendFormat("{0};", val); } // if this.lblLastUpdate.Text = DateTime.Now.ToLongTimeString(); if (this.streamwriter != null) { this.WriteToStatisticsFile(sb.ToString()); } // if } catch (Exception ex) { log.Error("Error refreshing sensor data", ex); } // catch watch.Stop(); // cycle time = approx 100ms with simulator ////log.InfoFormat("Cycle time = {0} ms", watch.ElapsedMilliseconds); } // RefreshSensorData()