upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
h803x.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 
28 #include <modbus/modbus.h>
29 
30 namespace upm {
31 
76  class H803X {
77  public:
78 
79  // MODBUS holding registers. These offsets are for the MSW only.
80  // The LSW always follows, though they are not enumerated here.
81  // These are all 2 register (32-bit total (16b HSW + 16b LSW))
82  // quantities, in IEEE 754 floating point format.
83  typedef enum {
84  // these two registers are used only for presetConsumption()
85  HOLDING_CONSUMPTION_KWH_INT_L = 0, // preset use only
86  HOLDING_CONSUMPTION_KWH_INT_H = 1, // preset use only
87 
88  // H8035/H8036
89  HOLDING_CONSUMPTION_KWH = 258, // floating point data
90 
91  HOLDING_REAL_POWER_KW = 260,
92 
93  // H8036 only
94  HOLDING_REACTIVE_POWER_KVAR = 262,
95  HOLDING_APPARENT_POWER_KVA = 264,
96  HOLDING_POWER_FACTOR = 266,
97  HOLDING_VOLTS_LINE_TO_LINE = 268,
98  HOLDING_VOLTS_LINE_TO_NEUTRAL = 270,
99  HOLDING_CURRENT = 272,
100  HOLDING_REAL_POWER_PHASE_A_KWH = 274,
101  HOLDING_REAL_POWER_PHASE_B_KWH = 276,
102  HOLDING_REAL_POWER_PHASE_C_KWH = 278,
103  HOLDING_POWER_FACTOR_PHASE_A = 280,
104  HOLDING_POWER_FACTOR_PHASE_B = 282,
105  HOLDING_POWER_FACTOR_PHASE_C = 284,
106  HOLDING_VOLTS_PHASE_AB = 286,
107  HOLDING_VOLTS_PHASE_BC = 288,
108  HOLDING_VOLTS_PHASE_AC = 290,
109  HOLDING_VOLTS_PHASE_AN = 292,
110  HOLDING_VOLTS_PHASE_BN = 294,
111  HOLDING_VOLTS_PHASE_CN = 296,
112  HOLDING_CURRENT_PHASE_A = 298,
113  HOLDING_CURRENT_PHASE_B = 300,
114  HOLDING_CURRENT_PHASE_C = 302,
115  HOLDING_AVG_REAL_POWER_KW = 304,
116  HOLDING_MIN_REAL_POWER_KW = 306,
117  HOLDING_MAX_REAL_POWER_KW = 308
118  } HOLDING_REGS_T;
119 
120  // these enums are used by presetConsumption() to scale the value
121  // properly depending on the devices' current capacity.
122  typedef enum {
123  MULT_100A = 128, // 100A devices
124  MULT_300A_400A = 32,
125  MULT_800A = 16,
126  MULT_1600A = 8,
127  MULT_2400A = 4
128  } MULTIPLIERS_T;
129 
141  H803X(std::string device, int address, int baud=9600, int bits=8,
142  char parity='N', int stopBits=2);
143 
147  ~H803X();
148 
154  void update();
155 
161  std::string getSlaveID();
162 
172  void setSlaveAddress(int addr);
173 
186  void presetConsumption(float value, MULTIPLIERS_T multiplier);
187 
195  {
196  return m_consumptionkWh;
197  };
198 
205  float getRealPower()
206  {
207  return m_realPowerkW;
208  };
209 
218  {
219  return m_reactivePowerkVAR;
220  };
221 
229  {
230  return m_apparentPowerkVA;
231  };
232 
240  {
241  return m_powerFactor;
242  };
243 
251  {
252  return m_voltsLineToLine;
253  };
254 
262  {
263  return m_voltsLineToNeutral;
264  };
265 
272  float getCurrent()
273  {
274  return m_current;
275  };
276 
284  {
285  return m_realPowerPhaseAkW;
286  };
287 
295  {
296  return m_realPowerPhaseBkW;
297  };
298 
306  {
307  return m_realPowerPhaseCkW;
308  };
309 
317  {
318  return m_powerFactorPhaseA;
319  };
320 
328  {
329  return m_powerFactorPhaseB;
330  };
331 
339  {
340  return m_powerFactorPhaseC;
341  };
342 
350  {
351  return m_voltsPhaseAB;
352  };
353 
361  {
362  return m_voltsPhaseBC;
363  };
364 
372  {
373  return m_voltsPhaseAC;
374  };
375 
383  {
384  return m_voltsPhaseAN;
385  };
386 
394  {
395  return m_voltsPhaseBN;
396  };
397 
405  {
406  return m_voltsPhaseCN;
407  };
408 
416  {
417  return m_currentPhaseA;
418  };
419 
427  {
428  return m_currentPhaseB;
429  };
430 
438  {
439  return m_currentPhaseC;
440  };
441 
449  {
450  return m_avgRealPowerkW;
451  };
452 
460  {
461  return m_minRealPowerkW;
462  };
463 
471  {
472  return m_maxRealPowerkW;
473  };
474 
481  void setDebug(bool enable);
482 
489  bool isH8036()
490  {
491  return m_isH8036;
492  };
493 
494  protected:
495  // holding registers
496  int readHoldingRegs(HOLDING_REGS_T reg, int len, uint16_t *buf);
497  void writeHoldingReg(HOLDING_REGS_T reg, int value);
498 
499  // clear out all stored data
500  void clearData();
501 
502  // MODBUS context
503  modbus_t *m_mbContext;
504 
505  // test to see if the connected device is an H8036, and set
506  // m_isH8036 appropriately
507  void testH8036();
508 
509  // Is this an H8036 (has extended registers)
510  bool m_isH8036;
511 
512  private:
513  bool m_debugging;
514 
515  // data
516 
517  // H8035 / H8036
518  float m_consumptionkWh;
519  float m_realPowerkW;
520 
521  // H8036 only
522  float m_reactivePowerkVAR;
523  float m_apparentPowerkVA;
524  float m_powerFactor;
525  float m_voltsLineToLine;
526  float m_voltsLineToNeutral;
527  float m_current; // in amps
528  float m_realPowerPhaseAkW;
529  float m_realPowerPhaseBkW;
530  float m_realPowerPhaseCkW;
531  float m_powerFactorPhaseA;
532  float m_powerFactorPhaseB;
533  float m_powerFactorPhaseC;
534  float m_voltsPhaseAB;
535  float m_voltsPhaseBC;
536  float m_voltsPhaseAC;
537  float m_voltsPhaseAN;
538  float m_voltsPhaseBN;
539  float m_voltsPhaseCN;
540  float m_currentPhaseA;
541  float m_currentPhaseB;
542  float m_currentPhaseC;
543  float m_avgRealPowerkW;
544  float m_minRealPowerkW;
545  float m_maxRealPowerkW;
546  };
547 }
void presetConsumption(float value, MULTIPLIERS_T multiplier)
Definition: h803x.cxx:346
float getApparentPower()
Definition: h803x.hpp:228
float getConsumption()
Definition: h803x.hpp:194
float getCurrentPhaseB()
Definition: h803x.hpp:426
float getPowerFactorPhaseB()
Definition: h803x.hpp:327
void setSlaveAddress(int addr)
Definition: h803x.cxx:267
void setDebug(bool enable)
Definition: h803x.cxx:286
float getRealPowerPhaseC()
Definition: h803x.hpp:305
float getAvgRealPower()
Definition: h803x.hpp:448
float getVoltsPhaseCToNeutral()
Definition: h803x.hpp:404
float getVoltsPhaseAToNeutral()
Definition: h803x.hpp:382
float getVoltsLineToNeutral()
Definition: h803x.hpp:261
float getCurrent()
Definition: h803x.hpp:272
float getPowerFactorPhaseC()
Definition: h803x.hpp:338
float getVoltsPhaseAToC()
Definition: h803x.hpp:371
~H803X()
Definition: h803x.cxx:119
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
float getVoltsPhaseBToNeutral()
Definition: h803x.hpp:393
float getMinRealPower()
Definition: h803x.hpp:459
H803X(std::string device, int address, int baud=9600, int bits=8, char parity='N', int stopBits=2)
Definition: h803x.cxx:58
float getCurrentPhaseA()
Definition: h803x.hpp:415
float getVoltsPhaseAToB()
Definition: h803x.hpp:349
float getRealPower()
Definition: h803x.hpp:205
float getVoltsPhaseBToC()
Definition: h803x.hpp:360
float getRealPowerPhaseB()
Definition: h803x.hpp:294
std::string getSlaveID()
Definition: h803x.cxx:241
void update()
Definition: h803x.cxx:181
float getPowerFactor()
Definition: h803x.hpp:239
float getRealPowerPhaseA()
Definition: h803x.hpp:283
float getPowerFactorPhaseA()
Definition: h803x.hpp:316
float getVoltsLineToLine()
Definition: h803x.hpp:250
UPM API for the Veris H803X Energy Meter.
Definition: h803x.hpp:76
bool isH8036()
Definition: h803x.hpp:489
float getCurrentPhaseC()
Definition: h803x.hpp:437
float getMaxRealPower()
Definition: h803x.hpp:470
float getReactivePower()
Definition: h803x.hpp:217