I have written code for my arduino cdc emulator.
how its should work: cd emulator listing RX until recieving"8D 0F XX XX XX" then should respond to stereo 8D 93 01
Transmition part working fine.
but I still facing problem with recieving and recognizing data. 8D OF
8D HEX TO DECIMAL=141
0F HEX TO DECIMAL =15
I have used following command if ((strtol(&rxData[0],0,16)==141) and (strtol(&rxData[3],0,16)==15)) {
the ELM and Arduino only work if change to strtol(&rxData[0],0,16)>0
its work, but not stable. plus volume regulator become inactive.
Seems that I missed something here....
here is my code:
Code: Select all
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(7, 8); // TX | RX
//char character;
//char DATAFROMMAFON=0;
char rxData[20];
char rxIndex=0;
int led = 13;
void setup()
{
Serial.begin(38400);
BTSerial.begin(38400);
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
//BTSerial.flush();
BTSerial.write(Serial.read());
BTSerial.write("ATL1");
BTSerial.println();
delay (500);
BTSerial.write("ATH1");
BTSerial.println();
delay (500);
BTSerial.write("ATS1");
BTSerial.println();
delay (500);
BTSerial.write("ATAL");
BTSerial.println();
delay (500);
BTSerial.write("ATE0");
BTSerial.println();
delay (500);
Serial.println("ATMA"); //J1850
BTSerial.println();
delay (500);
if (BTSerial.available() > 0) {
BTSerial.println("ATSH8D9301");// Hi Im CD changer!
BTSerial.println();
delay(50);
BTSerial.println("0000");
BTSerial.println();
delay(200);\
}
}
void loop() {// run over and over
BTSerial.flush();
getResponse();
getResponse();//
if ((strtol(&rxData[0],0,16)==141) and (strtol(&rxData[3],0,16)==15)) {
digitalWrite(13, HIGH);
BTSerial.println("ATSH8D9301");//
BTSerial.println();
delay(20);
BTSerial.println("0000"); //п
BTSerial.println();
delay(150);
}
}
void getResponse(void){
char inChar=0;
{
while(inChar != '\r'){
if(BTSerial.available() > 0){
if(BTSerial.peek() == '\r'){
inChar=BTSerial.read();
rxData[rxIndex]='\0';
rxIndex=0;
}
else {
inChar = BTSerial.read();
rxData[rxIndex++]=inChar;
}
}
}
}
}