Coder Social home page Coder Social logo

obd.net's Introduction

OBD.NET

C#-Library to read/write data from/to a car through an ELM327-/STN1170-Adapter

Projects

  • OBD.NET - OBD-II implementation in .NET 6/5 and .NET Framework 4.8
  • ConsoleClient - Example client application using SerialConnection, running with .NET 6

Usage

  • Add the OBD.NET package to project
public class Program
{
    public static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.WriteLine("Parameter ComPort needed.");

            IEnumerable<string> availablePorts = SerialConnection.GetAvailablePorts();

            Console.WriteLine("\nAvailable ports:");

            foreach (string port in availablePorts)
            {
                Console.WriteLine(port);
            }

            return;
        }

        string comPort = args[0];

        using SerialConnection connection = new SerialConnection(comPort);
        using ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug));

        dev.SubscribeDataReceived<EngineRPM>((sender, data) => Console.WriteLine("EngineRPM: " + data.Data.Rpm));
        dev.SubscribeDataReceived<VehicleSpeed>((sender, data) => Console.WriteLine("VehicleSpeed: " + data.Data));

        dev.SubscribeDataReceived<IOBDData>((sender, data) => Console.WriteLine($"PID {data.Data.PID.ToHexString()}: {data.Data}"));

        dev.Initialize();
        dev.RequestData<FuelType>();

        for (int i = 0; i < 5; i++)
        {
            dev.RequestData<EngineRPM>();
            dev.RequestData<VehicleSpeed>();
            Thread.Sleep(1000);
        }

        Console.ReadLine();

        //Async example
        // MainAsync(comPort).Wait();

        //Console.ReadLine();
    }

    /// <summary>
    /// Async example using new RequestDataAsync
    /// </summary>
    /// <param name="comPort">The COM port.</param>
    /// <returns></returns>
    public static async Task MainAsync(string comPort)
    {
        using SerialConnection connection = new SerialConnection(comPort);
        using ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug));

        dev.Initialize();

        EngineRPM engineRpm = await dev.RequestDataAsync<EngineRPM>();
        Console.WriteLine("Data: " + engineRpm.Rpm);

        engineRpm = await dev.RequestDataAsync<EngineRPM>();
        Console.WriteLine("Data: " + engineRpm.Rpm);

        VehicleSpeed vehicleSpeed = await dev.RequestDataAsync<VehicleSpeed>();
        Console.WriteLine("Data: " + vehicleSpeed.Speed);

        engineRpm = await dev.RequestDataAsync<EngineRPM>();
        Console.WriteLine("Data: " + engineRpm.Rpm);
    }
}

obd.net's People

Contributors

darthaffe avatar dependabot[bot] avatar derskythe avatar julian-baumann avatar mb512 avatar romanlum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

obd.net's Issues

C#6 compatibility

Hello,

This is a really good library for OBDII, I would like to use it linked with Unity. Unfortunately, Unity is not compatible with C#7 but only C#6, and your code use some C#7 features.

I'm just a beginner in C#, do you thing it is easy to modify your code in order to be compatible with C#6 ?

Regards,
Ghis

Loss of communication hangs obd.net

I've developed a simple OBD scanner to deliver data to HTML5 pages.

  1. If you call the obd.net initialize method and a device is not present the initialize call will return a run time error - Request Thread timeout. Is there a better method than waiting for a run time error ?

I have a watchdog that sends a data request command every sec and expects a reply back within 5 sec.
If a reply is not received within 5 sec the scanner is paused so that it will not request any additional data.

If the OBD device is turned off while the scanner is running the watchdog will timeout and pause the scanner.

An attempt to dispose of the obd.net object and the serial connection object hangs on the obd.net.Dispose(). This eventually causes an http request timeout. The dispose never returns.

Nothing is logged in the windows event log or the debugger log.

Any ideas on how to make sure that if a device connection is lost there is means to clean things up.

Since this is service it will continue to try to make a connection with the device.

  • Since I can't clean up the existing objects there is no means to continue attempts to re-establish the connection

OBD Port Issue

Hi Guys,

