Page 1 of 1

OBD UART Custom PID question

Posted: Fri Jan 08, 2021 7:57 pm
by pazu
Hi all,

I've read through a few posts now about custom PIDs but I'm not 100% clear on the procedure.

I've got the OBD UART adaptor hooked up and talking to the car fine with standard PIDs.

I have a custom PID I need to retrieve (AT Temp), settings out of Torque are:

OBD2 mode and PID:
2102

Equation:
f-50

OBD Header:
TCM

I note a post where Stanley says I can request Mode 2 PIDs here:

Code: Select all

int value;
obd.dataMode = 2;
if (obd.read(1, value)) {
  // do something with 'value'
}
So when I send the request then, should it be:

Code: Select all

int value;
obd.dataMode = 2;
if (obd.read(102, value)) {
  // do something with 'value' (like the equation)
}
For that specific non standard PID?

Do I need to change the OBD Header somewhere?

I tried adding #define PID_TRANS_TEMP 0x2102 in OBDUART.h but I'm sure that isn't correct.

Car is a Mitsubishi Pajero 2009 model if that helps/matters.

Thanks!

Re: OBD UART Custom PID question

Posted: Fri Jan 08, 2021 9:37 pm
by pazu
got a little bit further in formatting - believe the mode setting should be the following:

Code: Select all

  obd.dataMode = 21;
  if (obd.readPID(02, value)) {
    value = value - 50;

Not 100% on the equation yet but I'll sort that once I start getting data in.

the bit i'm not sure on now is the header - needs to be TCM but not sure of the hex code for that if there is one?

Re: OBD UART Custom PID question

Posted: Sat Jan 09, 2021 9:37 pm
by pazu
So I worked out how to get custom OBD commands working - for anything outside of the standards PIDs in the library, you have to use direct AT Commands. The function I have running well is posted below in the hope it helps someone else. There are a couple of quirks that I think are related to the library processing the commands, but it works reliably. In my code I've commented out the serial comments as I'm using this within another program. Hopefully this helps someone else!

Code: Select all

void RunATcommands()
{
  //Setup AT Commands to sent
  //ATZ - soft reset
  //ATH1 - Show headers
  //ATS1 - Show Spaces between bytes
  //0100 - test PID code - this is a quirk, if you remove this command all future commands fail
  //ATSH7E - Switch header to Trans Module for Pajero NT
  //2102 - Call Trans Temp PID
  static const char cmds[][9] = {"ATZ\r", "ATH1\r", "ATS1\r", "0100\r", "ATSH7E1\r", "2102\r"};
  char buf[128];

  for (byte i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
    const char *cmd = cmds[i];
    //mySerial.print("Sending ");
    //mySerial.println(cmd);

    //Send Serial command
    if (obd.sendCommand(cmd, buf, sizeof(buf))) {
      char *p = strstr(buf, cmd);
      if (p)
        p += strlen(cmd);
      else
        p = buf;

      //Take buffer and pull characters 39 and 40 - combined gives AT Temp (If statement is check digit to make sure we pull correct PID response from multiple lines)

      if (buf[4] == '1') {
        //Debug - print full Buffer
        //mySerial.println(buf);

        //Debug - print Char 39 and 40 seperately
        //mySerial.print(buf[39]);
        //mySerial.println(buf[40]);

        //Combine char 39 and 40 to create Hex number for conversion
        //char output1 = buf[39];
        //char output2 = buf[40];
        char output3[3] = {buf[39], buf[40]};

        //Debug - test combined output
        //mySerial.println("HEX Number");
        //mySerial.println(output3);

        //Take output of char 39 and 40 and convert from Hex to Dec, putting in a string like "63" in place of Output3 gives expected output
        long output4 = strtol(output3, NULL, 16);

        //Temp equation to correct output
        output4 = output4 - 50;

        //Print Trans Temp to serial
        //mySerial.println("Corrected DEC number");
        //mySerial.println(output4);

	//Display output on LED screen
        display.fillRect(72, 8, 58, 26, BLACK);
        display.setCursor(80, 16);
        display.setTextSize(2);
        display.print(output4);
        display.println('C');
        display.display();


      }

      while (*p == '\r') p++;
      while (*p) {
        Serial.write(*p);
        if (*p == '\r' && *(p + 1) != '\r')
          Serial.write('\n');
        p++;
      }
      //mySerial.println();

    } else {
      //mySerial.println("Timeout");
    }
    delay(1000);
  }
  //mySerial.println();
}