f211

Dependencies:   mbed-dev1

Fork of OBD21121 by Surendar S

Revision:
8:cc3cb620c3bd
Parent:
7:42cb4b01909c
Child:
9:a3bdc9a71466
--- a/main.cpp	Fri Mar 24 14:16:24 2017 +0000
+++ b/main.cpp	Fri Apr 28 08:42:35 2017 +0000
@@ -1,131 +1,72 @@
 #include "mbed.h"
 #include "functions.h"
 
-#define ENGINE_RPM          0x0D
-#define VEHICLE_SPEED       13/*0x0D*/
 #define PID_REQUEST         0x7DF
 #define PID_REPLY           0x7E8
 
-//??USE ARRAYLIST Of Int
-#define CANSPEED_500      500000        // CAN speed at 500 kbps
-#define CANSPEED_250      250000        // CAN speed at 250 kbps
-#define CANSPEED_125      125000        // CAN speed at 125 kbps
+#define CANSPEED_500      500000
+#define CANSPEED_250      250000
+#define CANSPEED_125      125000
 #define sendDebugMessage  1
 #define pushDebugData 1
 
-const int kLineW1=300;
-const int kLineW2=20;
-const int kLineW3=20;
-const int kLineW4=34;
-const int kLineP1=20;
-const int kLineP2=50;
-const int kLineP3=5000;
-const int kLineP4=14;
+const int canSpeeds[3]= {CANSPEED_125,CANSPEED_500,CANSPEED_250};
+Serial serialPorts[2] = {Serial(P0_11,P0_13),Serial(P0_15,P0_14)};
+Serial *kLineUART=NULL;
+DigitalOut *klineWakeup=new DigitalOut(P0_28);
+CAN canBus(P0_22, P0_23);
 
-
-const int canSpeeds[3]= {CANSPEED_125,CANSPEED_500,CANSPEED_250};
-char kLineCmd[6]={'\0','\0','\0','\0','\0','\0'};
-int kLineCommandCount=0;
+Ticker timeCounter;
+Timer OBDTimer;
+CANMessage can_MsgRx;
+char kLineCmd[6]= {'\0','\0','\0','\0','\0','\0'};
 
-DigitalOut myled(LED2);
-DigitalOut canDriverPowerSave(LED1);
-InterruptIn canActivity(P0_5);
-RawSerial serialPorts[2] = {RawSerial(P0_22,P0_23),RawSerial(P0_15,P0_14)};
-RawSerial *kLineUART=NULL;//(P0_26,P0_6),
-DigitalOut *klineWakeup=new DigitalOut(P0_26);
-Ticker timeCounter;
-uint8_t calculateChecksum(char *data,int len);
+void clearOBDCommand();
+void flushBuffer();
 void sendCommandToVehicle(char *_cmd);
+void setKLineMsg(char *_cmd);
+void flushBuffer();
+void kLinekeepalive();
+char tolower(unsigned char ch);
 int findStringLength(char *_cmd);
-void clearOBDCommand();
-int isVehicleCommandProcessing=0;
-void flushBuffer();
-int KlineReadCount=0;
-//Serial pc(P0_9,P0_29);
 
-CAN canBus(P0_24, P0_27);
-
-//AnalogIn batteryVoltage(P0_6);
-//DigitalOut indicatorLEDs[3] = {P0_22,P0_23,P0_6};
-DigitalOut kline(P0_1);
-
-//UART Counters
-int kLine=0;
+int isPoweredupNow=1;
 int interFace=1;
 int debug=1;
-
-
 int currentOBDProtocol=0;
-
 const int protCan=1;
 const int protkLine9141=2;
 const int protkLine14230=3;
 int canFrequency;
 int cmd;
-//LED Counters
-
-Ticker timeCounter2;
 char can_msg[8];
-Timer OBDTimer;
-
-//Data Storage for incoming data on Interface
 char *incomingDataInInterFace;
 const int incomingDataInInterFaceSize=256;
 int incomingDataInInterFaceCounter=0;
-
-//Data Storage for incoming data on OBD
-char *incomingDataInOBD;
+char * incomingDataInOBD;
 const int incomingDataInOBDSize=256;
-CANMessage can_MsgRx;
 int incomingDataInOBDCounter=0;