I cant put finger on the issue. because my code was running all okay. which suddenly started giving timeout error. Code is not able to pick up port and run commands. which was working properly earlier.

Initially we could not see port number against bluetooth.

ECCB901C

Supported Pids

Thank you for making this library.
When testing it i came across a bug, the Supported pids showed too many pids. I'm not very familiar with bit shifting, but in my view you only shift a bit and check if it's not zero.
Example: if ((A << (7 - i)) != 0) i think you miss selecting the bit for checking.

I will make a pull request with a solution, it's not a solution with bit shifting.

Move EnhancedSerialPort and SerialConnection out of OBD.Desktop

Hey there,

I'm using this library to read out OBD data with a Raspberry Pi on Linux.

Short Story: It works!

Long Story: There is no need for the Serial-related classes to be encapsulated in a Windows-Only .NET Framework Namespace, so why keep it there?

In order to make it work, had to manually copy the files to my own namespace and comment out the following lines:

if (!IsWindows)
{
    FieldInfo fieldInfo = BaseStream.GetType().GetField("fd", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null) throw new NotSupportedException("Unable to initialize SerialPort - 'fd'-field is missing.");
    _fd = (int)fieldInfo.GetValue(BaseStream);

    _disposedFieldInfo = BaseStream.GetType().GetField("disposed", BindingFlags.Instance | BindingFlags.NonPublic);
    if (_disposedFieldInfo == null) throw new NotSupportedException("Unable to initialize SerialPort - 'disposed'-field is missing.");

    fieldInfo = typeof(SerialPort).GetField("data_received", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null) throw new NotSupportedException("Unable to initialize SerialPort - 'data_received'-field is missing.");
    _dataReceived = fieldInfo.GetValue(this);

    _thread = new Thread(EventThreadFunction);
    _thread.Start();
}

Since it threw exceptions.

Under .NET Core or .NET 5+, Serialports work cross-platform with the System.IO.Ports package. Maybe it's time to move this out of the Desktop package?

PID 01 For MIL lamp status

I cant seem to find a method for getting the MIL lamp status, what i mean by this is i want to get to see is it on or off. If someone could reply i would be very gretful.

Reading MIL Status & DTCs

Really liking this library and wanted to created a project with it. But it is missing a very crucial feature: reading Diagnostic Trouble Codes. Do you have any plans on implementing them in the near future?

ATSH Command

Hi,

Is there functionality in this library to change the header with AT command ATSH?

J1939 Parser

Hi, I have a modified version of your driver working with an ODB2 MX+ reading data from a Ford F53 RV Chassis. I added commands to read the vehicle information and DTCs.

We switched from a Gasoline RV to a Diesel RV sitting on a Freightliner chassis with a Cummins diesel. It uses J1939. The MX+ module supports ELM 327 / J1939 but now I need to develop a parser.

Have you done any experimenting with J1939 ?

Adding a new PID, struggling...., c# newbie

I have created a new class for Odometer.
It needs a PID value of 3B6 (CAN message: 3B64597BF00). The whole technical architecture seems to be made around a PID of one byte so I need somehow to make AbstractOBDData.cs aware of this larger PID, but I fail because my knowledge of c# is not enough.
Can anyone give a pointer for this?
And I think I also need to convince AbstractPidsSupported.cs of this larger PID and adding it to the supported list.
I want to use this library to get Odometer from my Tesla Model 3 Vehicle Bus through the OBDLINK MX+.

`using OBD.NET.Common.DataTypes;

namespace OBD.NET.Common.OBDData
{
public class Odometer : AbstractOBDData
{
#region Properties & Fields

    public Kilometre Odom => new Kilometre((D + C << 8) + (B << 16) + (A << 24), 0, 0);

    #endregion

    #region Constructors

    public Odometer()
        : base(0x3B6, 4)
    { }

    #endregion

    #region Methods

    public override string ToString() => Odom.ToString();

    #endregion
}

}
`

Parameter ComPort needed

when i run this program it gives message "Parameter ComPort needed". where should i pass comp port my com port is 5.

