Thats the part of the method to get the VIN which I do still use. In the last step, I have add a WAIT message but withou any changes.
The result on the serial output you find below the code. The first request is not answered. On the 2nd request I get the answer of request 1 and so on.
It seems, that anything is stuckin in the buffer. I use the latest master-branch of the Seeed CAN Shield lib. I have declared a lot of double needed var's to separate the error by reading form memory, I know - it's not needed. Only for debugging.
void ReadVIN()
{
// send MSG for VIN Request
unsigned char len = 0;
unsigned char buf1[8];
unsigned char buf2[8];
unsigned char buf3[8];
unsigned char buf4[8];
unsigned char tmp[8] = {0x02, 0x09, 0x02, 0, 0, 0, 0, 0}; // Mode 9 PID 2 is the VIN
unsigned char flow_get[8] = {0x30, 0x00, 0, 0, 0, 0, 0, 0}; // Flow Frame Request
unsigned char flow_wait[8] = {0x31, 0x00, 0, 0, 0, 0, 0, 0}; // Flow Frame Wait
lcd.setCursor(0,0);
lcd.print("VIN: ");
Serial.println("--------------------------------------------------------------------");
Serial.println("START, SEND: ");
Serial.println("--------------------------------------------------------------------");
Serial.println("response: ");
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); // normal request init for 3 Bytes
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, flow_wait); // please wait
// recive MSG
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data
{
CAN.readMsgBuf(&len, buf1); // read data, len: data length, buf: data buf
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, flow_get); // cont. with request for 1st 7 Bytes
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, flow_wait); // please wait
}
Serial.println("--------------------------------------------------------------------");
Serial.println("Flow 1, SEND: ");
Serial.println("--------------------------------------------------------------------");
// recive MSG2
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data
{
CAN.readMsgBuf(&len, buf2); // read data, len: data length, buf: data buf
Serial.println("Response Message 2:");
for(int i = 5; i<len; i++) // print the data beginning with byte 1
{
lcd.print((char)buf2);
Serial.print(buf2,HEX);
Serial.print("\t");
}
len = 0;
Serial.print("\r\n");
Serial.println("--------------------------------------------------------------------");
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, flow_get); // cont. with request for the 2nd 7 Bytes
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, flow_wait); // please wait
}
lcd.setCursor(0,1);
Serial.println("--------------------------------------------------------------------");
Serial.println("Flow 2, SEND: ");
Serial.println("--------------------------------------------------------------------");
// recive MSG3
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data
{
CAN.readMsgBuf(&len, buf3); // read data, len: data length, buf: data buf
Serial.println("Response Message 3:");
for(int i = 1; i<len; i++) // print the data beginning with byte 1
{
lcd.print((char)buf3);
Serial.print(buf3,HEX);
Serial.print("\t");
}
len = 0;
Serial.print("\r\n");
Serial.println("--------------------------------------------------------------------");
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, flow_get); // cont. with request for the 2nd 7 Bytes
//CAN.sendMsgBuf(CAN_ID_PID, 0, 8, flow_wait); // please wait
}
// recive MSG4
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data
{
CAN.readMsgBuf(&len, buf4); // read data, len: data length, buf: data buf
Serial.println("Response Message 4:");
for(int i = 1; i<len; i++) // print the data beginning with byte 1
{
lcd.print((char)buf4);
Serial.print(buf4,HEX);
Serial.print("\t");
}
len = 0;
Serial.print("\r\n");
Serial.println("--------------------------------------------------------------------");
}
Serial.print("END");
Serial.println("--------------------------------------------------------------------");
delay(3000);
lcd.clear();
}
#########################################################################################
Serial Output:
START, SEND:
--------------------------------------------------------------------
response:
4B 30 46
--------------------------------------------------------------------
send Flow Frame Request:
--------------------------------------------------------------------
--------------------------------------------------------------------
Flow 1, SEND:
--------------------------------------------------------------------
Response Message 2:
I W A U
--------------------------------------------------------------------
--------------------------------------------------------------------
Flow 2, SEND:
--------------------------------------------------------------------
Response Message 3:
A 1 2 4 0 9 6
--------------------------------------------------------------------
END--------------------------------------------------------------------