From 2a4d5373475f57247c547c5224477a85f1219a23 Mon Sep 17 00:00:00 2001
From: Stanley Huang <stanleyhuangyc@gmail.com>
Date: Sun, 9 Mar 2014 12:26:36 +0800
Subject: Improve timeout processing

---
 libraries/OBD/OBD.cpp | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp
index c7c6024..a3186ea 100644
--- a/libraries/OBD/OBD.cpp
+++ b/libraries/OBD/OBD.cpp
@@ -169,22 +169,22 @@ int COBD::normalizeData(byte pid, char* data)
 
 char* COBD::getResponse(byte& pid, char* buffer)
 {
-	receive(buffer);
-	char *p = buffer;
-	while ((p = strstr(p, "41 "))) {
-		p += 3;
-		byte curpid = hex2uint8(p);
-		if (pid == 0) pid = curpid;
-		if (curpid == pid) {
-			errors = 0;
-			p += 2;
-			if (*p == ' ')
-				return p + 1;
-		} else {
-			receive(buffer);
-			p = buffer;
-		}
-	}
+    int timeout = OBD_TIMEOUT_SHORT;
+    while (receive(buffer, timeout) > 0) {
+        char *p = buffer;
+        while ((p = strstr(p, "41 "))) {
+            p += 3;
+            byte curpid = hex2uint8(p);
+            if (pid == 0) pid = curpid;
+            if (curpid == pid) {
+                errors = 0;
+                p += 2;
+                if (*p == ' ')
+                    return p + 1;
+            }
+        }
+        timeout = 0;
+    }
 	return 0;
 }
 
-- 
cgit v1.2.3