Hello everybody,
I have a little project running which uses the serial connection of the Teensy LC (Serial1) to read out data from an ELM327 device connected to my car.
I use a slightly modified version of irvine's great elm327 library for arduino. https://github.com/irvined1982/arduino-ELM327
My project runs very fine I'm displaying data that i collect from my car in realtime on a led display. The only downside of the ELM library is that it is blocking the whole process until it received an answer from the ELM device. This can take up to 60ms so far. So i decided to update the LEDs with an interrupt which runs with an IntervalTimer at 33ms. I have a LDR which is dimming my lights thats why it was senseful to do so.
But once I update the LEDs with the interrupt it seems like my serial has big issues. After is sent a command to the ELM device, the code waits for an response. This is a simple while loop and it has a timeout of 9 sec so if there is no answer it will automatically return. However when I enable the 33ms interrupt led update routine it works for around 1 sec and then get stuck inside the while loop. Now the timeout still works, but I will never again receive any more answer from my ELM device.
Reading the arduino reference it says
Can anyone give me some hints or advices which allow me to update the leds properly at 33ms while receiving data over the serial1 port in the main loop?
The corresponding send/receive code is shown below. The serial print of millis() works fine. The Serial.available() however returns 0 after a very short time.
Just for clarification the code below is executed "normally" in the loop(). My leds get updated in a seperate fucntion that is called by the IntervalTimer.
I have a little project running which uses the serial connection of the Teensy LC (Serial1) to read out data from an ELM327 device connected to my car.
I use a slightly modified version of irvine's great elm327 library for arduino. https://github.com/irvined1982/arduino-ELM327
My project runs very fine I'm displaying data that i collect from my car in realtime on a led display. The only downside of the ELM library is that it is blocking the whole process until it received an answer from the ELM device. This can take up to 60ms so far. So i decided to update the LEDs with an interrupt which runs with an IntervalTimer at 33ms. I have a LDR which is dimming my lights thats why it was senseful to do so.
But once I update the LEDs with the interrupt it seems like my serial has big issues. After is sent a command to the ELM device, the code waits for an response. This is a simple while loop and it has a timeout of 9 sec so if there is no answer it will automatically return. However when I enable the 33ms interrupt led update routine it works for around 1 sec and then get stuck inside the while loop. Now the timeout still works, but I will never again receive any more answer from my ELM device.
Reading the arduino reference it says
when enabling interrupts (https://www.arduino.cc/en/Reference/Interrupts). And it seems like this is my problem. What I ask myself now is how I can solve this without changing the library too much. (I know i could just remove the while loop and somehow make it non-blocking without using interrupts but this seems to be far more complicated to me since I would need some kind of command stack).incoming communication may be ignored
Can anyone give me some hints or advices which allow me to update the leds properly at 33ms while receiving data over the serial1 port in the main loop?
The corresponding send/receive code is shown below. The serial print of millis() works fine. The Serial.available() however returns 0 after a very short time.
Just for clarification the code below is executed "normally" in the loop(). My leds get updated in a seperate fucntion that is called by the IntervalTimer.
Code:
byte cmdLength;
// Flush any leftover data from the last command.
// Send the specified command to the controller.
flush();
ELM_PORT.print(cmd);
ELM_PORT.print('\r');
unsigned long timeOut;
int counter;
bool found;
// Start reading the data right away and don't stop
// until either the requested number of bytes has
// been read or the timeout is reached, or the >
// has been returned.
//
counter=0;
timeOut=millis()+ELM_TIMEOUT;
found=false;
while (!found && counter<( dataLength ) && millis()<timeOut)
{
Serial.print("Millis: ");Serial.println(millis());
if ( ELM_PORT.available() ){
Serial.print("Bytes available: "); Serial.println(ELM_PORT.available());
data[counter]=ELM_PORT.read();
if ( data[counter] == '>' ){
found=true;
data[counter]='\0';
}else{
++counter;
}
}
}