Categories: Car PCProgramming

Get OBD2 Data via ELM327 C#.NET

According to me, the most cool part of a CARPC is where you get OBD2 data to your touch screen interface. It is really cool. I wrote about the protocol called VAN in another article. OBD2 is the way to get data from your engine control unit which also called ECU.

First time when I begin to work on a carpc, I always used free front-ends. But I couldn’t customize the front-end software according to my requirements. So I started to develop my own CARPC front-end. Multimedia applications, navigation, watching TV via 3G and so on..

Getting data from my OBD2 port of my Citroen C4 was different. I tried OBD2 applications like ScanTool.NET, OBDview, etc. always there was missing functionality that one app cannot provide.

Physical Connection to get OBD2 Data

Let me talk about physical connection; In Cars, there is a port with standard 16 pins OBD2 pinout. This is the port when you get your car to technical service used to connect.

OBD2 Port to get OBD2 Data

The easiest way I know to get data from OBD2 is the OBD2 adapters like ELM327. You can connect that adapter to your PC through USB, then the OBD2 socket to your car. It behaves as a COM port through your USB. This means you can test and also get data from HyperTerminal program or Putty. Its command set is like a modem; using AT commands.

If you want to get data from your car, connect to your ELM327 adapter through virtual com port using your terminal program. Then send it a AT command. Just it. By knowing this, and if you know just a bit serial programming, do you really need to purchase a Diagnostics application?  Clearly.. “NO”.

get OBD2 Data using ELM327 USB Kit

Using ELM327 to get OBD2 Data

To begin retrieving  data from your engine, you need to initialise your ELM327 with your car. OBD2 data protocols varies by car vendors and specs. You need to initialize your OBD2 device according to your car vendor and model.

Usually, each value in OBD2 (speed, rpm, engine load..) uses 4 digits Hex number as identifier. You send the request with their identifier id and it responds to you by its identifier id. The logic is very easy at all.

Example: Send command “010C”

Receive response “010C001B”

010C is the identifier, 00 1B are the data hex numbers.

the first 00 is the A, and the 1B is the B let’s say.

we will know that the identifier 010C means rpm. To calculate rpm you need to make math between A and B.

rpm=((A*256)+B)/4

