AndroidOBD icon indicating copy to clipboard operation
AndroidOBD copied to clipboard

Incorrect results when calling pid.calculatedResults

Open Superredstone opened this issue 7 months ago • 8 comments

My output:

410C189F 2 ((A*256)+B)/4
07:13:43.740  D  Engine RPM : 0.0
07:13:43.741  D  0.0 rpm

My code:

        for (i in 0..1000) {
            val pid = checkNotNull(PIDUtils.getPid(ObdModes.MODE_01, "0C"))
            val command = OBDCommand(pid).setIgnoreResult(false).run(streams.inputStream, streams.outputStream)

            Log.d(LOG_TAG, "${command.formattedResult} ${command.rawResult} ${pid.bytes} ${pid.formula}" )
            Log.d(LOG_TAG, "${pid.description} : ${pid.calculatedResult}")
            Log.d(LOG_TAG, command.formattedResult)
        }

For some reason i get correct bytes back to my phone but when i try to access those with pid.calculatedResults or command.formattedResult i always get 0.0. This does not only apply for engine RPM, i also tested this on speed and fuel type.

Superredstone avatar Jul 10 '25 07:07 Superredstone

Image

With the latest version of the library Im getting engine RPM to show correctly

barnhill avatar Jul 26 '25 19:07 barnhill

Im wondering if the entire init sequence wasnt completed so its always returning 0 or something. Im about to add the same init sequence I use as a method to call to handle inits in 1.8.0 of the lib. Working on getting that in there now.

barnhill avatar Jul 26 '25 20:07 barnhill

i am using the init sequence that is provided in the README file:

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)

Superredstone avatar Jul 26 '25 20:07 Superredstone

Im releasing 1.8.0 with the init sequence built in. Now youll just need to call ObdInitSequence.run(bluetoothSocket).

barnhill avatar Jul 27 '25 00:07 barnhill

Are you getting any other messages in the logs to indicate why that command isnt running? Is the bluetooth socket returning some error or anything useful because 0 seems like defaults?

barnhill avatar Jul 27 '25 00:07 barnhill

If you get any errors during the calculation of the formatted values it should print an error to the logs like this: Log.e(OBDCommand::class.java.simpleName, "[Expression:" + expression + "] [mode:" + mPid.mode + "] [Pid:" + mPid.PID + "] [formula:" + mPid.formula + "] [bytes:" + mPid.bytes + "] [BytesReturned:" + mPid.data.size + "]", genEx)

barnhill avatar Jul 27 '25 00:07 barnhill

No errors are printend in the console, this code just prints the following: Formatted result: 0.0 Km Result: 612700EA5C0002AD00AA

                val pidOdometer = checkNotNull(PIDUtils.getPid(ObdModes.MODE_01, ODOMETER_PID));
                val cmdOdometer = OBDCommand(pidOdometer).setIgnoreResult(false).run(inputStream, outputStream)
                Log.i(LOG_TAG, "Formatted result: ${cmdOdometer.formattedResult} Result: ${cmdOdometer.rawResult}");

I've written a function with the same library that you are using for calculating values (EvalEX) and it does indeed work.

EDIT: This runs on the latest version of the library

Superredstone avatar Jul 28 '25 06:07 Superredstone

the calculation that Im feeding to EvalEx is as follows:

(A*(2^24)+B*(2^16)+C*(2^8)+D)/10

Where A-D are the first 4 bytes of the response like all other PIDs are calculated unless that calculation is incorrect?

barnhill avatar Aug 07 '25 01:08 barnhill