-int emergencyRestart=0;
-
-char *kLineMessage;
-const int kLineMessageSize=8;
-
-
-//OBD Command data
-char* charPid;
-char* charID;
-int pid;
-int ID;
-
-char kLine9141[]  = {0x68, 0x6a, 0xf1, 0x00, 0x00,0x00};
-char kLine14230[] = {0xc2, 0x33, 0xf1, 0x00, 0x00,0x00};
-char fastKLine14230[] = {0xC1, 0x33, 0xF1, 0x81, 0x66};
-
-
-//Wait Duration for OBD comm
 const int waitTimeForOBDComm=2000;
-//Sleep time between commands
 const int sleepTime=25000;
-//Time to send chip to power save mode
 const int powerSaveTime=120;
-
-//Global Variables
-//New Data receivd on OBD or Interface
 int OBDCmdReceived=0;
 int interfaceCmdReceived=0;
-
-//Last activity time on OBD Interface
 int lastOBDCmdRcvdTime=0;
 int lastInterfaceCmdRcvdTime=0;
-
-
-void timeCounterFunction2() {
-    
-}
+int KlineReadCount=0;
+int isVehicleCommandProcessing=0;
+int kLineCommandCount=0;
+char kByte=0;
 
 void readInterface()
 {
     char c;
-    serialPorts[debug].puts("iS:");
     while(serialPorts[interFace].readable()) {
         lastInterfaceCmdRcvdTime=0;
         c = serialPorts[interFace].getc();
 
         serialPorts[debug].putc(c);
-        
+
         if(incomingDataInInterFaceCounter<incomingDataInInterFaceSize && c!='\r' && c!='\n') {
             incomingDataInInterFace[incomingDataInInterFaceCounter] = c;
         }
@@ -134,129 +75,93 @@
         if(c=='\r') {
             interfaceCmdReceived=1;
             serialPorts[debug].puts("nl\r\n");
-          //  emergencyRestart=0;
-        }
-        /*if(c=='!') {
-            emergencyRestart++;
-            if(emergencyRestart>3) {
-                NVIC_SystemReset();
-            }
-        }*/
-
-    }
-}
-void readKLine()
-{
-    /*char c;
-    while(serialPorts[kLine].readable()) {
-        c = serialPorts[kLine].getc();
-        if(incomingDataInOBDCounter<incomingDataInOBDSize && c!='\r' && c!='\n') {
-            incomingDataInOBD[incomingDataInOBDCounter]=c;
-            incomingDataInOBDCounter++;
-        }
-        if(c=='\n') {
-            OBDCmdReceived++;
         }
 
-    }*/
-    char c;
-    while(kLineUART->readable()) {
-        c = kLineUART->getc();
-        if(incomingDataInOBDCounter<incomingDataInOBDSize && c!='\r' && c!='\n') {
-            incomingDataInOBD[incomingDataInOBDCounter]=c;
-            incomingDataInOBDCounter++;
-            serialPorts[debug].putc(c);
-        }
-        
     }
 }
 
 void canReader()
 {
     if (canBus.read(can_MsgRx)) {
-        //Setting
         lastOBDCmdRcvdTime=0;
-        //serialPorts[debug].puts("HRHK2");
-        if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == cmd)) {
-            serialPorts[debug].puts("HRHK");
+        if ((can_MsgRx.id == PID_REPLY)) {
             OBDCmdReceived=1;
-            for (int i = 0; i < (int)can_MsgRx.len; i++) {
-                serialPorts[debug].printf("%d:%x , ", i, can_MsgRx.data[i]);
-                incomingDataInOBD[incomingDataInOBDCounter]=can_MsgRx.data[i];
-                incomingDataInOBDCounter++;
+            for (int i = 0; i < (int)can_MsgRx.len && currentOBDProtocol!=0; i++) {
+                char c[2];
+                sprintf(c, "%02X",can_MsgRx.data[i]);
+                serialPorts[interFace].puts(c);
             }
         }
     }
 }
 