Now let’s clarify the initialization process; (C#.NET Code)

serialPort1.PortName = comPort;

            try { serialPort1.Open(); }
            catch (System.Exception excep) {}

            send_data("ATZ");
            System.Threading.Thread.Sleep(2000);
            send_data("ATE0");
            System.Threading.Thread.Sleep(1000);
            send_data("ATL0");
            System.Threading.Thread.Sleep(500);
            send_data("ATH1");
            System.Threading.Thread.Sleep(500);
            send_data("ATSP 5");
            System.Threading.Thread.Sleep(500);
            send_data("01 00");
            System.Threading.Thread.Sleep(500);

The code above, starts sending initialisation commands using threading. ATSP command is important. ATSP 5… number 5 is the protocol for my car. You need the try it with others if this does not work for yours. To try each command one by one, the best way is to use putty. Use the initialisation commands by using your keyboard and try ATSP command from 1 to 5. if the protocol you try is not compatible your car, you get “error” or “no data” response.

ATZ command; resets the ELM327.

ATE0, ATL0; disables extended responses. When it is disabled it is easier to process the responses programmatically.

ATH1; enables the responses with their headers. It is good to enable it because in responses you can see the identifier of data. So it is easier to understand the response.

We talked about the ATSP command. But shortly, ATSP1, ATSP2, ATSP3, ATSP4, ATSP5 are the protocols.

0100 command is to test if the connection is OK.

ELM327 PIDs

Each value of engine has a PID (010C, 010D)

01 0C 2 Engine RPM 0 16,383.75 rpm ((A*256)+B)/4
01 0D 1 Vehicle speed 0 255 km/h A

To get the other pids meaning you can use the releated wikipedia page; http://en.wikipedia.org/wiki/OBD-II_PIDs

Every car does not response to evey pid. Speed, engine rpm, these are standardized pids. And most cars easily respond to that pids.

I can get; speed, engine rpm, engine tempature, engine load, engine air flow rate, vehicle battery voltage from my car. (battery voltage is not obd2 releated data, just vc command)

Other thing is threadings.. they are very important because you have to wait for being ready of elm327 before sending a new command. 200 ms is enough for ELM327’s internal process.

Another trick is that you don’t have to try it with your car. there is a simulation application called “obdsimwindows”. It creates a virtual obd2 port

I read about this simulation program in mp3car forum.

http://www.mp3car.com/engine-management-obd-ii-engine-diagnostics-etc/137428-free-software-obd-ii-simulator-redux.html

burakalakus

View Comments

    • Hi

      I would like to create a wrapper application (C#) which will communicate with OBD to get the speed parameter.
      Please let me know if you have a suggestion for that.

      Thanks, Eli

  • Hi,
    Thank you for your article it's very helpful.
    I want to test each speaker of the car,
    Wondering what kind of commands I can use to isolate each speaker?
    (Like turn on the right back door speakers and all other speakers off.)

    • At Least for my citroen, it is not possible as i know. First you should search if it is possible by using official diagnostic software

  • Hi there, I want to get SOC, GID, Mileage, Kms driven, Car diagnostics parameters through OBD II scanner?

    CAR Model: Nissan LEAF S/ SV (Electric Vehicle) SV

    Can you please advise me with which scanner should i purchase from Market

    Do they support and compatible with Nissan Leaf S/SV model?

    I am looking forward to buy OBD II to WI-FI adaptor? Are these available?

    After I build Wi-Fi connection: Is it possible to use standard AT Commands for Extracting Data from Nissan Leaf EV?

    Is ELM327 adaptors from OBDII to wifi , would be sufficient and compatible to use. Please help

    Is there any limitations on OBDII via Wifi with adaptor

    Requesting you to put me in right direction

    thanks,

  • Hi Burak,

    Why we have to wait for 200 msec for the internal process of the elm327. What is the process taking such time ? And is it possible to make it quick?

    • May be with new release of elm327 it is different. As you can see it is an old article and at that time, if I sent my commands continously, I saw that elm327 gets stack. So practically I solved it by pausing 200 msec. for each command.

  • Hey I tried to connect to an ODB2 Adapter, to read some information. When I tried your connection Sequence, I got back : BUS INIT: ERROR, do you know what went wrong?

  • hi,
    I want to read only 0131 (Distance traveled since codes cleared).
    Have I to send those all commands? (ATZ, ATE0....)

    Can I send only 0131?

  • Hi,
    Do you have an idea to get an analog signal from my 2010 model Renault Grand Scenic OBDII port which uses CAN prothocol. I need this analog signal for Valeo Guideo Lane departure warning unit. In the installation guide booklet of Valeo Guideo it says like that:
    "1)Locate the vehicle’s speed information:(either at its radio,or at the speed output used by the GPS, or at the ABS ).
    2)Use the multimeter to check that it is indeed speed information:Connect the multimeter’s –connection to the vehicle's ground, and its + connection to the located speed output.Drive a short distance to check the information; the reading should fluctuate between 0 and 12V.
    Note:On certain multiplexed vehicles, the speed information is coded and cannot be used in unmodified form (on the Peugeot 807, for example). In this case, you can connect an electronic decoder (interface) to the multiplexed cable in order to convert the electronic data into electrical speed information that the multimeter can recognise"
    Please help.

    • As I understood it does not use can bus for that. It is using car's speedometer hardware directly which inputs your ecu. So this is not something related to CAN BUS.

  • Hi,

    Thanks for putting across the article, it is quite helpful.

    I was wondering if it is possible to get rid of an interpreter chip and get all the OBDII data dump by building a simple CAN interface. Take the dump and store in a flash device for further analysis.

    Let me know your comments on this.

    • Sure you can. It is not very hard by using arduino or microchip based circuit. Also you can modify the elm327 circuit and add yours circuit that sends commands to elm327 and stores the result in somewhere.

      • Thanks for your reply.

        I have to blindly dump the data into SD card without analyzing or interpreting it. Data analysis will be done later and it is not a part of initial project.

        • So, I guess a circuit with MCP2551 (line-transcoding) will work. MCP2551 will output the CAN-BUS in logic-level signalling. And you can read this output from PIC and store to SD.

    • It is very easy to use obd2 by using elm327 as you can see from article. The important point not all cars provides full access via obd2. You need to work with can which is more complex to use but more informative choice ;)

Recent Posts

Avaya SAL Gateway

Avaya SAL Gateway (Secure Access Link) is a remote connectivity solution used by Avaya to provide remote support for its… Read More

5 years ago

Change Linux Timezone (RHEL / CentOS)

You can change Linux Timezone via bash command line easily. All Timezone information are stored in /usr/share/zoneinfo/./etc/localtime is symlink to… Read More

5 years ago

DALI Protocol (Digital Addressable Lighting Interface)

DALI is a communication protocol that is used for the automation of lighting, which is not new at all. It… Read More

5 years ago

Manchester Code Explained

Communication requires being in sync just like an orchestra. What happens when the bass guitar in band is not sync… Read More

5 years ago

Secure Server Access with SSH Key without Password

You have access to many different Linux / Unix servers during the day, you are in the IT department, remembering… Read More

8 years ago

iPad Mini in CAR explained

CAR PC in car is now old school and I decided to mount an iPad mini into the dashboard. (more…) Read More

11 years ago