Mateusz Grzywacz / SDI12_device
Revision:
19:a29b98ae0005
Parent:
18:365e8aa4002c
Child:
20:abccaaa16955
--- a/SDI12_device.h	Tue Aug 07 16:36:17 2018 +0000
+++ b/SDI12_device.h	Wed Aug 08 16:47:55 2018 +0000
@@ -22,13 +22,12 @@
 
     Identification_struct _ident_struct;
 
-    static int detect(SDI12 &sdi12, char indices[], int stop = -1, int start = -1) {
+    const static int detect(SDI12 &sdi12, char indices[], int stop = -1, int start = -1) {
         char found = 0;
         for (size_t i = (start==-1?0:start); i < (stop==-1?ADDRRICES.size():stop+1); ++i) {
             string question = "?!";
             question[0] = ADDRRICES[i];
             sdi12.sendCommand(question);
-            std::string response;
             Timer detectTimer;
             detectTimer.start();
             while (detectTimer.read_ms() < RESPONSE_TIMEOUT * 4) {
@@ -40,7 +39,7 @@
                     // if first char is valid address char
                     if (ADDRRICES.find(buffer[0])) {
                         indices[found++] = buffer[0];
-                        debug("FOUND: %d -> %c\r\n", i, indices[found-1]);
+                        debug("FOUND (%d): %d -> %c\r\n", found, i, indices[found-1]);
                     }
                 }
             }
@@ -67,12 +66,31 @@
         _measurement = old._measurement;
         _valuesReadyCount = old._valuesReadyCount;
         _measurementReady = old._measurementReady;
+        return *this;
     }
 
     ~SDI12_device () {
-        delete [] _measurement.values;
+//        delete [] _measurement.values;
     }
+    bool present() {
+        std::string question = string("?") + string("!");
+        question[0] = _address;
 
+        _sdi12->sendCommand(question);
+
+        Timer timeout;
+        timeout.start();
+        while (sdi12.RxBufferAvailable() == 0) {
+            if (sdi12.RxInProgress()) timeout.reset();
+            if(timeout.read_ms() > RESPONSE_TIMEOUT) {
+                return false;
+            }
+        }
+        unsigned char bufferSize = sdi12.RxBufferAvailable();
+        char buffer[bufferSize];
+        sdi12.getRxBuffer(buffer); // only to flush the buffer
+        return true;
+    }
     // concurrent means that device under measurement won't signal its prempt readiness to the bus
     int measure(bool concurrent = true, char group = '0') {
 
@@ -87,7 +105,7 @@
         while (sdi12.RxBufferAvailable() == 0) {
             if (sdi12.RxInProgress()) timeout.reset();
             if(timeout.read_ms() > RESPONSE_TIMEOUT) {
-                return false;
+                return -2;
             }
         }
 
@@ -125,7 +143,7 @@
 
     bool read(char group = '0') {
 //         Measurement (M), Continuous (R), and Concurrent (C) commands and subsequent Data (D)
-        std::string question = std::string("?") + std::string("D") + std::string(1,group)+ std::string("!");
+        std::string question = std::string(_address, 1) + std::string("D") + std::string(1,group)+ std::string("!");
         question[0] = _address;
         _sdi12->sendCommand(question);