+void readKLine()
+{
+    char c;
+    while(kLineUART->readable()) {
+        c=kLineUART->getc();
+        serialPorts[interFace].printf("%02x",c);
+    }
+}
+
+
 void timeCounterFunction()
 {
     lastInterfaceCmdRcvdTime++;
     lastOBDCmdRcvdTime++;
     KlineReadCount++;
-    if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3){
+    if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3) {
         KlineReadCount=0;
-        if(incomingDataInInterFaceCounter){
+        if(incomingDataInInterFaceCounter) {
             return;
         }
-        sendCommandToVehicle("010D");
+        kLinekeepalive();
     }
-    //IF timout is over certain amount just shutdown
+
 }
 
 int main()
 {
-
+    serialPorts[interFace].baud(9600);
     serialPorts[debug].puts("f2119\r\n");
-    //Initialization
     incomingDataInInterFace=(char *)malloc(incomingDataInInterFaceSize*sizeof(char));
     incomingDataInOBD=(char *)malloc(incomingDataInOBDSize*sizeof(char));
+    *klineWakeup=0;
 
     clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
     clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
-
-
+    wait_ms(300);
     timeCounter.attach(&timeCounterFunction, 1.0);
     serialPorts[interFace].attach(&readInterface);
     serialPorts[interFace].puts("TU211 v1.0\r\n");
-    //checkCommunicationProtocol();
-
-
     while(1) {
-//        canDriverPowerSave=1;
         if(interfaceCmdReceived && checkForLocalInterfaceCommand()) {
+            serialPorts[debug].puts("Going to clear UART data\r\n");
             clearInterfaceCommand();
+            serialPorts[debug].puts("UART data cleared\r\n");
         }
-
-        if(currentOBDProtocol==0 && interfaceCmdReceived) {
+        if(currentOBDProtocol==0 && isPoweredupNow) {
+            isPoweredupNow=0;
 #if sendDebugMessage
             serialPorts[debug].puts("Going to figure protocol\r\n");
 #endif
-            canBus.attach(NULL);
-            serialPorts[interFace].puts("Searching...\r\n");
             serialPorts[debug].puts("Searching...\r\n");
             checkCommunicationProtocol();
             if(!currentOBDProtocol) {
 #if sendDebugMessage
                 serialPorts[debug].puts("!Unknownprotocol\r\n");
 #endif
-                //Goto Sleep?
             } else if(currentOBDProtocol==protCan) {
 #if sendDebugMessage
                 serialPorts[debug].puts("can Protocol\r\n");
 #endif
                 serialPorts[interFace].puts("OK\r\n");
-                canBus.attach(&canReader);
-            } else if(currentOBDProtocol==protkLine14230){
-                //serialPorts[interFace].puts("OK\r\n");
-                //serialPorts[kLine].attach(&readKLine);
+            } else if(currentOBDProtocol==protkLine14230) {
 #if sendDebugMessage
                 serialPorts[debug].puts("Kline Protocol\r\n");
 #endif
-                timeCounter2.attach(&timeCounterFunction2, 3.0);
             }
         }
-
         if(interfaceCmdReceived && checkForLocalInterfaceCommand()) {
             serialPorts[debug].puts("Going to clear UART data\r\n");
             clearInterfaceCommand();
@@ -265,96 +170,81 @@
         if(interfaceCmdReceived && currentOBDProtocol) {
             serialPorts[debug].puts("Going to process user command\r\n");
             clearOBDCommand();
-            if(currentOBDProtocol==protCan){
+            if(currentOBDProtocol==protCan) {
                 processUserCommand();
-            }else if(currentOBDProtocol==protkLine14230){
-                serialPorts[debug].printf("Command Received\r\n");
-                wait_ms(200);
+            } else if(currentOBDProtocol==protkLine14230) {
                 while(isVehicleCommandProcessing);
                 flushBuffer();
-                kLineUART->attach(&readKLine);
+                /*kLineUART->attach(NULL);
+                kLineUART->attach(&readKLine);*/
                 sendCommandToVehicle(incomingDataInInterFace);
                 wait_ms(500);
                 serialPorts[debug].printf("\r\n");
-                kLineUART->attach(NULL);
+            } else {
+                clearInterfaceCommand();
+                serialPorts[interFace].puts("?\r\n");
+#if sendDebugMessage
+                serialPorts[debug].puts("?\r\n");
+#endif
             }
-        } else {
+        } else if(interfaceCmdReceived) {
             clearInterfaceCommand();
             serialPorts[interFace].puts("?\r\n");
 #if sendDebugMessage
             serialPorts[debug].puts("?\r\n");
 #endif
         }
-        myled=0;
-        if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
+        sleepProcess();
+        /*if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
             powerSaveMode();
         } else {
-            ///???? suren changes
             sleepProcess();
-        }
-        //???? suren changes
-        /*if(interfaceCmdReceived && checkForLocalInterfaceCommand()) {
-            clearInterfaceCommand();
         }*/
-        myled=1;
     }
 }
-
 void checkCommunicationProtocol()
 {
-    
-    /*canBus.frequency(canSpeeds[2]);
-    setCanMessage("010D");
-    if (canBus.write(CANMessage(PID_REQUEST, can_msg, 8))) {
-            serialPorts[debug].printf("done\r\n");
-        }*/
-#if sendDebugMessage
-    serialPorts[debug].puts("check Communication protocal ENT\r\n");
-#endif
+    /* #if sendDebugMessage
+     serialPorts[debug].puts("check Communication protocal ENT\r\n");
+    #endif*/
     int i=0;
     do {
 #if sendDebugMessage
         serialPorts[debug].puts("CanBusCheck\r\n");
 #endif
+        canBus.reset();
         canBus.frequency(canSpeeds[i]);
+        canBus.attach(NULL);
         canBus.attach(&canReader);
-        canBus.reset();
         OBDTimer.reset();
         OBDTimer.start();
         OBDCmdReceived=0;
         setCanMessage("010D");
         if (canBus.write(CANMessage(PID_REQUEST, can_msg, 8))) {
-#if sendDebugMessage
-            serialPorts[debug].printf("OBDTIMER:%d\r\n",OBDTimer.read_ms());
-#endif
+            /*#if sendDebugMessage
+                       serialPorts[debug].printf("OBDTIMER:%d\r\n",OBDTimer.read_ms());
+            #endif*/
             while(OBDTimer.read_ms() < waitTimeForOBDComm) {
                 if(OBDCmdReceived) {
                     currentOBDProtocol=protCan;
                     canFrequency=i;
-#if sendDebugMessage
-                    serialPorts[debug].puts("OBD ResPonse is : ");
-                    serialPorts[debug].puts(incomingDataInOBD);
-                    serialPorts[debug].puts("check Communication protocal SET\r\n");
-#endif
-                    return;
+                    break;
                 }
             }
         }
         wait_ms(200);
         i++;
-        canBus.attach(NULL);
-    } while(i<3);
+    } while(i<3 && canFrequency==0);
     OBDTimer.stop();
-    if(!currentOBDProtocol){
+    canBus.attach(NULL);
+    if(!currentOBDProtocol) {
         //Kline interface check
         initializeKLine();
     }
-    
 #if sendDebugMessage
     serialPorts[debug].puts("check Communication protocal EXT\r\n");
 #endif
 }
-
 void clearMemoryLocation( char *base, int _size)
 {
     for(int forCount=0; forCount<_size; forCount++) {
@@ -363,13 +253,17 @@
 }
 void setCanMessage(char* _cmd)
 {
+    serialPorts[debug].puts("user Command :");
+    serialPorts[debug].puts(_cmd);
+    serialPorts[debug].puts("\r\n");
+    int len=findStringLength(_cmd)/2;
     char _mode[2]="";
     for(int i=0; i<2; i++) {
         _mode[i]=_cmd[i];
     }
     int mode=strtol(_mode,NULL, 16);
     cmd=strtol((_cmd+2),NULL, 16);
-    can_msg[0] = 0x02;
+    can_msg[0] = len;
     can_msg[1] = mode;
     can_msg[2] = cmd;
     can_msg[3] = 0;
@@ -378,22 +272,17 @@
     can_msg[6] = 0;
     can_msg[7] = 0;
 }
-
 void processUserCommand()
 {
     OBDTimer.reset();
     setCanMessage(incomingDataInInterFace);
     OBDTimer.start();
     OBDCmdReceived=0;
+    canBus.attach(NULL);
+    canBus.attach(&canReader);
     if (canBus.write(CANMessage(PID_REQUEST, can_msg, 8))) {
         while(OBDTimer.read_ms() < waitTimeForOBDComm) {
             if(OBDCmdReceived) {
-                sendResponseToInterface();
-#if sendDebugMessage
-                serialPorts[debug].puts("OBD ResPonse is : ");
-                serialPorts[debug].puts(incomingDataInOBD);
-                serialPorts[debug].puts("check Communication protocal SET\r\n");
-#endif
                 clearInterfaceCommand();
                 break;
             }
@@ -402,12 +291,10 @@
     OBDTimer.stop();
     if(!OBDCmdReceived) {
         clearInterfaceCommand();
-        serialPorts[interFace].puts("?\r\n");
+        serialPorts[interFace].puts("?");
     }
-    interfaceCmdReceived=0;
+    serialPorts[interFace].puts("\r\n");
 }
-
-
 void sleepProcess()
 {
 
@@ -424,94 +311,32 @@
 
 void powerSaveMode()
 {
-    //???? suren changes begin
 #if pushDebugData
     serialPorts[debug].puts("PowerSaveOn\r\n");
 #endif
-    canDriverPowerSave=0;
     wait(20);
 #if pushDebugData
     serialPorts[debug].puts("PowerSaveOff\r\n");
 #endif
-//???? suren changes end
-    //Putting CAn Driver on low power
-    //Detect can Activity
-    /*   if(currentOBDProtocol==protCan) {
-           canDriverPowerSave=1;
-       }
-
-    #if pushDebugData
-       serialPorts[debug].puts("PowerSaveOn\r\n");
-    #endif
-       //Remove pin allocaitons
-
-       //Detect can Activity On Acnbus
-       if(currentOBDProtocol==protCan) {
-           canActivity.rise(&activityOnCan);
-           canActivity.fall(&activityOnCan);
-       }
-
-
-       wait(20);
-       if(currentOBDProtocol==protCan) {
-           canDriverPowerSave=0;
-       }
-
-       //Make pin allocaitons
-
-       //Detect can Activity On Acnbus
-       if(currentOBDProtocol==protCan) {
-           canActivity.rise(NULL);
-           canActivity.fall(NULL);
-       }
-
-       lastOBDCmdRcvdTime=0;
-       lastInterfaceCmdRcvdTime=0;
-
-    #if pushDebugData
-       serialPorts[debug].puts("PowerSaveOff\r\n");
-    #endif
-    */
-
 }
-
-void tprintf( char* str, int num)
-{
-    int i2aCounter=0;
-    if (num == 0) {
-        str[i2aCounter++] = '0';
-        str[i2aCounter] = '\0';
-        return;
-    }
-    int tempNum=num;
-    while(tempNum!=0) {
-        tempNum/=10;
-        i2aCounter++;
-    }
-    str[i2aCounter]='\0';
-    i2aCounter--;
-    while(i2aCounter>=0) {
-        str[i2aCounter--] = (num % 10) + '0';
-        num = num/10;
-    }
-    return;
-}
-
 int checkForLocalInterfaceCommand()
 {
     serialPorts[debug].puts("CommandDETECTED\r\n");
+    int i=0;
+    while(incomingDataInInterFace[i]!='\0'){
+        incomingDataInInterFace[i]=tolower(incomingDataInInterFace[i]);
+        i++;
+    }
     char temp[2]= {incomingDataInInterFace[0],incomingDataInInterFace[1]};
-    serialPorts[debug].puts(temp);
-    serialPorts[debug].puts(incomingDataInInterFace);
-    if(strcmp(temp,"AT")!=0) {
+    if(strcmp(temp,"at")!=0) {
         serialPorts[debug].puts("Not Local Command\r\n");
         return 0;
     }
-    if(strcmp(incomingDataInInterFace,"ATZ")==0) {
+    if(strcmp(incomingDataInInterFace,"atz")==0) {
         NVIC_SystemReset();
     }
-    if(strcmp(incomingDataInInterFace,"ATRV")==0) {
-        serialPorts[debug].puts("Gointoreport bat vol\r\n");
+    if(strcmp(incomingDataInInterFace,"atrv")==0) {
+        serialPorts[interFace].puts("12.1V\r\n");
         clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
         return 1;
     }
@@ -534,178 +359,56 @@
 void clearOBDCommand()
 {
     clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
-    incomingDataInInterFaceCounter=0;
-    interfaceCmdReceived=0;
+    incomingDataInOBDCounter=0;
+    OBDCmdReceived=0;
+}
+int findStringLength(char *_cmd)
+{
+    int i=0;
+    for(i=0; _cmd[i]!='\0'; ++i);
+    return i;
+}
+int initializeKLine()
+{
+#if sendDebugMessage
+    serialPorts[debug].puts("K-Line Check\r\n");
+#endif
+    *klineWakeup=1;
+    wait_ms(300);
+    *klineWakeup=0;
+    wait_ms(25);
+    *klineWakeup=1;
+    wait_ms(25);
+    delete klineWakeup;
+    kLineUART=new Serial(P0_28,P0_27);
+    kLineUART->baud(10400);
+    kLineUART->attach(&readKLine);
+    uint8_t crc=0;
+    crc=crc+0xC1;
+    crc=crc+0x33;
+    crc=crc+0xF1;
+    crc=crc+0x81;
+    kLineUART->putc(0xC1);
+    kLineUART->putc(0x33);
+    kLineUART->putc(0xF1);
+    kLineUART->putc(0x81);
+    kLineUART->putc(crc);
+    wait(1);
+    //have to check K-line Response
+    currentOBDProtocol=protkLine14230; // have to remove this line
+    return 0;
 }
 
-int initializeKLine(){
-     *klineWakeup=0;
-     wait_ms(25);
-     *klineWakeup=1;
-     wait_ms(25);
-     delete klineWakeup;
-     kLineUART=new RawSerial(P0_26,P0_6);
-     kLineUART->attach(&readKLine);
-     kLineUART->baud(10400);
-     uint8_t crc=0;
-        crc=crc+0xC1;
-        crc=crc+0x33;
-        crc=crc+0xF1;
-        crc=crc+0x81;
-        kLineUART->putc(0xC1);
-        kLineUART->putc(0x33);
-        kLineUART->putc(0xF1);
-        kLineUART->putc(0x81);
-        kLineUART->putc(crc);
-        wait(1);
-        /////??? have to remove below line 
-        kLineUART->attach(NULL);
-        currentOBDProtocol=protkLine14230;
-    return 0;
-    serialPorts[kLine].baud(5);
-    serialPorts[kLine].attach(NULL);
-    kline=0; wait_ms(200);  // start
-    kline=1; wait_ms(400);  // first two bits
-    kline=0; wait_ms(400);  // second pair
-    kline=1; wait_ms(400);  // third pair
-    kline=0; wait_ms(400);  // last pair
-    clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
-    lastOBDCmdRcvdTime=0;
-    OBDCmdReceived++;
-    serialPorts[kLine].attach(&readKLine); // just attached listener before complete the the first step
-    kline=1; wait_ms(200);  // stop b
-    //Use PINMUX to move to serial port 
-    //start time count to recevice vehicel data
-    serialPorts[kLine].attach(NULL);
-    /*if (incomingDataInOBD==0x55){ 
-        serialPorts[kLine].baud(10400);
-        clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
-        lastOBDCmdRcvdTime=0;
-        OBDCmdReceived++;
-        serialPorts[kLine].attach(&readKLine);
-        //start time count to recevice vehicel data
-        //serialPorts[kLine].attach(NULL); //dont want detach it. But we have to reset the variables.
-        lastOBDCmdRcvdTime=0;
-        OBDCmdReceived++;
-        if (incomingDataInOBD==0x08 || incomingDataInOBD==0x94){
-            int firstData=incomingDataInOBD;
-            clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
-            //start time count to recevice vehicel data
-            if (incomingDataInOBD==firstData){
-                
-            }
-    }*/
-    OBDTimer.reset();
-    OBDTimer.start();
-    char c;
-    while(OBDTimer.read_ms() < kLineW1){
-        if(serialPorts[kLine].readable()) {   
-            c=serialPorts[kLine].getc();
-        }
-    }
-    if(c!=0x55){
-        return 0;
-    }
-    int dataReceived=0;
-    OBDTimer.reset();
-    OBDTimer.start();
-    
-    while(OBDTimer.read_ms() < kLineW2){
-        if(serialPorts[kLine].readable()) {   
-            c=serialPorts[kLine].getc();
-            dataReceived=1;
-            break;
-            
-        }
-    }
-    if(!dataReceived){
-        return 0;
-    }
-    dataReceived=0;  
-    char d; 
-    OBDTimer.reset();
-    OBDTimer.start();
- 
-    while(OBDTimer.read_ms() < kLineW3){
-        if(serialPorts[kLine].readable()) {   
-            d=serialPorts[kLine].getc();
-            dataReceived++;
-            break;
-        }
-    }
-    OBDTimer.stop();
-    if(!dataReceived){
-        return 0;
-    }
-    // these two should be identical according to the spec.
-    if(c!=d){
-        return 0;
-    }
-
-    // we obtained w1 and w2, now invert and send it back.
-    // tester waits w4 between 25 and 50 ms:
-    wait_ms(kLineW4);    
-    serialPorts[kLine].putc(~c);
- 
-    OBDTimer.reset();
-    OBDTimer.start();
- 
-    while(OBDTimer.read_ms() < kLineW4){
-        if(serialPorts[kLine].readable()) {   
-            d=serialPorts[kLine].getc();
-            dataReceived++;
-            break;
-        }
-    }
-    OBDTimer.stop();
-    if(!dataReceived){
-        return 0;
-    }
-    if(c!=0xCC){
-        return 0;
-    }
-    //Detectig ISO9141 protocol
-    if(c==0x08 || c== 0x96){
-        currentOBDProtocol=protkLine9141;
-
-    }
-    //Detectig ISO14230 protocol
-    if(c==0xef || c== 0x8f){
-        currentOBDProtocol=protkLine14230;        
-    }
-
-    return 1;  
-
-}
-
-/*int fastInitializeKLine(){
-    
-
-}*/
-void writeToKLine(){
-    wait_ms(kLineP4);
-}
-
-int fastInitializeKLine(){
-    kline=0; wait_ms(25);  // start
-    kline=1; wait_ms(25);  // start
-    serialPorts[kLine].baud(10400);
-    
-    for(int k =0;sizeof(fastKLine14230);k++){
-        serialPorts[kLine].putc(fastKLine14230[k]);
-    } 
-   return 1; 
-}
-void setKLineMsg(char *_cmd){
-    for(int i=3;i<6;i++){
+void setKLineMsg(char *_cmd)
+{
+    for(int i=3; i<6; i++) {
         kLineCmd[i]='\0';
     }
     int len=findStringLength(_cmd)/2;
     char _mode[2]="";
     uint8_t crc=0;
-    if(len==1){
+    if(len==1) {
         crc=crc+0xC1;
-        //kLineUART->putc(0xC1);
         kLineCmd[0]=0xC1;
         crc=crc+0x33;
         kLineCmd[1]=(0x33);
@@ -714,7 +417,7 @@
         crc=crc+strtol((_cmd),NULL, 16);
         kLineCmd[3]=(strtol((_cmd),NULL, 16));
         kLineCmd[4]=(crc);
-    }else if(len==2){
+    } else if(len==2) {
         crc=crc+0xC2;
         kLineCmd[0]=(0xC2);
         crc=crc+0x33;
@@ -731,44 +434,40 @@
         kLineCmd[5]=(crc);
     }
 }
-void sendCommandToVehicle(char *_cmd){//kLineUART
+
+void sendCommandToVehicle(char *_cmd)
+{
     isVehicleCommandProcessing=1;
-//    clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
     setKLineMsg(_cmd);
-    for(kLineCommandCount=0;kLineCommandCount<6;kLineCommandCount++){
-        if(kLineCmd[kLineCommandCount]=='\0'){
+    for(kLineCommandCount=0; kLineCommandCount<6; kLineCommandCount++) {
+        if(kLineCmd[kLineCommandCount]=='\0') {
             break;
         }
         kLineUART->putc(kLineCmd[kLineCommandCount]);
-        wait_ms(5);
     }
+    wait_ms(500);
     //have to check given and received commands are same.
     //if it is same we have to read the response else just fush buffer and send the command again
-    ///??? suren code begin
-    wait_ms(500);
-        
-    ///??? suren code end
-    clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
-    interfaceCmdReceived=0;
-    incomingDataInInterFaceCounter=0;
-//    isUserPortAvailable=1;
+    clearInterfaceCommand();
     isVehicleCommandProcessing=0;
 }
 
-
-int findStringLength(char *_cmd){
-    int i=0;
-    for(i=0;_cmd[i]!='\0';++i);
-    return i;
-}
-void flushBuffer(){
-    while(kLineUART->readable()){
+void flushBuffer()
+{
+    while(kLineUART->readable()) {
         kLineUART->getc();
     }
 }
-//byte iso_checksum(byte *data, byte len){
-//  byte crc=0;
-//  for(byte i=0; i<len; i++)
-//    crc=crc+data[i];
-//  return crc;
-//}
+void kLinekeepalive()
+{
+    kLineUART->putc(0xc1);
+    kLineUART->putc(0x33);
+    kLineUART->putc(0xf1);
+    kLineUART->putc(0x3e);
+    kLineUART->putc(0x23);
+}
+char tolower(unsigned char ch) {
+    if (ch >= 'A' && ch <= 'Z')
+        ch = 'a' + (ch - 'A');
+    return ch;
+ }
\ No newline at end of file