Mateusz Grzywacz / SDI12_device
Revision:
11:e0ccc1072c42
Parent:
10:efd1e313fe8c
Child:
12:270bb5b69e05
diff -r efd1e313fe8c -r e0ccc1072c42 SDI12_device.h
--- a/SDI12_device.h	Thu Aug 02 11:16:24 2018 +0000
+++ b/SDI12_device.h	Thu Aug 02 16:58:15 2018 +0000
@@ -91,7 +91,8 @@
             _measurement.readyIn = waitFor;
 //            _measurement.timestampMeasurementReadyAt
             debug("wait for %d measurement for %d seconds...", _measurement.count, waitFor);
-            _measurementEventId = sensorQueue.call_in(waitFor * 1000, callback(this, &SDI12_device::read));
+            _valuesReadyCount = 0;
+            _measurementEventId = sensorQueue.call_in(waitFor * 1000, callback(this, &SDI12_device::read), '0');
 //            debug("measurement scheduled");
 //            if (_measurement.values != NULL) delete[] _measurement.values;
 //            _measurement.values = new float[measurementsCount];
@@ -116,9 +117,9 @@
         return _measurement.readyIn;
     }
 
-    bool read() {
-        char valuesAlreadyRead = 0;
-        std::string question = string("?") + string("D0") + string("!");
+    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("!");
         question[0] = _address;
         _sdi12.sendCommand(question);
 
@@ -149,9 +150,12 @@
                 next_end_index = response.find_first_of("-+", next_start_index + 1); // std::substr is prepared to take std::npos
                 float value = std::atof(response.substr(next_start_index, next_end_index).c_str());
 //                debug("parsed: %f\r\n", value);
-                _measurement.values[valuesAlreadyRead++] = value;
+                _measurement.values[_valuesReadyCount++] = value;
                 next_start_index = response.find_first_of("-+", next_end_index);
             }
+            // if current parsing doesn't return all the expexted measurements, then press harder and poll further Dx commands. RECURSION HERE *.*
+            if (_valuesReadyCount < _measurement.count)
+                read(group + 1);
         }
         return true;
     }
@@ -179,6 +183,7 @@
     char _address;
     int _measurementEventId;
     Measurement_struct _measurement;
+    size_t _valuesReadyCount;
 
     EventQueue sensorQueue;