From 23ea91258a34ab568c5dd2878f2eaa8a0dec6fe7 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Mon, 27 Jun 2016 19:01:23 +0800 Subject: Bugfix --- libraries/OBD2UART/OBD2UART.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'libraries/OBD2UART') diff --git a/libraries/OBD2UART/OBD2UART.cpp b/libraries/OBD2UART/OBD2UART.cpp index a079baa..c2a28fe 100644 --- a/libraries/OBD2UART/OBD2UART.cpp +++ b/libraries/OBD2UART/OBD2UART.cpp @@ -251,7 +251,7 @@ char* COBD::getResultValue(char* buf) { char* p = buf; for (;;) { - if (isdigit(*p)) { + if (isdigit(*p) || *p == '-') { return p; } p = strchr(p, '\r'); @@ -426,7 +426,7 @@ float COBD::getTemperature() char buf[32]; if (sendCommand("ATTEMP\r", buf, sizeof(buf)) > 0) { char* p = getResultValue(buf); - return (float)(atoi(p) + 12412) / 340; + if (p) return (float)(atoi(p) + 12412) / 340; } else { return -1000; @@ -435,9 +435,10 @@ float COBD::getTemperature() bool COBD::readAccel(int& x, int& y, int& z) { - char buf[64]; + char buf[32]; if (sendCommand("ATACL\r", buf, sizeof(buf)) > 0) do { char* p = getResultValue(buf); + if (!p) break; x = atoi(p++); if (!(p = strchr(p, ','))) break; y = atoi(++p); @@ -450,9 +451,10 @@ bool COBD::readAccel(int& x, int& y, int& z) bool COBD::readGyro(int& x, int& y, int& z) { - char buf[64]; + char buf[32]; if (sendCommand("ATGYRO\r", buf, sizeof(buf)) > 0) do { char* p = getResultValue(buf); + if (!p) break; x = atoi(p++); if (!(p = strchr(p, ','))) break; y = atoi(++p); -- cgit v1.2.3