DarthAffe / OBD.NET

C#-Library to read data from car through an ELM327-/STN1170-Adapter
GNU General Public License v2.0
185 stars 83 forks source link

Use of the library in a generic way #34

Open mariapazosjimenez opened 2 weeks ago

mariapazosjimenez commented 2 weeks ago

Hi, I'm trying to make a self-diagnosis application for myself. The objective of my application is to provide a table to the user with all the parameters available to analyze and so that he can select which ones are of interest. I had thought about using the library like this: (in the example string signal would be the data selected by the user, which would have a PID associated with it)

using SerialConnection connection = new SerialConnection("COM3"); // Se crea el objeto ELM327 using ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug));

// Se crea una lista con los posibles datos que se pueden diagnosticar List OBDparameters = new List { new RealOBDParam(typeof(EngineCoolantTemperature),"05"), new RealOBDParam(typeof(EngineRPM),"0C"), new RealOBDParam(typeof(VehicleSpeed),"0D") }

// Parametro seleccionado por el usuario string signal = "0C" // Buscar el parametro seleccionado por el usuario en la lista foreach (var param in this.OBDparameters) { // RealTime if (param.ParameterID == signal) { // Suscribirse a la acción correspondiente dev.SubscribeDataReceived((sender, data) => Console.WriteLine("Engine RPM: " + data.Data.Rpm)); dev.RequestData();

    break;
}

}

public class RealOBDParam { public Type ParameterType { get; set; } // Para guardar el tipo (como EngineRPM, VehicleSpeed, etc.) public string ParameterID { get; set; } // Para guardar el nombre/descripción del parámetro // Constructor para inicializar ambos campos public RealOBDParam(Type parameterType, string parameterID) { this.ParameterType = parameterType; this.ParameterID = parameterID; } }

Using the library in this way is not correct, can someone help me?

DarthAffe commented 2 weeks ago

You can't use variables for generics.

I'm not 100% sure if I correctly understood what you're trying to do, but if you don't know which value you are selecting don't use the generic RequestData, but the one where you can directly pass the PID.

For example:

byte pid = 0x0D;
dev.RequestData(pid);

For that you have to subscribe to the interface-data-received (as in the example) and in there handle the data depending on your use case.

dev.SubscribeDataReceived<IOBDData>((_, data) =>{ /* Your logic here */ });`
mariapazosjimenez commented 2 weeks ago

Okey, thank you ! that is what I was searching for. I have followed your advice and my code now looks like this but the event 'dev.SubscribeDataReceived((sender, data) 'never runs

using System; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Threading; //using OBD; using OBD.NET; using OBD.NET.Communication; using OBD.NET.Devices; using OBD.NET.Events; using OBD.NET.Logging; using OBD.NET.OBDData; using static OBD.NET.Devices.ELM327; using System.Runtime.CompilerServices; using OBD.NET.Extensions; using static System.Runtime.InteropServices.JavaScript.JSType;

List receibedData = new List();

SerialConnection connection = new SerialConnection("COM3"); // Se crea el objeto ELM327 ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug)); List pidList = new List(); pidList.Add(0x0D); pidList.Add(0x0B); pidList.Add(0x0C); pidList.Add(0x1F); // Suscribirse a la recepción de datos antes de inicializar el dispositivo dev.SubscribeDataReceived((sender, data) => { // Agregar datos a la lista receibedData.Add(data.Data.PID.ToHexString() + ";" + data.Data); Console.WriteLine("Data received: " + data.Data.PID.ToHexString()); // Log para ver si se reciben datos }); dev.Initialize(); for (int i = 0; i <10; i++) { foreach (var pid in pidList) { dev.RequestData(pid); Thread.Sleep(100); } Thread.Sleep(100); }

foreach (var data in receibedData) { Console.WriteLine(data); }

mariapazosjimenez commented 2 weeks ago

The general event is only executed if you also request a particular data. For example, I have requested speed, well now that event will be executed (with the speed data obviously) but if there is no particular data it will not be executed

List receibedData = new List();

SerialConnection connection = new SerialConnection("COM3"); // Se crea el objeto ELM327 ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug)); List pidList = new List(); pidList.Add(0x0D); pidList.Add(0x0B); pidList.Add(0x0C); pidList.Add(0x1F); // Suscribirse a la recepción de datos antes de inicializar el dispositivo dev.SubscribeDataReceived((sender, data) => Console.WriteLine("Vehicle speed: " + data.Data.Speed)); dev.SubscribeDataReceived((_, data) => { // Agregar datos a la lista receibedData.Add(data.Data.PID.ToHexString() + ";" + data.Data); Console.WriteLine("Data received: " + data.Data.PID.ToHexString()); // Log para ver si se reciben datos }); dev.Initialize(); for (int i = 0; i <10; i++) { foreach (var pid in pidList) { dev.RequestData(); dev.RequestData(pid); Thread.Sleep(100); } Thread.Sleep(100); }

foreach (var data in receibedData) { Console.WriteLine(data); }

DarthAffe commented 2 weeks ago

I've no idea what you're trying to do. The code you added should not compile like this. If you really want to process data from outside the obd standard you need to implement it yourself using the RawDataReceived event.

mariapazosjimenez commented 1 week ago

I will try to explain myself as best as possible. My goal is to make a diagnostic application that offers parameters that can be analyzed with the OBD standard. The user does not have to analyze all the parameters that the application offers, there may be the case in which he only wants to analyze a couple of parameters. Below, I illustrate this idea with an example. In this image we can see the parameter options that are given to the user to analyze. The user could choose 1, 2 or 3 parameters, but this is not known: diagnosisParametersOptions

Now, let's suppose that despite being able to diagnose these three parameters, this user only wants to diagnose the EngineRPM parameter: diagnosisSelectedParam

The application is already started and the user has chosen a parameter, in this example, EngineRPM. What should my code do in this case? Well, it will have to detect which parameter is the one that the user has selected. Once this parameter is detected, for example through the PID, I will have to do a SubscribeDataReceived and RequestData() ¿no? When the application starts, it does not know which parameters are subscribed and requested, it is something that occurs at run time according to the user's actions.

I hope I have explained myself

mariapazosjimenez commented 1 week ago

I would like to request data by pid instead of the OBD.NET.Devices.ELM327.RequestData but I don't know how to use the event that is generated in this case when the response is received

mariapazosjimenez commented 1 week ago

I have finally managed to receive data with this simple code. Thank you very much for your suggestions, they have really helped me a lot. // SerialConnection connection = new SerialConnection("COM3"); // Se crea el objeto ELM327 ELM327 elm327Device = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.None)); elm327Device.Initialize(); // Suponiendo que el usuario selecciona el PID de interés byte userSelectedPid = 0x0C; // Método que recibe el PID seleccionado

elm327Device.RawDataReceived += (sender, args) => { // Aquí manejas los datos crudos recibidos string rawData = args.Data; // Datos crudos recibidos Console.WriteLine(rawData); DateTime timestamp = args.Timestamp; // Timestamp de los datos };

// Envía la solicitud de datos al dispositivo para el PID seleccionado while (true) { elm327Device.RequestData(0x0C); Thread.Sleep(10); }

//