Quote:
Originally Posted by mabviper
Anytime I have timing issues, I usually relax all timings. For CAN, the identifier ID also relates to priority. The higher the number, the lower the priority. If you're reading all values by sending out a request for each value, I suggest increasing your loop delay.
Sent from my Nexus 4 using Tapatalk
|
I'm sending out a request like this:
Code:
if (whichSensor.indexOf("obdbrzoiltempf") >= 0){
Serial1.println("2101");
getResponse2();
Serial.println("brz oil temp");
delay(40);
value = ((float)strtol(&rxData[109],0,16) - 40) * 1.8 + 32;
Serial.println(value);
Serial1 is the obd ii uart device.
Serial is just a usb serial monitor (standard arduino stuff)
There are two versions of getResponse() that I've been toying with. One of which uses peek in a nice way. I think that's the right one to work on. I need to modify it to pick up multiple lines in the return of a CAN message.
Code:
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(Serial1.available()==0){if(millis()-start>3000){break;}}
while(Serial1.available()){
//check to see if end of line/message
if (Serial1.peek()=='\r'){
obdIn=Serial1.read();
rxData[i]='\0';
Serial.println(rxData);
i=0;
}
// The prompt is sometimes the only thing recieved so this needs to be taken care of
else if(Serial1.peek()=='>'){
obdIn=Serial1.read();
//Serial.write(obdIn);
}
// Add next character to string
else{
obdIn=Serial1.read();
rxData[i++]=obdIn;
}
}
Serial.print("rxData(in getResponse): ");
Serial.println(rxData);
rxIndex=0;
}
This is the other one:
Code:
//from: https://forum.sparkfun.com/viewtopic.php?f=14&t=32457&start=60 and https://forum.sparkfun.com/viewtopic.php?f=14&t=38253
void getResponse2(void){
char c;
int start=millis();
//If nothing is currently available do nothing and break after 3 seconds
while(Serial1.available()==0){if(millis()-start>3000){break;}}
do {
if (Serial1.available() > 0)
{
c = Serial1.read();
if ((c != '>') && (c != '\r') && (c != '\n')) //Keep these out of our buffer
{
rxData[rxIndex++] = c; //Add whatever we receive to the buffer
}
}
}
while (c != '>'); //The ELM327 ends its response with this char so when we get it we exit out.
rxData[rxIndex++] = '\0'; //Converts the array into a string
Serial.print("rxData(in getResponse2): ");
Serial.println(rxData);
rxIndex = 0; //Set this to 0 so next time we call the read we get a "clean" buffer
}
I'm sure I've screwed something fundamental up here.
I can use a basic program to send typed commands directly to the device and it seems to work, which leads me to believe that this is timing related.
At the moment after doing an ATZ and ATE0 the first display page/sensor reading is water temp and it just returns "?".
If I stick entirely to using the second getResponse (without the peek). Everything works, except the water temp...this is what really has me stumped.