public static void Main(string[] args )
{

         if (args.Length < 1)
           {
              Console.WriteLine("Parameter ComPort needed.");
            
               return;
           }

Set Service Mode

I need to request the Vehicle Information using Service 09. The SendCommand function references a static mode 01. I need the retrieve the VIN from service 09. 09 02

You have a TODO to implement different modes in ELM327 in the Mode declaration.
What were your thoughts on setting a different service mode ?

I'm going to create new RequestData function.
RequestData(service,pid) where the service would be set against the values specified in the Mode declaration.

pid data types should have another member to set the service code.

I'll need to create a data type for the VIN to return the string. 17 bytes.

I'll let your know how it works.

project error

I am running visual studio 2017 ,I get this message below when trying to run the program, how do i solve this issue?
thank you
a project with an out put type of class library cannot be started directly.
in order to debug this project add an executable project to this solution witch references the library project set the executable project as the startup project

WebService Issue

I created a simple .net WebService API to retrieve PID values to display on a page.

I confirmed that my OBD2 module is working via the example Client.

When I make a call to the requestDataAsync<> function it sends the data to the module and receives the hex response. At that point it appears to hang / timeout. Eventually the call completes but by that time the web service call has timed out and returns an error to the Page.

The web service method is using await when it calls the requestDataAsync function.

Any ideas?

Establishing connection

Hi there, thanks for the library. I have a question which might be really dumb but if I implement this into a unity project, do I need to implement the connection with my own code before connecting OBD.NET to that comport or is that all done in your library? I can't seem to connect my app to the obd even though other apps work. In the unity project the script to connect is mostly taken from the example console program you provided. Am I missing something? Thanks.

Extracting Vehicle Identification Number

Hi,

Does anybody know how to extract a vehicles identification number (VIN) using the ODB.NET library?

What I have managed to figure out is that it is under mode 9, PID 2. Command 0902.
However not sure how to setup a reference class to extract the VIN details.

Thanks,

Rob

Trying on Tesla Model3

Anyone here have a working sample with Tesla Model3?

I'm using this connector:
https://e-mobility-driving-solutions.com/produkt/diagnostics-cable-tesla-m3-01-2019-bundle/?lang=en

Windows 10, VS2019,
Download this git repo,
configure the bluetooth dongle and map to com3,
run ODB.NET.ConsoleClient with "com3" as argument,
below the output.

Seem working ("Response: 'ELM327 v2.2'"), but no data fetched.
I'm interested in fetching SteeringAngle and similar data, reported here:
https://docs.google.com/spreadsheets/d/1ijvNE4lU9Xoruvcg5AhUNLKr7xYyHcxa8YSkTxAERUw/edit#gid=150828462
but the ID 0x129 is more than a byte, so it's not the PID for dev.RequestData(byte pid) ... what i'm missing?

Thanks for any feedback.

output:

05/09/2021 20:33:45 -  Debug -  Opened Serial-Connection!
05/09/2021 20:33:45 -  Debug -  Initializing ...
05/09/2021 20:33:45 -  Debug -  Resetting Device ...
05/09/2021 20:33:45 -  Verbose -  Queuing Command: 'ATZ''
05/09/2021 20:33:45 -  Debug -  Turning Echo Off ...
05/09/2021 20:33:45 -  Verbose -  Queuing Command: 'ATE0''
05/09/2021 20:33:45 -  Debug -  Turning Linefeeds Off ...
05/09/2021 20:33:45 -  Verbose -  Queuing Command: 'ATL0''
05/09/2021 20:33:45 -  Debug -  Turning Headers Off ...
05/09/2021 20:33:45 -  Verbose -  Queuing Command: 'ATH0''
05/09/2021 20:33:45 -  Debug -  Turning Spaced Off ...
05/09/2021 20:33:45 -  Verbose -  Queuing Command: 'ATS0''
05/09/2021 20:33:45 -  Debug -  Setting the Protocol to 'Auto' ...
05/09/2021 20:33:45 -  Verbose -  Queuing Command: 'ATSP0''
05/09/2021 20:33:45 -  Verbose -  Writing Command: 'ATZ''
05/09/2021 20:33:46 -  Verbose -  Response: 'ELM327 v2.2'
05/09/2021 20:33:46 -  Verbose -  Writing Command: 'ATE0''
05/09/2021 20:33:46 -  Verbose -  Response: 'ATE0'
05/09/2021 20:33:46 -  Verbose -  Response: 'OK'
05/09/2021 20:33:46 -  Verbose -  Writing Command: 'ATL0''
05/09/2021 20:33:46 -  Verbose -  Response: 'OK'
05/09/2021 20:33:46 -  Verbose -  Writing Command: 'ATH0''
05/09/2021 20:33:46 -  Verbose -  Response: 'OK'
05/09/2021 20:33:46 -  Verbose -  Writing Command: 'ATS0''
05/09/2021 20:33:46 -  Verbose -  Response: 'OK'
05/09/2021 20:33:46 -  Verbose -  Writing Command: 'ATSP0''
05/09/2021 20:33:46 -  Verbose -  Response: 'OK'
05/09/2021 20:33:46 -  Debug -  Requesting Type FuelType ...
05/09/2021 20:33:46 -  Debug -  Requesting PID 51 ...
05/09/2021 20:33:46 -  Verbose -  Queuing Command: '0151''
05/09/2021 20:33:46 -  Verbose -  Writing Command: '0151''
05/09/2021 20:33:46 -  Debug -  Requesting Type EngineRPM ...
05/09/2021 20:33:46 -  Debug -  Requesting PID 0C ...
05/09/2021 20:33:46 -  Verbose -  Queuing Command: '010C''
05/09/2021 20:33:46 -  Debug -  Requesting Type VehicleSpeed ...
05/09/2021 20:33:46 -  Debug -  Requesting PID 0D ...
05/09/2021 20:33:46 -  Verbose -  Queuing Command: '010D''
05/09/2021 20:33:46 -  Verbose -  Response: 'SEARCHING...'
05/09/2021 20:33:47 -  Debug -  Requesting Type EngineRPM ...
05/09/2021 20:33:47 -  Debug -  Requesting PID 0C ...
05/09/2021 20:33:47 -  Verbose -  Queuing Command: '010C''
05/09/2021 20:33:47 -  Debug -  Requesting Type VehicleSpeed ...
05/09/2021 20:33:47 -  Debug -  Requesting PID 0D ...
05/09/2021 20:33:47 -  Verbose -  Queuing Command: '010D''
05/09/2021 20:33:48 -  Debug -  Requesting Type EngineRPM ...
05/09/2021 20:33:48 -  Debug -  Requesting PID 0C ...
05/09/2021 20:33:48 -  Verbose -  Queuing Command: '010C''
05/09/2021 20:33:48 -  Debug -  Requesting Type VehicleSpeed ...
05/09/2021 20:33:48 -  Debug -  Requesting PID 0D ...
05/09/2021 20:33:48 -  Verbose -  Queuing Command: '010D''
05/09/2021 20:33:49 -  Debug -  Requesting Type EngineRPM ...
05/09/2021 20:33:49 -  Debug -  Requesting PID 0C ...
05/09/2021 20:33:49 -  Verbose -  Queuing Command: '010C''
05/09/2021 20:33:49 -  Debug -  Requesting Type VehicleSpeed ...
05/09/2021 20:33:49 -  Debug -  Requesting PID 0D ...
05/09/2021 20:33:49 -  Verbose -  Queuing Command: '010D''
05/09/2021 20:33:50 -  Debug -  Requesting Type EngineRPM ...
05/09/2021 20:33:50 -  Debug -  Requesting PID 0C ...
05/09/2021 20:33:50 -  Verbose -  Queuing Command: '010C''
05/09/2021 20:33:50 -  Debug -  Requesting Type VehicleSpeed ...
05/09/2021 20:33:50 -  Debug -  Requesting PID 0D ...
05/09/2021 20:33:50 -  Verbose -  Queuing Command: '010D''
05/09/2021 20:33:51 -  Verbose -  Queuing Command: 'ATPC''
05/09/2021 20:33:52 -  Verbose -  Response: 'UNABLE TO CONNECT'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.