Reading Utility metering data using MBED

Dependencies:   mbed

Revision:
1:633092f351c6
Parent:
0:bacf1c9b9afc
Child:
2:b386810af678
--- a/mt382.h	Mon Sep 24 12:33:58 2012 +0000
+++ b/mt382.h	Tue Sep 25 08:56:19 2012 +0000
@@ -26,23 +26,23 @@
  *
  * Overview of used record codes & formats
  *  0-0:96.1.1(5A424556303035303936303530313132)        Serialno meter in hexadecimal ascii code
- *  1-0:1.8.1(00185.000*kWh)	                      * Total consumpion tarif 1 (night)
- *  1-0:1.8.2(00084.000*kWh)	                      * Total consumpion tarif 2 (day)
- *  1-0:2.8.1(00013.000*kWh)	                      * Total delivery tarif 1 (night)
- *  1-0:2.8.2(00019.000*kWh)	                      * Total delivery tarif 2 (day)
- *  0-0:96.14.0(0001)	                              1 Actual tarif (1)
- *  1-0:1.7.0(0000.98*kW)	                          * Current usage
- *  1-0:2.7.0(0000.00*kW)	                          * Current delivery
- *  0-0:17.0.0(999*A)	                                Maximum current by phase
- *  0-0:96.3.10(1)	                                    Position of switch
- *  0-0:96.13.1()	                                  1 Message (numeric)
- *  0-0:96.13.0()	                                  1 Message (text)
- *  0-1:24.1.0(3)	                                    Other equipment on M-Bus
- *  0-1:96.1.0(3238313031353431323030333330313132)	    Serialno of gasmeter
- *  0-1:24.3.0(120517020000)(08)(60)(1)(0-1:24.2.1)(m3)	Time of last gas measurement (120517020000 = 17 mei 2012 2uur
- *  (00124.477)	                                      * Total consumption gas
- *  0-1:24.4.0(1)	                                    Position of gas valve?
- *                                                    ^-Implemented, 1-planned for later date
+ *  1-0:1.8.1(00185.000*kWh)                          * Total consumpion tarif 1 (night)
+ *  1-0:1.8.2(00084.000*kWh)                          * Total consumpion tarif 2 (day)
+ *  1-0:2.8.1(00013.000*kWh)                          * Total delivery tarif 1 (night)
+ *  1-0:2.8.2(00019.000*kWh)                          * Total delivery tarif 2 (day)
+ *  0-0:96.14.0(0001)                                 1 Actual tarif (1)
+ *  1-0:1.7.0(0000.98*kW)                             * Current usage
+ *  1-0:2.7.0(0000.00*kW)                             * Current delivery
+ *  0-0:17.0.0(999*A)                                   Maximum current by phase
+ *  0-0:96.3.10(1)                                      Position of switch
+ *  0-0:96.13.1()                                     1 Message (numeric)
+ *  0-0:96.13.0()                                     1 Message (text)
+ *  0-1:24.1.0(3)                                       Other equipment on M-Bus
+ *  0-1:96.1.0(3238313031353431323030333330313132)      Serialno of gasmeter
+ *  0-1:24.3.0(120517020000)(08)(60)(1)(0-1:24.2.1)(m3) Time of last gas measurement (120517020000 = 17 mei 2012 2uur
+ *  (00124.477)                                       * Total consumption gas
+ *  0-1:24.4.0(1)                                       Position of gas valve?
+ *                                                    *-Implemented, 1-planned for later date
  */
  
 namespace MT {
@@ -146,10 +146,6 @@
         _delay->detach();
     }
     
-    char * getRaw(void) {
-        return _mt_data;
-    }
-    
     // get data from 'smart meter'
     int getReading () {
 
@@ -160,18 +156,23 @@
         
         while((_mt_sts & 0x02) == 0 && (_mt_sts & 0x80) == 0) {
             if(_dev->readable()) {
-                _mt_data[_mt_pnt++] = _dev->getc();
-                if (_mt_data[_mt_pnt-1] == '/') {
-                    _mt_sts = 1;
-                }
-                if (_mt_data[_mt_pnt-1] == '!') {
-                    _mt_sts = _mt_sts | 0x02;
-                    _delay->detach();
-                    _out->write(1);
+                // get available character from MT382
+                char mt_tst = _dev->getc();
+                // remove any ascii NULL character
+                if (mt_tst != 0x00) {
+                    _mt_data[_mt_pnt++] = mt_tst;
+                    if (_mt_data[_mt_pnt-1] == '/') {
+                        _mt_sts = 1;
+                    }
+                    if (_mt_data[_mt_pnt-1] == '!') {
+                        _mt_sts = _mt_sts | 0x02;
+                        _delay->detach();
+                        _out->write(1);
+                    }
                 }
             }
         }
-
+        
         int mt_tmp = 0;
         _mt_flg = 0;
         int mt_elem = sizeof(_l_mt)/sizeof(_l_mt[0]);
@@ -209,15 +210,20 @@
         return 0;
     }
 
+    // return raw data character count
+    int getCount(void) {
+        return _mt_pnt;
+    }
+
     // return selected value
     double getValue(int param = 0) { 
         return _mt_res[param];
     }
-
-    // return raw data character count
-    int getCount(void) {
-        return _mt_pnt;
-    }
+    
+    // return raw data 
+    char * getRaw(void) {
+        return _mt_data;
+    }    
 };
 
 };                                                              // namespace MT ends.