Interacting with ELM327

Hi,

im trying to read from ELM327 connected to the OBD interface of my car. It seems to work, but there is an issue with the reception.

My Serial output:

[ELM327 v1.3a]
[>ATE0]
[>OK]
[>OK]
[>SEARCHING…41 0C 00441 441441 0C 00 00 ]
[441 0C 41 441 0441 0441 0C 00 00 ]
[>41 0C 00 00 ]
[>41 0C 00 00 ]
[>41 0C 00 00 ]
[>41 0C 00 00 ]
[>41 0C 00 00 ]
[>41 0C 00 00 ]
[>41 0C 00 00 ]
[>4141 0C 00 00 ]
[441 0C 00 00 ]
[441 0C 00 00 ]
[441 0C 00 00 ]
[441 0C 00 00 ]
[>4141 0C 00 00 ]
[441 0C 00 00 ]
[441 0C 00 00 ]
[441 0C 00 00 ]
[441 0C 00 00 ]
[>4141 0C 00 00 ]

As you see there is corrupted data every 5 lines. Do you know why this happens?

My code:

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

String inputString = "";         // a string to hold incoming data
SoftwareSerial softwareSerial(8, 9);


int  serIn;
char serInString[100];  // array that will hold the different bytes  100=100characters;
                        // -> you must state how long the array will be else it won't work.
int  serInIndx  = 0;    // index of serInString[] in which to insert the next incoming byte
int  serOutIndx = 0;    // index of the outgoing serInString[] array;

//This is a character buffer that will store the data from the serial port
char rxData[20];
char rxIndex=0;

//Variables to hold the speed and RPM data.
int vehicleSpeed=0;
int vehicleRPM=0;

void setup()
{
  Serial.begin(9600);
  softwareSerial.begin(19200); 

  softwareSerial.println("Start...");
  delay(1000);
  
  Serial.flush();
  Serial.println("ATZ");
  delay(1000);
  Serial.print("ATZ\r");
  delay(5000);
  
  Serial.print("ATE0\r");
  delay(1000);
  
  Serial.print("ATL0\r");
  delay(1000);
  
  Serial.print("ATSP0\r");
  delay(1000);

  Serial.print("0100\r");
  delay(5000);  
}


void readOBD() {
     char inChar;
  String rec = "[";
 // only if there are bytes in the serial buffer execute the following code
  if(Serial.available()) {    
       
    //keep reading and printing from serial untill there are bytes in the serial buffer
     while (Serial.available()>0){
        inChar = Serial.read();  //read Serial        
         if(inChar == '\r' || inChar == '\n') break;
        rec += String(inChar);
     }
     rec += "]";
     if(rec.length() > 4) 
     {
       softwareSerial.println(rec);
     }
     else {
       rec = "";
     }
  }
}

void loop()
{
  Serial.println("010C");
  readOBD();

  delay(200);
}

Do you know why this happens?

Since you have not defined what is connected to the Serial instance, or what the SoftwareSerial instance is for, no.

There REALLY is no excuse for using Strings to read data that appears to have a relatively stable length.

I am having a similar issue, did someone fix the problem

celica:
I am having a similar issue, did someone fix the problem

That would be an inability to provide sufficient information to allow us to help.

No, no one has been able to fix that problem.

I open another post…for this reason I did not give more information…my fault.

Cut/Paste of my post:

Hi everyone,
being very expert about the CAN bus, I am trying to hack the ELM327 using the following hardware:

ELM327 - Bluetooh HC-05 - Arduino UNO

Unfortunately, I have some trouble reading/scannning CAN messages.
When I set the EML327, it seems that everything is fine (I can read ‘OK’ after every AT command), but when I try to scan everything using ATMA the device stop working after 2-3 CAN messages…moreover the error is not always the same.

According to me is the setting of the device that is wrong.

I would appreciate someone that can fix my code or advice other avenues.

Example_basic_bluetooth_3.ino (4.66 KB)

@celica, do not hijack. Do not cross-post. Thread locked.