Hi everyone,
My project is to develop a device that could communicate with vehicle and acquire the vehicle behavior parameter such as engine load, rpm and etc. I'm using ELM327 as the interpreter and Arduino Mega 2560 as the microcontroller.
Currently I'm in the begining stage of my project and I'm trying have some basic communication with ELM327 via Mega2560 without connecting them to the vehicles.
The basic Command i would like to send is:
ATZ (reset the chips)
ATI (request for the product ID string) - expected reply: ELM327 v1.3a
AT@1 (request fr the internal device description string to be printed) - expected reply: OBDII to RS232 Interpreter
The reference project i able to get from internet is the obduino32k but the code is too complicated for me to understand. Then i found this code in arduino forumhttp://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290148115/all and make some modification to fit my requirement. the LEDs tat indicate the transmitting and receiving data process of the ELM did blink after the compilation of the code, yet there is no reply being displayed in the serial monitor. Hence i assumed that the elm able to receive the command but unable to reply for it (or maybe 2560 could not read the reply).
I wonder whats wrong with my code? =(
#include <string.h>
#undef int // bug from Arduino IDE 0011
#include <stdio.h>
#define ASERIAL elm327
#include <NewSoftSerial.h>
#define NUL '\0'
#define CR '\r' // carriage return = 0x0d = 13
#define PROMPT '>'
#define rxPin 10
#define txPin 11
NewSoftSerial elm327(rxPin, txPin);
char elmrx[16];
char r;
int i=0;
void setup()
{
Serial.begin(38400);
ASERIAL.begin(38400);
Serial.flush();
ASERIAL.flush();
}
void loop()
{
Serial.print("Reset elm");
elm327.println("ATZ\r");
delay(1000);
elm327.println("ATZ\r");
delay(1000);
read_reply();
elm327.println("ATI\r");
Serial.print("ATI");
delay(1000);
read_reply();
Serial.flush();
elm327.flush();
//while(1){}
}
void read_reply()
{
while((r=elm327.read()) != PROMPT)
{
r=ASERIAL.read();
elmrx[i]=r;
i++;
}
elmrx[i]='\0'; //end the array with null
r=NUL; //clear the variable r
Serial.println(elmrx);
Serial.flush();
elm327.flush();
}
The reply of ELM is in ASCII. does it matter in this case? or the IDE would convert it to the normal character by itself?