I have been following this thread as I just got the obd II uart board also and was having problems getting a correct response. After a few days I have some code that will use a laptop hooked up to the arduino through usb to send and receive to the obd board. The obd tx goes to pin 2 and the rx goes to pin 3 on the arduino. I have this working on my ford contour so I hope this will help you. Make sure to send the atz command before trying any other command.
Code: Select all#include <SoftwareSerial.h>
SoftwareSerial obd(2,3);
char inChar=0;
char inOBD=0;
char rxIndex=0;
char rxDataPC[20];
char rxDataCar[30];
void setup()
{
obd.begin(9600);
Serial.begin(9600);
}
void loop(){
inChar=0;
while(Serial.available()>0){
if(Serial.available() > 0){
//Start by checking if we've received the end of message character ('\r').
if(Serial.peek() == '\r'){
//Clear the Serial buffer
inChar=Serial.read();
//Put the end of string character on our data string
rxDataPC[rxIndex]='\0';
//Reset the buffer index so that the next character goes back at the beginning of the string.
rxIndex=0;
obd.println(rxDataPC);
inChar=0;
getResponse();
delay(20);
getResponse();
}
//If we didn't get the end of message character, just add the new character to the string.
else{
//Get the new character from the Serial port.
inChar = Serial.read();
//Add the new character to the string, and increment the index variable.
rxDataPC[rxIndex++]=inChar;
}
}
}
}
void getResponse(void){
char obdIn=0;
int i=0;
int start=millis();
//If nothing is currently available do nothing and break after 3 seconds
while(obd.available()==0){if(millis()-start>3000){break;}}
while(obd.available()){
//check to see if end of line/message
if (obd.peek()=='\r'){
obdIn=obd.read();
rxDataCar[i]='\0';
Serial.println(rxDataCar);
i=0;
}
// The prompt is sometimes the only thing recieved so this needs to be taken care of
else if(obd.peek()=='>'){
obdIn=obd.read();
Serial.write(obdIn);
}
// Add next character to string
else{
obdIn=obd.read();
rxDataCar[i++]=obdIn;
}
}
}
If you have any questions feel free to ask I check the forums often