OBD2 - Reading VIN

Hello all,

Here are some details of my setup:

Arudino MEGA 2560

Sparkfun OBD-II UART (https://www.sparkfun.com/products/9555)

2002 Chevrolet Silverado PCM

I’m having partial success with reading the VIN from my car. Using mostly sample code, and bit of my own, I’m getting

the following output:

01 00 00 00 31

02 47 31 46 50

03 32 32 47 31

04 37 34 34 37

This is great, however the output should have a 5th line, ie:

01 00 00 00 31

02 47 31 46 50

03 32 32 47 31

04 37 34 34 37

05 14 15 16 17

A vehicle VIN number is 17 digits and I’m only getting 13. Why is the last line not showing?

Thank you in advance.

Here is my code:

Edited by moderators to add code tags.

char rxData[333];  //80
char rxIndex = 0;

void setup(){

  Serial.begin(115200);
  Serial1.begin(9600);   //Sparkfun OBD2

  // turn echo off
  // Serial1.println("ATE0\r");
  // Serial1.flush();
  delay(500);
}

void loop(){
  
  delay(2000); //(2500)
  Serial1.println("0902");  //Request VIN
  // delay(100);
   
  getResponse();  
  //getResponse();  (ATEO echo off)
  
  Serial.print(rxData[6]); 
  Serial.print(rxData[7]); 
  Serial.print(rxData[8]); 
  Serial.print(rxData[9]);  
  Serial.print(rxData[10]); 
  Serial.print(rxData[11]); 
  Serial.print(rxData[12]); 
  Serial.print(rxData[13]); 
  Serial.print(rxData[14]); 
  Serial.print(rxData[15]); 
  Serial.print(rxData[16]); 
  Serial.print(rxData[17]); 
  Serial.print(rxData[18]); 
  Serial.println(rxData[19]); 
  
  delay(200);
  
}


void getResponse(void){

  char inChar = 0; 
  while(inChar != '\r'){
  
    if(Serial1.available() > 0){
  
      if(Serial1.peek() == '\r'){
        inChar = Serial1.read();
        rxData[rxIndex] = '\0';
        rxIndex = 0;
      }
  
      else{
        inChar = Serial1.read();
        rxData[rxIndex++] = inChar;
      }
    }
  }
}

You do know you’re only printing rfxData[6] through rxData[19]? That’s 14 characters.

Correct.

The 14 characters are:

0 …> Serial.print(rxData[6]);

1 …> Serial.print(rxData[7]);

space > Serial.print(rxData[8]);

0…> Serial.print(rxData[9]);

0…> Serial.print(rxData[10]);

space > Serial.print(rxData[11]);

0…> Serial.print(rxData[12]);

0…> Serial.print(rxData[13]);

space > Serial.print(rxData[14]);

0…> Serial.print(rxData[15]);

0…> Serial.print(rxData[16]);

space > Serial.print(rxData[17]);

3…> Serial.print(rxData[18]);

1…> Serial.println(rxData[19]);

My point was that you’re not printing any more characters. Did you receive more that you’re not printing?

Yes, I’m expecting more data from the command 0902 sent to the PCM/ECU to retrieve the VIN.

The program spits out 4 lines of data, then stops.

There should be a 5th line of data. I tried to increase the buffer size from 20 to 80 which helped; and then 320 [which is overkill]

If I uncomment the delay before “getResponse()” a 5th data set is listed, but it’s not correct. The data becomes “corrupt”.

Serial1.println(“0902”); //Request VIN

// delay(100);

getResponse();

So, my next question is:

What part of that code is generating the “loop” to print lines 01 - 04, but not 05?

There is no #include library to cross reference?

You may want to add a Serial.print(inChar); in the else block of getResponse() to see what is really coming back from the ODB2.

I haven’t used it but a quick look at the documentation for the ELM327 (page 44) implies that you need to send the 0902 once and then do 5 getResponse() calls, once per line of the responding data. After each getResponse, you would print the 14 characters corresponding to that line of data. Don’t put in any delay() calls between sending the command and any of the getResponse calls to avoid overflowing the serial data buffer.

/mike

n1ist:
You may want to add a Serial.print(inChar); in the else block of getResponse() to see what is really coming back from the ODB2.

I haven’t used it but a quick look at the documentation for the ELM327 (page 44) implies that you need to send the 0902 once and then do 5 getResponse() calls, once per line of the responding data. After each getResponse, you would print the 14 characters corresponding to that line of data. Don’t put in any delay() calls between sending the command and any of the getResponse calls to avoid overflowing the serial data buffer.

/mike

Thank you for the tips. I’ll give them a try and update the thread later this evening.

Thanks N1IST, it’s working much better now.

Funny you mentioned pg.44 as I stopped reading on page 42-43 when it began to reference CANBUS.

In any case, this is the output:

49 02 01 00 00 00 32 01 00 00 00 32

49 02 02 47 31 46 50 02 47 31 46 50

49 02 03 32 32 47 30 03 32 32 47 30

49 02 04 32 32 31 32 04 32 32 31 32

49 02 05 37 34 31 37 05 37 34 31 37

05 37 34 31 37

090205 37 34 31 37

I’ll need to tweak the code to remove the echo and once I get it correct, the code will be posted.