Open mariapazosjimenez opened 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 */ });`
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
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
SerialConnection connection = new SerialConnection("COM3");
// Se crea el objeto ELM327
ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug));
List
foreach (var data in receibedData) { Console.WriteLine(data); }
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
SerialConnection connection = new SerialConnection("COM3");
// Se crea el objeto ELM327
ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug));
List
foreach (var data in receibedData) { Console.WriteLine(data); }
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.
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:
Now, let's suppose that despite being able to diagnose these three parameters, this user only wants to diagnose the EngineRPM parameter:
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
I hope I have explained myself
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
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); }
//
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();
}
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?