Page 1 of 1

PID Support Checker Sketch

Posted: Mon Apr 13, 2015 11:11 am
by haydent
Here's a little sketch that uses freematics obd library to go through the list of pid's and report back which ones are supported by your car.

Code: Select all

#define SD_LOG
#define LAST_PID 128
#define DEBUG Serial3
#define DEBUG_BAUD 115200
#define LOOP_DELAY 500 //milli delay between pid's

#include <Arduino.h>
#include <OBD.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

File logFILE;
COBD obd;

void setup()
{
  DEBUG.begin(DEBUG_BAUD);
  DEBUG.println("PID TESTER");
#ifdef SD_LOG
  pinMode(SS, OUTPUT);
  SD.begin(10);
  char* filename = "PID_TEST.TXT";
  SD.remove(filename);
  logFILE = SD.open(filename, FILE_WRITE);
  logFILE.print("PID TESTER\r");
  logFILE.flush();
#endif 
  obd.begin();
  obd.init();


void loop()

   
   static byte pid = 0;   
   int value;
   
   DEBUG.print(pid);
#ifdef SD_LOG     
   logFILE.print(pid);
#endif
   
   if (obd.read(pid, value)){
     DEBUG.println(" RESPONSE");
     #ifdef SD_LOG
       logFILE.println(" RESPONSE");
       logFILE.flush();
     #endif
   }else{
     DEBUG.println(" N/A");
     #ifdef SD_LOG
       logFILE.println(" N/A");
       logFILE.flush();
     #endif   
   }
 
    pid++;
    delay(LOOP_DELAY);
   
    if(pid > LAST_PID){
       DEBUG.println("DONE");
    #ifdef SD_LOG     
       logFILE.println("DONE");
    #endif
      while(1){};
    }
}

Re: PID Support Checker Sketch

Posted: Sat Apr 18, 2015 4:29 am
by jmlsteele
OBD has built in codes to check for support of PIDs (0x00, 0x20, 0x40, 0x60 and 0x80). Each command returns as 32 bit result indicating support for the next 32 PIDs (or not).
The OBD library uses these PIDs and stores the results in pidmap, which is a public variable of the COBD class. This is done when COD::init is executed, so you can get all the information you need without having to send a command yourself (I haven't tested the below code, but it does compile and I went through the logic on the whiteboard...)

Code: Select all

int pid = 0;
void loop() {
  DEBUG.print(pid);
  #ifdef SD_LOG     
   logFILE.print(pid);
  #endif

  if (pidmap[pid/8] & (0x01 << (7-pid%8))) {
    DEBUG.println(" SUPPORTED");
    #ifdef SD_LOG
      logFILE.println(" SUPPORTED");
      logFILE.flush();
    #endif
  } else {
    DEBUG.println(" N/A");
    #ifdef SD_LOG
      logFILE.println(" N/A");
      logFILE.flush();
    #endif
  }
  pid++;
  delay(LOOP_DELAY);
   
  if(pid > 0x7F){
    DEBUG.println("DONE");
    #ifdef SD_LOG     
      logFILE.println("DONE");
    #endif
    while(1){};
  }
}

Re: PID Support Checker Sketch

Posted: Mon Apr 20, 2015 10:53 am
by stanley
Thanks for sharing your sketch.

Re: PID Support Checker Sketch

Posted: Wed Apr 22, 2015 10:33 am
by haydent
yes i can see that bit in init now, thanks for pointing it out.

the reason i did it my way is that i had already tried those pid supported requests with a elm327 and they dont seem to work correctly with my car. thus my manual method.