OBD-II programming

So a year or two ago I bought myself an ElmScan tool from scantool.net to be able to get a Miles Per Gallon (MPG) reading out of the ECU of my car and onto my PDA. Well today I buckled down to learn the standard a bit. It’s not as difficult as I thought. Writing code for Pocket PC 2002 is, but I’ll get into that later. So I’ve gained the following knowledge today: All transmissions over RS232 are done using ASCII characters, not actual hex bytes as I originally thought. (You know, to be more efficient with the bandwidth? Well whatever.) Next, I learned the coolness of AT signals. You know, those that your modem makes, or more correctly, outputs to the console (ATZ comes to mind). So starting off, I used 9600baud for transmission speed, because I was sure that communication worked well at that rate. Later I will be moving to 38.4Kbaud. So to set up a connection, all you need is to send the following signals and do the following actions:
1) Send “ATZr” and wait about 1 second. (This resets the ElmScan tool)
2) Send “ATE0r” and wait 200 msec. (This turns off echo)
3) Send “ATL0r” and wait 200 msec. (This makes it so that only a CR (‘/r’) is sent instead of CRLF (“/r/n”))
4) Send “0100r” and wait 1 second. (This initializes the communication between the ElmTool and the CAN bus and returns a bit encoded set of supported PIDs.)
5) Clear the receive buffer of the RS232. (Unless you really care about the initialization information)

That’s about it. Note the use of the ‘r’ byte, this signifies to the ELM tool that it should execute the command you just issued. The scan tool responds in the following ways (*.* means version number, and XX is some value specific to your car model):
1) “ATZrrELM Scan v*.*r>”
2) “ATE0rOKrr>”
3) “OKrr>”
4) “41 00 BE XX XX XXrr>”

One thing I have still to figure out is how exactly the supported PIDs are bit encoded. Also I have this issue with Visual Studio not cooperating with my wishes to compile .NET CF v1.* code. I think twisting Sir.Gates’ arm should be a good remedy. Reading this document, I found out that all you need to calculate MPG is Speed * 7.718 / MAF. The Speed PID is 0D, and the MAF PID is 10. To query those, you just send the ASCII string “010Dr” and “0110r” respectively. A typical response is “410D00” if you’re stationary. I don’t seem to have a Mass Air Flow sensor in my Honda so I get a “NO DATA” as a text string back for the MAF. I’ll have to find another way to calculate MPG. I recommend reading that PDF as it seems to summarize the operation of the ElmScan rather well. For more technical information, don’t forget to hit up the official site.

Well, that’s it for now, I’ll keep you posted on my progress.
-Fekete András

Liked it? Take a second to support me on Patreon!

6 thoughts on “OBD-II programming

  1. Hi Fekete,

    Just saw your post, wondering how you’re getting on.

    I’ve reciently purchased a ODBKey OBD adapter for which ive been writing some pda software for using .net 2.

    One of the features ive added is instant and average fuel economy which works well providing the engine is under a constant load. As soon as you take your foot off the accelerator it jumps up to like 200mpg – im sure you’re experiencing the same problem, was wondering if you’ve found a solution to it?

    Love the article btw, shame i didn’t find it when i had just started programming with my adapter – would have made the connection and initialisation a lot less painful.



  2. Hi!

    Unfortunately I haven’t had any time lately to work on my OBDII stuff. Though, first thing that comes to mind is to have your software just clamp the output. It kinda makes sense that your MPG drastically increases, since when you let off the accelerator the car isn’t using much gas to keep going.
    Good luck in your endeavor. Seems like you’re further along than I am.

  3. Phil,
    I imagine what you are trying to do with our programming is to get an “instantanieous” MPG, when there really is no such thing. MPG requires some distance to be traveled in order to get a real reading. You might be better off having your program average you the last 10 readings …. or having it calculate MPG off a set of 10 readings.

  4. Hi Phil,

    I saw your post and I’m looking for the same thing (MPG) for my iPhone. Ths OBDKey adapter does not (yet) support it, so I would like to write it myself, but need some code to ge me started. Any tips for this?

    Thanks, Paul

  5. Hello Fekete,

    I’ve just ordered an Elmscan-5 bluetooth connector. I’m interested in writing a parser and frontend for showing MPG as well as other information on the fly using WinCE and/or Palm devices. I have experience programming both. Any luck w/yours? Ciao.


  6. Portsample,

    Unfortunately, I haven’t gotten far with my project. Calculating MPG isn’t as easy as I originally thought. There’s no real algorithm that I’ve found unless you derive your own for your car (using MAF measurements, speed, etc). Though if you want a parser, I’d look on scantool.net, as they have some premade source code to get windows mobile and palm going with those tools. Let me know how that goes.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.