No Description

h803x.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/python
  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. import time, sys, signal, atexit
  24. import pyupm_h803x as sensorObj
  25. ## Exit handlers ##
  26. # This function stops python from printing a stacktrace when you hit control-C
  27. def SIGINTHandler(signum, frame):
  28. raise SystemExit
  29. # This function lets you run code on exit
  30. def exitHandler():
  31. print "Exiting..."
  32. sys.exit(0)
  33. # Register exit handlers
  34. atexit.register(exitHandler)
  35. signal.signal(signal.SIGINT, SIGINTHandler)
  36. defaultDev = "/dev/ttyUSB0"
  37. # if an argument was specified, use it as the device instead
  38. if (len(sys.argv) > 1):
  39. defaultDev = sys.argv[1]
  40. print "Using device", defaultDev
  41. print "Initializing..."
  42. # Instantiate an H803X instance, using MODBUS slave address 1, and
  43. # default comm parameters (9600, 8, N, 2)
  44. sensor = sensorObj.H803X(defaultDev, 1)
  45. # output the serial number and firmware revision
  46. print "Slave ID:", sensor.getSlaveID()
  47. print
  48. # update and print available values every second
  49. while (1):
  50. # update our values from the sensor
  51. sensor.update()
  52. # H8035 / H8036
  53. print "Consumption (kWh):", sensor.getConsumption()
  54. print "Real Power (kW):", sensor.getRealPower()
  55. if (sensor.isH8036()):
  56. # The H8036 has much more data available...
  57. print "Reactive Power (kVAR):", sensor.getReactivePower()
  58. print "Apparent Power (kVA):", sensor.getApparentPower()
  59. print "Power Factor:", sensor.getPowerFactor()
  60. print "Volts Line to Line:", sensor.getVoltsLineToLine()
  61. print "Volts Line to Neutral:", sensor.getVoltsLineToNeutral()
  62. print "Current:", sensor.getCurrent()
  63. print "Real Power Phase A (kW):", sensor.getRealPowerPhaseA()
  64. print "Real Power Phase B (kW):", sensor.getRealPowerPhaseB()
  65. print "Real Power Phase C (kW):", sensor.getRealPowerPhaseC()
  66. print "Power Factor Phase A:", sensor.getPowerFactorPhaseA()
  67. print "Power Factor Phase B:", sensor.getPowerFactorPhaseB()
  68. print "Power Factor Phase C:", sensor.getPowerFactorPhaseC()
  69. print "Volts Phase A to B:", sensor.getVoltsPhaseAToB()
  70. print "Volts Phase B to C:", sensor.getVoltsPhaseBToC()
  71. print "Volts Phase A to C:", sensor.getVoltsPhaseAToC()
  72. print "Volts Phase A to Neutral: ",
  73. print sensor.getVoltsPhaseAToNeutral()
  74. print "Volts Phase B to Neutral: ",
  75. print sensor.getVoltsPhaseBToNeutral()
  76. print "Volts Phase C to Neutral: ",
  77. print sensor.getVoltsPhaseCToNeutral()
  78. print "Current Phase A:", sensor.getCurrentPhaseA()
  79. print "Current Phase B:", sensor.getCurrentPhaseB()
  80. print "Current Phase C:", sensor.getCurrentPhaseC()
  81. print "Avg Real Power (kW):", sensor.getAvgRealPower()
  82. print "Min Real Power (kW):", sensor.getMinRealPower()
  83. print "Max Real Power (kW):", sensor.getMaxRealPower()
  84. print
  85. time.sleep(2)