f211

Dependencies:   mbed-dev1

Fork of OBD21121 by Surendar S

Revision:
11:853d12fe7ec0
Parent:
10:fe1b976a3f9c
Child:
12:91c72461d70c
--- a/main.cpp	Mon May 08 10:44:36 2017 +0000
+++ b/main.cpp	Tue Jun 13 07:46:28 2017 +0000
@@ -55,7 +55,7 @@
 int incomingDataInOBDCounter=0;
 const int waitTimeForOBDComm=2000;
 const int sleepTime=25000;
-const int powerSaveTime=180;
+const int powerSaveTime=30;
 int OBDCmdReceived=0;
 int interfaceCmdReceived=0;
 int lastOBDCmdRcvdTime=0;
@@ -154,7 +154,7 @@
     clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize);
     clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize);
 
-    wait_ms(1000);
+    wait_ms(800);
     timeCounter.attach(&timeCounterFunction, 1.0);
     serialPorts[interFace]->attach(&readInterface);
     serialPorts[interFace]->puts("+TU211 v1.0\r");
@@ -166,8 +166,8 @@
             clearInterfaceCommand();
             serialPorts[debug]->puts("UART data cleared\r\n");
         }
-        if(currentOBDProtocol==0 && isPoweredupNow) {
-            isPoweredupNow=0;
+        if(currentOBDProtocol==0/* && isPoweredupNow*/) {
+//            isPoweredupNow=0;
 #if sendDebugMessage
             serialPorts[debug]->puts("Going to figure protocol\r\n");
 #endif
@@ -198,9 +198,10 @@
             serialPorts[debug]->puts("Going to process user command\r\n");
             clearOBDCommand();
             if(currentOBDProtocol==protCan) {
+                while(isVehicleCommandProcessing){}
                 processUserCommand(incomingDataInInterFace);
             } else if(currentOBDProtocol==protkLine14230) {
-                while(isVehicleCommandProcessing);
+                while(isVehicleCommandProcessing){}
                 flushBuffer();
                 /*kLineUART->attach(NULL);
                 kLineUART->attach(&readKLine);*/
@@ -222,7 +223,7 @@
 #endif
         }
 //        sleepProcess();
-        if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ) {
+        if(lastOBDCmdRcvdTime> powerSaveTime /*&& lastInterfaceCmdRcvdTime> powerSaveTime */) {
             powerSaveMode();
         } else {
             sleepProcess();
@@ -303,6 +304,7 @@
 }
 void processUserCommand(char *_incomingDataInInterFace)
 {
+    isVehicleCommandProcessing=1;
     OBDTimer.reset();
     setCanMessage(_incomingDataInInterFace);
     OBDTimer.start();
@@ -342,6 +344,7 @@
     if(!_powerSaveFlag) {
         serialPorts[interFace]->puts("\r\n");
     }
+    isVehicleCommandProcessing=0;
 }
 void sleepProcess()
 {
@@ -356,7 +359,7 @@
         KlineReadCount=KlineReadCount+50;
         if(currentOBDProtocol==protkLine14230 && KlineReadCount>=3000) {
             KlineReadCount=0;
-            if(incomingDataInInterFaceCounter) {
+            if(isVehicleCommandProcessing) {
                 continue;
             }
             kLinekeepalive();
@@ -375,6 +378,7 @@
     strcpy(incomingDataInInterFace,"010D");
     _powerSaveFlag=1;
     if(currentOBDProtocol==protCan) {
+        while(isVehicleCommandProcessing){}
         processUserCommand(incomingDataInInterFace);
     } else if(currentOBDProtocol==protkLine14230) {
         while(isVehicleCommandProcessing);
@@ -413,7 +417,7 @@
     LPC_SWM->PINASSIGN8 = 0xffffffffUL;
 //    LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
     clearInterfaceCommand();
-    serialPorts[interFace]->attach(&readInterface);
+//    serialPorts[interFace]->attach(&readInterface); //  it will not work
     InterruptIn in2(P0_22);
     InterruptIn in3(P0_27);
     in2.rise(&flip);
@@ -434,7 +438,7 @@
             if(checkForLocalInterfaceCommand()) {
                 clearInterfaceCommand();
             } else {
-                serialPorts[interFace]->printf("?\r\n");
+                serialPorts[interFace]->printf("?\r\n"); // here ahave to send "TTUF" // sleep command
             }
             clearInterfaceCommand();
         } else {
@@ -578,7 +582,7 @@
         }
         kLineUART->putc(kLineCmd[kLineCommandCount]);
     }
-    wait_ms(500);
+    wait_ms(1000);
     serialPorts[debug]->printf("Got Data\r\n");
     //have to check given and received commands are same.
     //if it is same we have to read the response else just flush buffer and send the command again
@@ -602,11 +606,11 @@
     int count=0;
     while(count<len) {
         if(_powerSaveFlag) {
-            serialPorts[interFace]->printf("+TUAWAKE\r\n");
+            serialPorts[interFace]->printf("+TUAWAKE"); 
             _powerSaveFlag=0;
             break;
         }
-        if(obdDataLength>0) {
+        if(obdDataLength>0) { // have to recheck this condition
             serialPorts[interFace]->printf("%02X",incomingDataInOBD[count]);
             serialPorts[debug]->printf("%02X",incomingDataInOBD[count]);
         } else {
@@ -616,6 +620,9 @@
         count++;
         obdDataLength--;
     }
+    
+    
+    //????? suren: Have to recheck the following lines
     if(len==0) {
         if(!_powerSaveFlag) {
             serialPorts[interFace]->printf("?");
@@ -627,6 +634,7 @@
         serialPorts[debug]->printf("\r\n");
         serialPorts[debug]->printf("Done\r\n");
     }
+    wait_ms(300);
     clearInterfaceCommand();
     clearOBDCommand();
     isVehicleCommandProcessing=0;
@@ -690,6 +698,8 @@
 //    LPC_SYSCON->SYSAHBCLKCTRL1 |= (0<<7);
     serialPorts[0] = new Serial(P0_11,P0_13);
     serialPorts[1] = new Serial(P0_15,P0_14);
+    /*serialPorts[0] = new Serial(P0_16,P0_12);
+    serialPorts[1] = new Serial(USBTX,USBRX);*/
     klineWakeup = new DigitalOut(P0_28);
     canBus = new CAN(P0_22, P0_23);
     canDisableKey=new DigitalOut(P0_0,0);