0

Having trouble figuring out how to format the commands I send to my OBD device. First off Torque is working fine so I know its my code that's the problem not the device.

Here's how I'm sending my commands:

public void run() {

            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    byte[] command = new byte[5];
                    command[0] = rawToByte(268);
                    //command[1] = rawToByte(269);

                    Log.i("gas", "Command: "+Byte.toString(command[0]));
                    write(command);
    /////////////////////////////////////////    problem with below line               
                    bytes = mmInStream.read(buffer);
                    Log.i("gas", "Response: "+Integer.toString(bytes));
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    break;
                }
            }
        }

This is based off some research I was doing, if anyone can suggest an easy way, that would be appreciated. 268 is the decimal equivalent of "010C" which is the code for engine rpm according to the elm327 sheet.

My write() method:

public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
                mmOutStream.flush();

            } catch (IOException e) { }
        }

And my rawToByte() method:

public byte rawToByte(int b) {
            return (byte) (0xff & b);
        }

The response log cat reads "5" every time and same if I use 269 for speed. This might be an error code that im sending the commands wrong. The write() method on the socket mmOutStream only takes bytes and the rawToByte method I got from some question on here (which doesnt answer this question) only takes integers so i cant feed it "010C", but this shouldn't make a difference.

My application needs to constantly get speed and rpm of a vehicle.

Can anybody specify (simply) how to properly send commands to an OBDII Elm327 device?

user2269886
  • 45
  • 1
  • 8
  • anybody? running well past my deadline because I can't figure out whats going on here. Any help will be rewarded with written praise! – user2269886 Dec 20 '13 at 18:27

1 Answers1

0

I'm not sure if you are still in need of an answer, but here is how I would try to do it:

String message = "010D\r";
mmOutStream.write(message.getBytes());
byte[] buffer = new byte[1024];
mmInStream.read(buffer);

Then convert the buffer back to a string. You may need to add a couple steps to remove the empty bytes from your array to get the results you are looking for. Hope that helps!

Dan Harms
  • 4,725
  • 2
  • 18
  • 28
  • Thanks for the response, I tried what you suggested for "010C/r" with the engine running and the responses I got were. "5", "12" then I am constantly getting "17". Also I revved the engine while this was running and it made no difference. The engine was running prior to the running the app so if the values returned were actuly rpm values they should be the same. Tried with "010D/r" as well and exact same results – user2269886 Jan 08 '14 at 17:33
  • Are you converting back to a string or looking at the raw bytes of the response? – Dan Harms Jan 14 '14 at 02:39
  • I'm using logcat like this: Log.i("gas", "Response: "+mmInStream.read(buffer)); – user2269886 Jan 15 '14 at 11:10
  • Try doing `Log.i("gas", "Response: " + new String(buffer));` That will convert the bytes into a real string. – Dan Harms Jan 15 '14 at 15:43
  • Well were getting somewhere, ive attached a link to my log file, ignore the "Getting Location.." messages their for another part of the app. https://www.dropbox.com/s/r5giw96e3xv5zi5/log.txt says CAN error, ran the app with the engine running, then an exception stopped the connection, forgot to print a stack trace but i assume the obd device stopped responding – user2269886 Jan 15 '14 at 17:00
  • You need to clear the error before it will return anything else. Send an "ATZ\r" before the speed or rpm request. See if that helps. Otherwise, read up on the different [AT commands you can use with ELM327.](http://elmelectronics.com/DSheets/ELM327DS.pdf) – Dan Harms Jan 16 '14 at 02:42
  • Thanks that worked, I also had to set the protocol to automatic before the loop. One last thing if you don't mind, This is how I'm sending and receiving now: https://www.dropbox.com/s/r5giw96e3xv5zi5/log.txt And this is the response I'm getting: Response: 7F 10 12STOPPED>10CPPED>10D>10C7F 10 12STO???????? From the data sheet you linked it says it should respond with "41" followed by the code request "0C" then the values, after "0C" it says 7f1012 I dont know how to interpret that, 10 12 in decimal/4 = 1028.5 which was the rpm at the time. What is 7F? and why is there no "41"? – user2269886 Jan 16 '14 at 17:02
  • Also I marked your answer as accepted because you did answer the original question, my question above is just to save me a bit of a headache :) – user2269886 Jan 16 '14 at 17:20
  • It looks like you have multiple responses running together. Play around with setting some of the other AT codes to see if you can get the output to be a little bit cleaner. I think I solved this by setting to automatic timing "ATAT1\r". – Dan Harms Jan 16 '14 at 23:04