Skip to content

Conversation

@gabrik
Copy link

@gabrik gabrik commented Dec 27, 2023

Hi,

I would first like to thank you for this very useful library.

I started using it with my simulated ECU: https://github.com/spoonieau/OBD2-ECU-Simulator but I was not getting out the right data, while Torque or other ODB apps were able to get them.

I found that the readme lacked some PID configuration for the command to parse the data.

So before I got this data:

2023-12-27 14:15:43.998 29171-30361 PID                     io.xxx.xxx                   D   : 0.0
2023-12-27 14:15:43.998 29171-30361 PID Formatted Result    io.xxx.xxx                   D  0.0 null
2023-12-27 14:15:45.103 29171-30361 PID                     io.xxx.xxx                   D   : 0.0
2023-12-27 14:15:45.103 29171-30361 PID Formatted Result    io.xxx.xxx                   D  0.0 null

Then after passing the right parameters to the PID I got ( I configured the simulator to 2000RPM):

2023-12-27 15:14:04.268 29162-30222 PID                     io.xxx.xxx                   D   : 2000.0
2023-12-27 15:14:04.268 29162-30222 PID Formatted Result    io.xxx.xxx                   D  2000.0 null
2023-12-27 15:14:05.376 29162-30222 PID                     io.xxx.xxx                   D   : 2000.0
2023-12-27 15:14:05.377 29162-30222 PID Formatted Result    io.xxx.xxx                   D  2000.0 null
2023-12-27 15:14:06.483 29162-30222 PID                     io.xxx.xxx                   D   : 2000.0

I hope this will be helpful to other developers using this library.

@gabrik
Copy link
Author

gabrik commented Dec 27, 2023

As a side note, I guess some more commands needs to be sent into the socket to properly configure the device.

As if I open torque and then my app everything works just fine, while if I connect the ODB device and open directly my app I get a bunch of garbage data.

Any help in that direction will be more than appreciated, so that I can include it in this PR.

@barnhill
Copy link
Owner

There are some init commands that you need to send over to setup the connection.

Here is a snippet from an app I have connecting to the ELM327 device and right after this block it sends a normal request for a pid and they work.

        val MODE_AT = "AT"
		
    //set defaults
        initPid.mode = MODE_AT
        initPid.PID = "D"
        var cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
        Log.d(TAG, "Set defaults sent (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)

        //resets the ELM327
        initPid.mode = MODE_AT
        initPid.PID = "Z"
        cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
        Log.d(TAG, "Reset command sent (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)

        //extended responses off
        initPid.mode = MODE_AT
        initPid.PID = "E0"
        cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
        Log.d(TAG, "Extended Responses Off (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)

        //line feeds off
        initPid.mode = MODE_AT
        initPid.PID = "L0"
        cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
        Log.d(TAG, "Turn Off Line Feeds (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)

        //printing of spaces off
        initPid.mode = MODE_AT
        initPid.PID = "S0"
        cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
        Log.d(TAG, "Printing Spaces Off (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)

        //headers off
        initPid.mode = MODE_AT
        initPid.PID = "H0"
        cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
        Log.d(TAG, "Headers Off (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)

        //set protocol
        initPid.mode = "$MODE_AT SP"
        initPid.PID = ObdProtocols.AUTO.value.toString()
        cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
        Log.d(TAG, "Select Protocol (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)

        //set timeout for response from the ECU
        initPid.mode = "$MODE_AT ST"
        initPid.PID = Integer.toHexString(0xFF and ECU_RESPONSE_TIMEOUT)
        cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)

@barnhill
Copy link
Owner

Im sure this code can be cut down from instructions

```
//Request MODE 1, PID 0C - RPM
val pid = PID(ObdModes.MODE_01, "0C")
pid.formula = "(256A+B)/4" // Engine RPM formula https://en.wikipedia.org/wikiOBD-II_PIDs#Service_01
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont have to build this PID yourself.

You can request a fully built pid from the built in library this way:

val pid = PIDUtils.getPid(ObdModes.MODE_01, "OC")

@barnhill
Copy link
Owner

barnhill commented Jul 9, 2025

Im sure this init code could be encapsulated into a method and have default values which would allow the consumer to init with a specific set of values if they need to change them. This might be a great enhancement to add to allow a one call init method to prevent others from having to all this initial command work to get it connected.

@barnhill
Copy link
Owner

Can you try 1.8.1 and see what you get with it? I have now built in the init code needed and the readme has been updated to reflect how to init the ELM device.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants