energymeter.py 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. #!/usr/bin/env python3
  2. """
  3. EnergyMeter: A Python module for interfacing with several energy meters.
  4. """
  5. __author__ = 'Stan Janssen'
  6. __email__ = 'stanjanssen@finetuned.nl'
  7. __url__ = 'https://finetuned.nl/'
  8. __license__ = 'Apache License, Version 2.0'
  9. __version__ = '0.73'
  10. __status__ = 'Beta'
  11. import random
  12. import socket
  13. import struct
  14. import time
  15. import minimalmodbus
  16. class ABBMeter:
  17. """Meter class that uses a minimalmodbus Instrument to query an ABB meter."""
  18. def __init__(self, port, baudrate=38400, slaveaddress=1, type=None):
  19. """ Initialize the ABBMeter object.
  20. Arguments:
  21. * port: a serial port (string)
  22. * baudrate: the baudrate to use (integer)
  23. * slaveaddress: the address of the modbus device (integer)
  24. * Specification of the type. Used to limit the registers to a specific set.
  25. Returns:
  26. * An ABBMeter object
  27. """
  28. minimalmodbus.BAUDRATE = baudrate
  29. minimalmodbus.TIMEOUT = 0.5
  30. self.instrument = minimalmodbus.Instrument(port, slaveaddress)
  31. if type in ABBMeter.REGSETS:
  32. self.registers = [register for register in ABBMeter.REGS if register['name'] in ABBMeter.REGSETS[type]]
  33. else:
  34. self.registers = ABBMeter.REGS
  35. def read(self, regnames=None):
  36. """ Read one, many or all registers from the device
  37. Args:
  38. * registers (str or list). If None, read all. If string, read
  39. single register. If list, read all registers from list.
  40. Returns:
  41. * If single register, it returns a single value. If all or list,
  42. return a dict with the keys and values.
  43. Raises:
  44. * KeyError, TypeError, IOError
  45. """
  46. if regnames is None:
  47. return self._batch_read(self.registers)
  48. if type(regnames) is list:
  49. registers = [register for register in self.registers if register['name'] in regnames]
  50. if len(registers) < len(regnames):
  51. regs_not_available = [regname for regname in regnames if regname not in \
  52. [register['name'] for register in self.registers]]
  53. print("Warning: the following registers are not available on this device: " +
  54. ", ".join(regs_not_available))
  55. print("The available registers are: %s" +
  56. ", ".join(register['name'] for register in self.registers))
  57. if len(registers) == 0:
  58. return {}
  59. registers.sort(key=lambda reg: reg['start'])
  60. return self._batch_read(registers)
  61. elif type(regnames) is str:
  62. registers = [register for register in self.registers if register['name'] == regnames]
  63. if len(registers) == 0:
  64. return "Register not found on device."
  65. return self._read_single(registers[0])
  66. else:
  67. raise TypeError
  68. def _read_single(self, register):
  69. """
  70. Read a single register and return the value.
  71. """
  72. if register['length'] is 1:
  73. return self.instrument.read_register(registeraddress=register['start'],
  74. numberOfDecimals=register['decimals'],
  75. signed=register['signed'])
  76. if register['length'] is 2:
  77. value = self.instrument.read_long(registeraddress=register['start'],
  78. signed=register['signed'])
  79. return value / 10 ** register['decimals']
  80. if register['length'] is 4:
  81. value = self.instrument.read_registers(registeraddress=register['start'],
  82. numberOfRegisters=register['length'])
  83. return self._convert_value(values=value,
  84. signed=register['signed'],
  85. numberOfDecimals=register['decimals'])
  86. def _read_multiple(self, registers):
  87. """
  88. Read multiple registers from the slave device and return their values as a dict.
  89. Arguments:
  90. * A list of registers (complete structs)
  91. Returns:
  92. * A dict containing all keys and values
  93. """
  94. first_reg = min([register['start'] for register in registers])
  95. num_regs = max([register['start'] + register['length'] for register in registers]) - first_reg
  96. values = self.instrument.read_registers(registeraddress=first_reg,
  97. numberOfRegisters=num_regs)
  98. return self._interpret_result(values, registers)
  99. def _batch_read(self, registers):
  100. """
  101. Read multiple registers in batches, limiting each batch to at most 128 registers.
  102. Arguments:
  103. * A list of registers (complete structs)
  104. Returns:
  105. * A dict containing all keys and values
  106. """
  107. # Count up to at most 128 registers:
  108. start_reg = registers[0]['start']
  109. batch = []
  110. results = {}
  111. for register in registers:
  112. if register['start'] + register['length'] - start_reg < 128:
  113. batch.append(register)
  114. else:
  115. results.update(self._read_multiple(batch))
  116. batch = []
  117. batch.append(register)
  118. start_reg = register['start']
  119. results.update(self._read_multiple(batch))
  120. return results
  121. def _interpret_result(self, data, registers):
  122. """
  123. Pull the returned string apart and package the data back to its
  124. intended form.
  125. Arguments:
  126. * data: list of register values returned from the device
  127. * registers: the original requested set of registers
  128. Returns:
  129. * A dict containing the register names and resulting values
  130. """
  131. first_reg = min([register['start'] for register in registers])
  132. results = {}
  133. for register in registers:
  134. regname = register['name']
  135. start = register['start'] - first_reg
  136. end = start + register['length']
  137. values = data[start:end]
  138. results[regname] = self._convert_value(values=values,
  139. signed=register['signed'],
  140. numberOfDecimals=register['decimals'])
  141. return results
  142. def _convert_value(self, values, signed=False, numberOfDecimals=0):
  143. """
  144. Convert a list of returned integers to the intended value.
  145. Arguments:
  146. * bytestring: a list of integers that together represent the value
  147. * signed: whether the value is a signed value
  148. * decimals: number of decimals the return value should contain
  149. """
  150. numberOfRegisters = len(values)
  151. formatcode_i = '>'
  152. formatcode_o = '>'
  153. if numberOfRegisters == 1:
  154. formatcode_i += "H"
  155. if signed:
  156. formatcode_o += "h"
  157. else:
  158. formatcode_o += "H"
  159. if numberOfRegisters == 2:
  160. formatcode_i += 'HH'
  161. if signed:
  162. formatcode_o += "l"
  163. else:
  164. formatcode_o += "L"
  165. if numberOfRegisters == 4:
  166. formatcode_i += "HHHH"
  167. if signed:
  168. formatcode_o += "q"
  169. else:
  170. formatcode_o += "Q"
  171. bytestring = struct.pack(formatcode_i, *values)
  172. value = struct.unpack(formatcode_o, bytestring)[0]
  173. if value in ABBMeter.NULLS:
  174. return None
  175. else:
  176. return float(value) / 10 ** numberOfDecimals
  177. # Register map of the ABB A and B series energy meters.
  178. REGS = [{'name': 'active_import', 'start': 20480, 'length': 4, 'signed': True, 'decimals': 2},
  179. {'name': 'active_export', 'start': 20484, 'length': 4, 'signed': True, 'decimals': 2},
  180. {'name': 'active_net', 'start': 20488, 'length': 4, 'signed': True, 'decimals': 2},
  181. {'name': 'reactive_import', 'start': 20492, 'length': 4, 'signed': True, 'decimals': 2},
  182. {'name': 'reactive_export', 'start': 20496, 'length': 4, 'signed': True, 'decimals': 2},
  183. {'name': 'reactive_net', 'start': 20500, 'length': 4, 'signed': True, 'decimals': 2},
  184. {'name': 'apparent_import', 'start': 20504, 'length': 4, 'signed': True, 'decimals': 2},
  185. {'name': 'apparent_export', 'start': 20508, 'length': 4, 'signed': True, 'decimals': 2},
  186. {'name': 'apparent_net', 'start': 20512, 'length': 4, 'signed': True, 'decimals': 2},
  187. {'name': 'active_import_co2', 'start': 20516, 'length': 4, 'signed': True, 'decimals': 2},
  188. {'name': 'active_import_currency', 'start': 20532, 'length': 4, 'signed': True, 'decimals': 2},
  189. {'name': 'active_import_tariff_1', 'start': 20848, 'length': 4, 'signed': False, 'decimals': 2},
  190. {'name': 'active_import_tariff_2', 'start': 20852, 'length': 4, 'signed': False, 'decimals': 2},
  191. {'name': 'active_import_tariff_3', 'start': 20856, 'length': 4, 'signed': False, 'decimals': 2},
  192. {'name': 'active_import_tariff_4', 'start': 20860, 'length': 4, 'signed': False, 'decimals': 2},
  193. {'name': 'active_export_tariff_1', 'start': 20880, 'length': 4, 'signed': False, 'decimals': 2},
  194. {'name': 'active_export_tariff_2', 'start': 20884, 'length': 4, 'signed': False, 'decimals': 2},
  195. {'name': 'active_export_tariff_3', 'start': 20888, 'length': 4, 'signed': False, 'decimals': 2},
  196. {'name': 'active_export_tariff_4', 'start': 20892, 'length': 4, 'signed': False, 'decimals': 2},
  197. {'name': 'reactive_import_tariff_1', 'start': 20912, 'length': 4, 'signed': False, 'decimals': 2},
  198. {'name': 'reactive_import_tariff_2', 'start': 20916, 'length': 4, 'signed': False, 'decimals': 2},
  199. {'name': 'reactive_import_tariff_3', 'start': 20920, 'length': 4, 'signed': False, 'decimals': 2},
  200. {'name': 'reactive_import_tariff_4', 'start': 20924, 'length': 4, 'signed': False, 'decimals': 2},
  201. {'name': 'reactive_export_tariff_1', 'start': 20944, 'length': 4, 'signed': False, 'decimals': 2},
  202. {'name': 'reactive_export_tariff_2', 'start': 20948, 'length': 4, 'signed': False, 'decimals': 2},
  203. {'name': 'reactive_export_tariff_3', 'start': 20952, 'length': 4, 'signed': False, 'decimals': 2},
  204. {'name': 'reactive_export_tariff_4', 'start': 20956, 'length': 4, 'signed': False, 'decimals': 2},
  205. {'name': 'active_import_l1', 'start': 21600, 'length': 4, 'signed': False, 'decimals': 2},
  206. {'name': 'active_import_l2', 'start': 21604, 'length': 4, 'signed': False, 'decimals': 2},
  207. {'name': 'active_import_l3', 'start': 21608, 'length': 4, 'signed': False, 'decimals': 2},
  208. {'name': 'active_export_l1', 'start': 21612, 'length': 4, 'signed': False, 'decimals': 2},
  209. {'name': 'active_export_l2', 'start': 21616, 'length': 4, 'signed': False, 'decimals': 2},
  210. {'name': 'active_export_l3', 'start': 21620, 'length': 4, 'signed': False, 'decimals': 2},
  211. {'name': 'active_net_l1', 'start': 21624, 'length': 4, 'signed': True, 'decimals': 2},
  212. {'name': 'active_net_l2', 'start': 21628, 'length': 4, 'signed': True, 'decimals': 2},
  213. {'name': 'active_net_l3', 'start': 21632, 'length': 4, 'signed': True, 'decimals': 2},
  214. {'name': 'reactive_import_l1', 'start': 21636, 'length': 4, 'signed': False, 'decimals': 2},
  215. {'name': 'reactive_import_l2', 'start': 21640, 'length': 4, 'signed': False, 'decimals': 2},
  216. {'name': 'reactive_import_l3', 'start': 21644, 'length': 4, 'signed': False, 'decimals': 2},
  217. {'name': 'reactive_export_l1', 'start': 21648, 'length': 4, 'signed': False, 'decimals': 2},
  218. {'name': 'reactive_export_l2', 'start': 21652, 'length': 4, 'signed': False, 'decimals': 2},
  219. {'name': 'reactive_export_l3', 'start': 21656, 'length': 4, 'signed': False, 'decimals': 2},
  220. {'name': 'reactive_net_l1', 'start': 21660, 'length': 4, 'signed': True, 'decimals': 2},
  221. {'name': 'reactive_net_l2', 'start': 21664, 'length': 4, 'signed': True, 'decimals': 2},
  222. {'name': 'reactive_net_l3', 'start': 21668, 'length': 4, 'signed': True, 'decimals': 2},
  223. {'name': 'apparent_import_l1', 'start': 21672, 'length': 4, 'signed': False, 'decimals': 2},
  224. {'name': 'apparent_import_l2', 'start': 21676, 'length': 4, 'signed': False, 'decimals': 2},
  225. {'name': 'apparent_import_l3', 'start': 21680, 'length': 4, 'signed': False, 'decimals': 2},
  226. {'name': 'apparent_export_l1', 'start': 21684, 'length': 4, 'signed': False, 'decimals': 2},
  227. {'name': 'apparent_export_l2', 'start': 21688, 'length': 4, 'signed': False, 'decimals': 2},
  228. {'name': 'apparent_export_l3', 'start': 21692, 'length': 4, 'signed': False, 'decimals': 2},
  229. {'name': 'apparent_net_l1', 'start': 21696, 'length': 4, 'signed': True, 'decimals': 2},
  230. {'name': 'apparent_net_l2', 'start': 21700, 'length': 4, 'signed': True, 'decimals': 2},
  231. {'name': 'apparent_net_l3', 'start': 21704, 'length': 4, 'signed': True, 'decimals': 2},
  232. {'name': 'resettable_active_import', 'start': 21804, 'length': 4, 'signed': False, 'decimals': 2},
  233. {'name': 'resettable_active_export', 'start': 21808, 'length': 4, 'signed': False, 'decimals': 2},
  234. {'name': 'resettable_reactive_import', 'start': 21812, 'length': 4, 'signed': False, 'decimals': 2},
  235. {'name': 'resettable_reactive_export', 'start': 21816, 'length': 4, 'signed': False, 'decimals': 2},
  236. {'name': 'voltage_l1_n', 'start': 23296, 'length': 2, 'signed': False, 'decimals': 1},
  237. {'name': 'voltage_l2_n', 'start': 23298, 'length': 2, 'signed': False, 'decimals': 1},
  238. {'name': 'voltage_l3_n', 'start': 23300, 'length': 2, 'signed': False, 'decimals': 1},
  239. {'name': 'voltage_l1_l2', 'start': 23302, 'length': 2, 'signed': False, 'decimals': 1},
  240. {'name': 'voltage_l3_l2', 'start': 23304, 'length': 2, 'signed': False, 'decimals': 1},
  241. {'name': 'voltage_l1_l3', 'start': 23306, 'length': 2, 'signed': False, 'decimals': 1},
  242. {'name': 'current_l1', 'start': 23308, 'length': 2, 'signed': False, 'decimals': 2},
  243. {'name': 'current_l2', 'start': 23310, 'length': 2, 'signed': False, 'decimals': 2},
  244. {'name': 'current_l3', 'start': 23312, 'length': 2, 'signed': False, 'decimals': 2},
  245. {'name': 'current_n', 'start': 23314, 'length': 2, 'signed': False, 'decimals': 2},
  246. {'name': 'active_power_total', 'start': 23316, 'length': 2, 'signed': True, 'decimals': 2},
  247. {'name': 'active_power_l1', 'start': 23318, 'length': 2, 'signed': True, 'decimals': 2},
  248. {'name': 'active_power_l2', 'start': 23320, 'length': 2, 'signed': True, 'decimals': 2},
  249. {'name': 'active_power_l3', 'start': 23322, 'length': 2, 'signed': True, 'decimals': 2},
  250. {'name': 'reactive_power_total', 'start': 23324, 'length': 2, 'signed': True, 'decimals': 2},
  251. {'name': 'reactive_power_l1', 'start': 23326, 'length': 2, 'signed': True, 'decimals': 2},
  252. {'name': 'reactive_power_l2', 'start': 23328, 'length': 2, 'signed': True, 'decimals': 2},
  253. {'name': 'reactive_power_l3', 'start': 23330, 'length': 2, 'signed': True, 'decimals': 2},
  254. {'name': 'apparent_power_total', 'start': 23332, 'length': 2, 'signed': True, 'decimals': 2},
  255. {'name': 'apparent_power_l1', 'start': 23334, 'length': 2, 'signed': True, 'decimals': 2},
  256. {'name': 'apparent_power_l2', 'start': 23336, 'length': 2, 'signed': True, 'decimals': 2},
  257. {'name': 'apparent_power_l3', 'start': 23338, 'length': 2, 'signed': True, 'decimals': 2},
  258. {'name': 'frequency', 'start': 23340, 'length': 1, 'signed': False, 'decimals': 2},
  259. {'name': 'phase_angle_power_total', 'start': 23341, 'length': 1, 'signed': True, 'decimals': 1},
  260. {'name': 'phase_angle_power_l1', 'start': 23342, 'length': 1, 'signed': True, 'decimals': 1},
  261. {'name': 'phase_angle_power_l2', 'start': 23343, 'length': 1, 'signed': True, 'decimals': 1},
  262. {'name': 'phase_angle_power_l3', 'start': 23344, 'length': 1, 'signed': True, 'decimals': 1},
  263. {'name': 'phase_angle_voltage_l1', 'start': 23345, 'length': 1, 'signed': True, 'decimals': 1},
  264. {'name': 'phase_angle_voltage_l2', 'start': 23346, 'length': 1, 'signed': True, 'decimals': 1},
  265. {'name': 'phase_angle_voltage_l3', 'start': 23347, 'length': 1, 'signed': True, 'decimals': 1},
  266. {'name': 'phase_angle_current_l1', 'start': 23351, 'length': 1, 'signed': True, 'decimals': 1},
  267. {'name': 'phase_angle_current_l2', 'start': 23352, 'length': 1, 'signed': True, 'decimals': 1},
  268. {'name': 'phase_angle_current_l3', 'start': 23353, 'length': 1, 'signed': True, 'decimals': 1},
  269. {'name': 'power_factor_total', 'start': 23354, 'length': 1, 'signed': True, 'decimals': 3},
  270. {'name': 'power_factor_l1', 'start': 23355, 'length': 1, 'signed': True, 'decimals': 3},
  271. {'name': 'power_factor_l2', 'start': 23356, 'length': 1, 'signed': True, 'decimals': 3},
  272. {'name': 'power_factor_l3', 'start': 23357, 'length': 1, 'signed': True, 'decimals': 3},
  273. {'name': 'current_quadrant_total', 'start': 23358, 'length': 1, 'signed': False, 'decimals': 0},
  274. {'name': 'current_quadrant_l1', 'start': 23359, 'length': 1, 'signed': False, 'decimals': 0},
  275. {'name': 'current_quadrant_l2', 'start': 23360, 'length': 1, 'signed': False, 'decimals': 0},
  276. {'name': 'current_quadrant_l3', 'start': 23361, 'length': 1, 'signed': False, 'decimals': 0},
  277. {'name': 'voltage_harmonics_l1_n_thd', 'start': 23808, 'length': 2, 'signed': False, 'decimals': 1},
  278. {'name': 'voltage_harmonics_l1_n_2nd', 'start': 23810, 'length': 2, 'signed': False, 'decimals': 1},
  279. {'name': 'voltage_harmonics_l1_n_3rd', 'start': 23812, 'length': 2, 'signed': False, 'decimals': 1},
  280. {'name': 'voltage_harmonics_l1_n_4th', 'start': 23814, 'length': 2, 'signed': False, 'decimals': 1},
  281. {'name': 'voltage_harmonics_l1_n_5th', 'start': 23816, 'length': 2, 'signed': False, 'decimals': 1},
  282. {'name': 'voltage_harmonics_l1_n_6th', 'start': 23818, 'length': 2, 'signed': False, 'decimals': 1},
  283. {'name': 'voltage_harmonics_l1_n_7th', 'start': 23820, 'length': 2, 'signed': False, 'decimals': 1},
  284. {'name': 'voltage_harmonics_l1_n_8th', 'start': 23822, 'length': 2, 'signed': False, 'decimals': 1},
  285. {'name': 'voltage_harmonics_l1_n_9th', 'start': 23824, 'length': 2, 'signed': False, 'decimals': 1},
  286. {'name': 'voltage_harmonics_l1_n_10th', 'start': 23826, 'length': 2, 'signed': False, 'decimals': 1},
  287. {'name': 'voltage_harmonics_l1_n_11th', 'start': 23828, 'length': 2, 'signed': False, 'decimals': 1},
  288. {'name': 'voltage_harmonics_l1_n_12th', 'start': 23830, 'length': 2, 'signed': False, 'decimals': 1},
  289. {'name': 'voltage_harmonics_l1_n_13th', 'start': 23832, 'length': 2, 'signed': False, 'decimals': 1},
  290. {'name': 'voltage_harmonics_l1_n_14th', 'start': 23834, 'length': 2, 'signed': False, 'decimals': 1},
  291. {'name': 'voltage_harmonics_l1_n_15th', 'start': 23836, 'length': 2, 'signed': False, 'decimals': 1},
  292. {'name': 'voltage_harmonics_l1_n_16th', 'start': 23838, 'length': 2, 'signed': False, 'decimals': 1},
  293. {'name': 'voltage_harmonics_l2_n_thd', 'start': 23936, 'length': 2, 'signed': False, 'decimals': 1},
  294. {'name': 'voltage_harmonics_l2_n_2nd', 'start': 23938, 'length': 2, 'signed': False, 'decimals': 1},
  295. {'name': 'voltage_harmonics_l2_n_3rd', 'start': 23940, 'length': 2, 'signed': False, 'decimals': 1},
  296. {'name': 'voltage_harmonics_l2_n_4th', 'start': 23942, 'length': 2, 'signed': False, 'decimals': 1},
  297. {'name': 'voltage_harmonics_l2_n_5th', 'start': 23944, 'length': 2, 'signed': False, 'decimals': 1},
  298. {'name': 'voltage_harmonics_l2_n_6th', 'start': 23946, 'length': 2, 'signed': False, 'decimals': 1},
  299. {'name': 'voltage_harmonics_l2_n_7th', 'start': 23948, 'length': 2, 'signed': False, 'decimals': 1},
  300. {'name': 'voltage_harmonics_l2_n_8th', 'start': 23950, 'length': 2, 'signed': False, 'decimals': 1},
  301. {'name': 'voltage_harmonics_l2_n_9th', 'start': 23952, 'length': 2, 'signed': False, 'decimals': 1},
  302. {'name': 'voltage_harmonics_l2_n_10th', 'start': 23954, 'length': 2, 'signed': False, 'decimals': 1},
  303. {'name': 'voltage_harmonics_l2_n_11th', 'start': 23956, 'length': 2, 'signed': False, 'decimals': 1},
  304. {'name': 'voltage_harmonics_l2_n_12th', 'start': 23958, 'length': 2, 'signed': False, 'decimals': 1},
  305. {'name': 'voltage_harmonics_l2_n_13th', 'start': 23960, 'length': 2, 'signed': False, 'decimals': 1},
  306. {'name': 'voltage_harmonics_l2_n_14th', 'start': 23962, 'length': 2, 'signed': False, 'decimals': 1},
  307. {'name': 'voltage_harmonics_l2_n_15th', 'start': 23964, 'length': 2, 'signed': False, 'decimals': 1},
  308. {'name': 'voltage_harmonics_l2_n_16th', 'start': 23966, 'length': 2, 'signed': False, 'decimals': 1},
  309. {'name': 'voltage_harmonics_l3_n_thd', 'start': 24064, 'length': 2, 'signed': False, 'decimals': 1},
  310. {'name': 'voltage_harmonics_l3_n_2nd', 'start': 24066, 'length': 2, 'signed': False, 'decimals': 1},
  311. {'name': 'voltage_harmonics_l3_n_3rd', 'start': 24068, 'length': 2, 'signed': False, 'decimals': 1},
  312. {'name': 'voltage_harmonics_l3_n_4th', 'start': 24070, 'length': 2, 'signed': False, 'decimals': 1},
  313. {'name': 'voltage_harmonics_l3_n_5th', 'start': 24072, 'length': 2, 'signed': False, 'decimals': 1},
  314. {'name': 'voltage_harmonics_l3_n_6th', 'start': 24074, 'length': 2, 'signed': False, 'decimals': 1},
  315. {'name': 'voltage_harmonics_l3_n_7th', 'start': 24076, 'length': 2, 'signed': False, 'decimals': 1},
  316. {'name': 'voltage_harmonics_l3_n_8th', 'start': 24078, 'length': 2, 'signed': False, 'decimals': 1},
  317. {'name': 'voltage_harmonics_l3_n_9th', 'start': 24080, 'length': 2, 'signed': False, 'decimals': 1},
  318. {'name': 'voltage_harmonics_l3_n_10th', 'start': 24082, 'length': 2, 'signed': False, 'decimals': 1},
  319. {'name': 'voltage_harmonics_l3_n_11th', 'start': 24084, 'length': 2, 'signed': False, 'decimals': 1},
  320. {'name': 'voltage_harmonics_l3_n_12th', 'start': 24086, 'length': 2, 'signed': False, 'decimals': 1},
  321. {'name': 'voltage_harmonics_l3_n_13th', 'start': 24088, 'length': 2, 'signed': False, 'decimals': 1},
  322. {'name': 'voltage_harmonics_l3_n_14th', 'start': 24090, 'length': 2, 'signed': False, 'decimals': 1},
  323. {'name': 'voltage_harmonics_l3_n_15th', 'start': 24092, 'length': 2, 'signed': False, 'decimals': 1},
  324. {'name': 'voltage_harmonics_l3_n_16th', 'start': 24094, 'length': 2, 'signed': False, 'decimals': 1},
  325. {'name': 'voltage_harmonics_l1_l2_thd', 'start': 24192, 'length': 2, 'signed': False, 'decimals': 1},
  326. {'name': 'voltage_harmonics_l1_l2_2nd', 'start': 24194, 'length': 2, 'signed': False, 'decimals': 1},
  327. {'name': 'voltage_harmonics_l1_l2_3rd', 'start': 24196, 'length': 2, 'signed': False, 'decimals': 1},
  328. {'name': 'voltage_harmonics_l1_l2_4th', 'start': 24198, 'length': 2, 'signed': False, 'decimals': 1},
  329. {'name': 'voltage_harmonics_l1_l2_5th', 'start': 24200, 'length': 2, 'signed': False, 'decimals': 1},
  330. {'name': 'voltage_harmonics_l1_l2_6th', 'start': 24202, 'length': 2, 'signed': False, 'decimals': 1},
  331. {'name': 'voltage_harmonics_l1_l2_7th', 'start': 24204, 'length': 2, 'signed': False, 'decimals': 1},
  332. {'name': 'voltage_harmonics_l1_l2_8th', 'start': 24206, 'length': 2, 'signed': False, 'decimals': 1},
  333. {'name': 'voltage_harmonics_l1_l2_9th', 'start': 24208, 'length': 2, 'signed': False, 'decimals': 1},
  334. {'name': 'voltage_harmonics_l1_l2_10th', 'start': 24210, 'length': 2, 'signed': False, 'decimals': 1},
  335. {'name': 'voltage_harmonics_l1_l2_11th', 'start': 24212, 'length': 2, 'signed': False, 'decimals': 1},
  336. {'name': 'voltage_harmonics_l1_l2_12th', 'start': 24214, 'length': 2, 'signed': False, 'decimals': 1},
  337. {'name': 'voltage_harmonics_l1_l2_13th', 'start': 24216, 'length': 2, 'signed': False, 'decimals': 1},
  338. {'name': 'voltage_harmonics_l1_l2_14th', 'start': 24218, 'length': 2, 'signed': False, 'decimals': 1},
  339. {'name': 'voltage_harmonics_l1_l2_15th', 'start': 24220, 'length': 2, 'signed': False, 'decimals': 1},
  340. {'name': 'voltage_harmonics_l1_l2_16th', 'start': 24222, 'length': 2, 'signed': False, 'decimals': 1},
  341. {'name': 'voltage_harmonics_l3_l2_thd', 'start': 24320, 'length': 2, 'signed': False, 'decimals': 1},
  342. {'name': 'voltage_harmonics_l3_l2_2nd', 'start': 24322, 'length': 2, 'signed': False, 'decimals': 1},
  343. {'name': 'voltage_harmonics_l3_l2_3rd', 'start': 24324, 'length': 2, 'signed': False, 'decimals': 1},
  344. {'name': 'voltage_harmonics_l3_l2_4th', 'start': 24326, 'length': 2, 'signed': False, 'decimals': 1},
  345. {'name': 'voltage_harmonics_l3_l2_5th', 'start': 24328, 'length': 2, 'signed': False, 'decimals': 1},
  346. {'name': 'voltage_harmonics_l3_l2_6th', 'start': 24330, 'length': 2, 'signed': False, 'decimals': 1},
  347. {'name': 'voltage_harmonics_l3_l2_7th', 'start': 24332, 'length': 2, 'signed': False, 'decimals': 1},
  348. {'name': 'voltage_harmonics_l3_l2_8th', 'start': 24334, 'length': 2, 'signed': False, 'decimals': 1},
  349. {'name': 'voltage_harmonics_l3_l2_9th', 'start': 24336, 'length': 2, 'signed': False, 'decimals': 1},
  350. {'name': 'voltage_harmonics_l3_l2_10th', 'start': 24338, 'length': 2, 'signed': False, 'decimals': 1},
  351. {'name': 'voltage_harmonics_l3_l2_11th', 'start': 24340, 'length': 2, 'signed': False, 'decimals': 1},
  352. {'name': 'voltage_harmonics_l3_l2_12th', 'start': 24342, 'length': 2, 'signed': False, 'decimals': 1},
  353. {'name': 'voltage_harmonics_l3_l2_13th', 'start': 24344, 'length': 2, 'signed': False, 'decimals': 1},
  354. {'name': 'voltage_harmonics_l3_l2_14th', 'start': 24346, 'length': 2, 'signed': False, 'decimals': 1},
  355. {'name': 'voltage_harmonics_l3_l2_15th', 'start': 24348, 'length': 2, 'signed': False, 'decimals': 1},
  356. {'name': 'voltage_harmonics_l3_l2_16th', 'start': 24350, 'length': 2, 'signed': False, 'decimals': 1},
  357. {'name': 'voltage_harmonics_l1_l3_thd', 'start': 24448, 'length': 2, 'signed': False, 'decimals': 1},
  358. {'name': 'voltage_harmonics_l1_l3_2nd', 'start': 24450, 'length': 2, 'signed': False, 'decimals': 1},
  359. {'name': 'voltage_harmonics_l1_l3_3rd', 'start': 24452, 'length': 2, 'signed': False, 'decimals': 1},
  360. {'name': 'voltage_harmonics_l1_l3_4th', 'start': 24454, 'length': 2, 'signed': False, 'decimals': 1},
  361. {'name': 'voltage_harmonics_l1_l3_5th', 'start': 24456, 'length': 2, 'signed': False, 'decimals': 1},
  362. {'name': 'voltage_harmonics_l1_l3_6th', 'start': 24458, 'length': 2, 'signed': False, 'decimals': 1},
  363. {'name': 'voltage_harmonics_l1_l3_7th', 'start': 24460, 'length': 2, 'signed': False, 'decimals': 1},
  364. {'name': 'voltage_harmonics_l1_l3_8th', 'start': 24462, 'length': 2, 'signed': False, 'decimals': 1},
  365. {'name': 'voltage_harmonics_l1_l3_9th', 'start': 24464, 'length': 2, 'signed': False, 'decimals': 1},
  366. {'name': 'voltage_harmonics_l1_l3_10th', 'start': 24466, 'length': 2, 'signed': False, 'decimals': 1},
  367. {'name': 'voltage_harmonics_l1_l3_11th', 'start': 24468, 'length': 2, 'signed': False, 'decimals': 1},
  368. {'name': 'voltage_harmonics_l1_l3_12th', 'start': 24470, 'length': 2, 'signed': False, 'decimals': 1},
  369. {'name': 'voltage_harmonics_l1_l3_13th', 'start': 24472, 'length': 2, 'signed': False, 'decimals': 1},
  370. {'name': 'voltage_harmonics_l1_l3_14th', 'start': 24474, 'length': 2, 'signed': False, 'decimals': 1},
  371. {'name': 'voltage_harmonics_l1_l3_15th', 'start': 24476, 'length': 2, 'signed': False, 'decimals': 1},
  372. {'name': 'voltage_harmonics_l1_l3_16th', 'start': 24478, 'length': 2, 'signed': False, 'decimals': 1},
  373. {'name': 'current_harmonics_l1_thd', 'start': 24576, 'length': 2, 'signed': False, 'decimals': 1},
  374. {'name': 'current_harmonics_l1_2nd', 'start': 24578, 'length': 2, 'signed': False, 'decimals': 1},
  375. {'name': 'current_harmonics_l1_3rd', 'start': 24580, 'length': 2, 'signed': False, 'decimals': 1},
  376. {'name': 'current_harmonics_l1_4th', 'start': 24582, 'length': 2, 'signed': False, 'decimals': 1},
  377. {'name': 'current_harmonics_l1_5th', 'start': 24584, 'length': 2, 'signed': False, 'decimals': 1},
  378. {'name': 'current_harmonics_l1_6th', 'start': 24586, 'length': 2, 'signed': False, 'decimals': 1},
  379. {'name': 'current_harmonics_l1_7th', 'start': 24588, 'length': 2, 'signed': False, 'decimals': 1},
  380. {'name': 'current_harmonics_l1_8th', 'start': 24590, 'length': 2, 'signed': False, 'decimals': 1},
  381. {'name': 'current_harmonics_l1_9th', 'start': 24592, 'length': 2, 'signed': False, 'decimals': 1},
  382. {'name': 'current_harmonics_l1_10th', 'start': 24594, 'length': 2, 'signed': False, 'decimals': 1},
  383. {'name': 'current_harmonics_l1_11th', 'start': 24596, 'length': 2, 'signed': False, 'decimals': 1},
  384. {'name': 'current_harmonics_l1_12th', 'start': 24598, 'length': 2, 'signed': False, 'decimals': 1},
  385. {'name': 'current_harmonics_l1_13th', 'start': 24600, 'length': 2, 'signed': False, 'decimals': 1},
  386. {'name': 'current_harmonics_l1_14th', 'start': 24602, 'length': 2, 'signed': False, 'decimals': 1},
  387. {'name': 'current_harmonics_l1_15th', 'start': 24604, 'length': 2, 'signed': False, 'decimals': 1},
  388. {'name': 'current_harmonics_l1_16th', 'start': 24606, 'length': 2, 'signed': False, 'decimals': 1},
  389. {'name': 'current_harmonics_l2_thd', 'start': 24704, 'length': 2, 'signed': False, 'decimals': 1},
  390. {'name': 'current_harmonics_l2_2nd', 'start': 24706, 'length': 2, 'signed': False, 'decimals': 1},
  391. {'name': 'current_harmonics_l2_3rd', 'start': 24708, 'length': 2, 'signed': False, 'decimals': 1},
  392. {'name': 'current_harmonics_l2_4th', 'start': 24710, 'length': 2, 'signed': False, 'decimals': 1},
  393. {'name': 'current_harmonics_l2_5th', 'start': 24712, 'length': 2, 'signed': False, 'decimals': 1},
  394. {'name': 'current_harmonics_l2_6th', 'start': 24714, 'length': 2, 'signed': False, 'decimals': 1},
  395. {'name': 'current_harmonics_l2_7th', 'start': 24716, 'length': 2, 'signed': False, 'decimals': 1},
  396. {'name': 'current_harmonics_l2_8th', 'start': 24718, 'length': 2, 'signed': False, 'decimals': 1},
  397. {'name': 'current_harmonics_l2_9th', 'start': 24720, 'length': 2, 'signed': False, 'decimals': 1},
  398. {'name': 'current_harmonics_l2_10th', 'start': 24722, 'length': 2, 'signed': False, 'decimals': 1},
  399. {'name': 'current_harmonics_l2_11th', 'start': 24724, 'length': 2, 'signed': False, 'decimals': 1},
  400. {'name': 'current_harmonics_l2_12th', 'start': 24726, 'length': 2, 'signed': False, 'decimals': 1},
  401. {'name': 'current_harmonics_l2_13th', 'start': 24728, 'length': 2, 'signed': False, 'decimals': 1},
  402. {'name': 'current_harmonics_l2_14th', 'start': 24730, 'length': 2, 'signed': False, 'decimals': 1},
  403. {'name': 'current_harmonics_l2_15th', 'start': 24732, 'length': 2, 'signed': False, 'decimals': 1},
  404. {'name': 'current_harmonics_l2_16th', 'start': 24734, 'length': 2, 'signed': False, 'decimals': 1},
  405. {'name': 'current_harmonics_l3_thd', 'start': 24832, 'length': 2, 'signed': False, 'decimals': 1},
  406. {'name': 'current_harmonics_l3_2nd', 'start': 24834, 'length': 2, 'signed': False, 'decimals': 1},
  407. {'name': 'current_harmonics_l3_3rd', 'start': 24836, 'length': 2, 'signed': False, 'decimals': 1},
  408. {'name': 'current_harmonics_l3_4th', 'start': 24838, 'length': 2, 'signed': False, 'decimals': 1},
  409. {'name': 'current_harmonics_l3_5th', 'start': 24840, 'length': 2, 'signed': False, 'decimals': 1},
  410. {'name': 'current_harmonics_l3_6th', 'start': 24842, 'length': 2, 'signed': False, 'decimals': 1},
  411. {'name': 'current_harmonics_l3_7th', 'start': 24844, 'length': 2, 'signed': False, 'decimals': 1},
  412. {'name': 'current_harmonics_l3_8th', 'start': 24846, 'length': 2, 'signed': False, 'decimals': 1},
  413. {'name': 'current_harmonics_l3_9th', 'start': 24848, 'length': 2, 'signed': False, 'decimals': 1},
  414. {'name': 'current_harmonics_l3_10th', 'start': 24850, 'length': 2, 'signed': False, 'decimals': 1},
  415. {'name': 'current_harmonics_l3_11th', 'start': 24852, 'length': 2, 'signed': False, 'decimals': 1},
  416. {'name': 'current_harmonics_l3_12th', 'start': 24854, 'length': 2, 'signed': False, 'decimals': 1},
  417. {'name': 'current_harmonics_l3_13th', 'start': 24856, 'length': 2, 'signed': False, 'decimals': 1},
  418. {'name': 'current_harmonics_l3_14th', 'start': 24858, 'length': 2, 'signed': False, 'decimals': 1},
  419. {'name': 'current_harmonics_l3_15th', 'start': 24860, 'length': 2, 'signed': False, 'decimals': 1},
  420. {'name': 'current_harmonics_l3_16th', 'start': 24862, 'length': 2, 'signed': False, 'decimals': 1},
  421. {'name': 'current_harmonics_n_thd', 'start': 24960, 'length': 2, 'signed': False, 'decimals': 1},
  422. {'name': 'current_harmonics_n_2nd', 'start': 24962, 'length': 2, 'signed': False, 'decimals': 1},
  423. {'name': 'current_harmonics_n_3rd', 'start': 24964, 'length': 2, 'signed': False, 'decimals': 1},
  424. {'name': 'current_harmonics_n_4th', 'start': 24966, 'length': 2, 'signed': False, 'decimals': 1},
  425. {'name': 'current_harmonics_n_5th', 'start': 24968, 'length': 2, 'signed': False, 'decimals': 1},
  426. {'name': 'current_harmonics_n_6th', 'start': 24970, 'length': 2, 'signed': False, 'decimals': 1},
  427. {'name': 'current_harmonics_n_7th', 'start': 24972, 'length': 2, 'signed': False, 'decimals': 1},
  428. {'name': 'current_harmonics_n_8th', 'start': 24974, 'length': 2, 'signed': False, 'decimals': 1},
  429. {'name': 'current_harmonics_n_9th', 'start': 24976, 'length': 2, 'signed': False, 'decimals': 1},
  430. {'name': 'current_harmonics_n_10th', 'start': 24978, 'length': 2, 'signed': False, 'decimals': 1},
  431. {'name': 'current_harmonics_n_11th', 'start': 24980, 'length': 2, 'signed': False, 'decimals': 1},
  432. {'name': 'current_harmonics_n_12th', 'start': 24982, 'length': 2, 'signed': False, 'decimals': 1},
  433. {'name': 'current_harmonics_n_13th', 'start': 24984, 'length': 2, 'signed': False, 'decimals': 1},
  434. {'name': 'current_harmonics_n_14th', 'start': 24986, 'length': 2, 'signed': False, 'decimals': 1},
  435. {'name': 'current_harmonics_n_15th', 'start': 24988, 'length': 2, 'signed': False, 'decimals': 1},
  436. {'name': 'current_harmonics_n_16th', 'start': 24990, 'length': 2, 'signed': False, 'decimals': 1}]
  437. REGSETS = {
  438. "A43": ["active_import", "active_export", "active_net",
  439. "reactive_import", "reactive_export", "reactive_net",
  440. "apparent_import", "apparent_export", "apparent_net",
  441. "active_import_co2", "active_import_currency",
  442. "active_import_l1", "active_import_l2", "active_import_l3",
  443. "active_export_l1", "active_export_l2", "active_export_l3",
  444. "active_net_l1", "active_net_l2", "active_net_l3",
  445. "reactive_import_l1", "reactive_import_l2",
  446. "reactive_import_l3", "reactive_export_l1",
  447. "reactive_export_l2", "reactive_export_l3",
  448. "reactive_net_l1", "reactive_net_l2", "reactive_net_l3",
  449. "apparent_import_l1", "apparent_import_l2",
  450. "apparent_import_l3", "apparent_export_l1",
  451. "apparent_export_l2", "apparent_export_l3",
  452. "apparent_net_l1", "apparent_net_l2", "apparent_net_l3",
  453. "voltage_l1_n", "voltage_l2_n", "voltage_l3_n",
  454. "voltage_l1_l2", "voltage_l3_l2", "voltage_l1_l3",
  455. "current_l1", "current_l2", "current_l3",
  456. "active_power_total", "active_power_l1", "active_power_l2",
  457. "active_power_l3", "reactive_power_total",
  458. "reactive_power_l1", "reactive_power_l2",
  459. "reactive_power_l3", "apparent_power_total",
  460. "apparent_power_l1", "apparent_power_l2",
  461. "apparent_power_l3", "frequency", "phase_angle_power_total",
  462. "power_factor_total", "power_factor_l1", "power_factor_l2",
  463. "power_factor_l3", "current_quadrant_total",
  464. "current_quadrant_l1", "current_quadrant_l2",
  465. "current_quadrant_l3"],
  466. "B23": ["active_import", "active_export", "active_net",
  467. "reactive_import", "reactive_export", "reactive_net",
  468. "apparent_import", "apparent_export", "apparent_net",
  469. "active_import_co2", "active_import_currency",
  470. "active_import_l1", "active_import_l2", "active_import_l3",
  471. "active_export_l1", "active_export_l2", "active_export_l3",
  472. "active_net_l1", "active_net_l2", "active_net_l3",
  473. "reactive_import_l1", "reactive_import_l2",
  474. "reactive_import_l3", "reactive_export_l1",
  475. "reactive_export_l2", "reactive_export_l3",
  476. "reactive_net_l1", "reactive_net_l2", "reactive_net_l3",
  477. "apparent_import_l1", "apparent_import_l2",
  478. "apparent_import_l3", "apparent_export_l1",
  479. "apparent_export_l2", "apparent_export_l3",
  480. "apparent_net_l1", "apparent_net_l2", "apparent_net_l3",
  481. "voltage_l1_n", "voltage_l2_n", "voltage_l3_n",
  482. "voltage_l1_l2", "voltage_l3_l2", "voltage_l1_l3",
  483. "current_l1", "current_l2", "current_l3",
  484. "active_power_total", "active_power_l1", "active_power_l2",
  485. "active_power_l3", "reactive_power_total",
  486. "reactive_power_l1", "reactive_power_l2",
  487. "reactive_power_l3", "apparent_power_total",
  488. "apparent_power_l1", "apparent_power_l2",
  489. "apparent_power_l3", "frequency", "phase_angle_power_total",
  490. "power_factor_total", "power_factor_l1", "power_factor_l2",
  491. "power_factor_l3", "current_quadrant_total",
  492. "current_quadrant_l1", "current_quadrant_l2",
  493. "current_quadrant_l3"],
  494. "B21": ["active_import", "active_export", "active_net",
  495. "reactive_import", "reactive_export", "reactive_net",
  496. "apparent_import", "apparent_export", "apparent_net",
  497. "active_import_co2", "active_import_currency",
  498. "voltage_l1_n", "current_l1", "active_power_total",
  499. "reactive_power_total", "apparent_power_total", "frequency",
  500. "phase_angle_power_total", "power_factor_total",
  501. "current_quadrant_total"]
  502. }
  503. NULLS = [pow(2, n) - 1 for n in (64, 63, 32, 31, 16, 15)]
  504. class SMAMeter:
  505. """
  506. Implementation for a Sunny Boy SMA Modbus over TCP meter.
  507. """
  508. def __init__(self, port, tcp_port=502, slaveaddress=126, type=None, baudrate=None):
  509. self.device = socket.create_connection(address=(port, tcp_port))
  510. self.device_id = slaveaddress
  511. def read(self, regnames=None):
  512. if regnames is None:
  513. registers = SMAMeter.REGS
  514. return self._read_multiple(registers)
  515. if type(regnames) is str:
  516. registers = [register for register in SMAMeter.REGS if register['name'] == regnames]
  517. if len(registers) == 0:
  518. return "Register not found on device."
  519. return self._read_single(registers[0])
  520. if type(regnames) is list:
  521. registers = [register for register in SMAMeter.REGS if register['name'] in regnames]
  522. return self._read_multiple(registers)
  523. def _read_single(self, register):
  524. message = self._modbus_message(start_reg=register['start'], num_regs=register['length'])
  525. data = self._perform_request(message)
  526. return self._convert_value(data, signed=register['signed'], decimals=register['decimals'])
  527. def _read_multiple(self, registers):
  528. registers.sort(key=lambda reg: reg['start'])
  529. first_reg = min([register['start'] for register in registers])
  530. num_regs = max([register['start'] + register['length'] for register in registers]) - first_reg
  531. message = self._modbus_message(start_reg=first_reg, num_regs=num_regs)
  532. data = self._perform_request(message)
  533. return self._interpret_result(data, registers)
  534. def _modbus_message(self, start_reg, num_regs):
  535. transaction_id = random.randint(1, 2**16 - 1)
  536. return struct.pack(">HHHBBHH", transaction_id,
  537. SMAMeter.PROTOCOL_CODE,
  538. 6,
  539. self.device_id,
  540. SMAMeter.FUNCTION_CODE,
  541. start_reg - SMAMeter.REG_OFFSET,
  542. num_regs)
  543. def _perform_request(self, message):
  544. self.device.send(message)
  545. data = bytes()
  546. expect_bytes = 9 + 2 * struct.unpack(">H", message[-2:])[0]
  547. attempt = 1
  548. while len(data) is not expect_bytes:
  549. time.sleep(0.05)
  550. data += self.device.recv(2048)
  551. if attempt >= 10:
  552. return 2 * struct.unpack(">H", message[-2:])[0] * [0]
  553. attempt += 1
  554. return data[9:]
  555. def _interpret_result(self, data, registers):
  556. """
  557. Pull the returned string apart and package the data back to its
  558. intended form.
  559. Arguments:
  560. * data: list of register values returned from the device
  561. * registers: the original requested set of registers
  562. Returns:
  563. * A dict containing the register names and resulting values
  564. """
  565. first_reg = min([register['start'] for register in registers])
  566. results = {}
  567. for register in registers:
  568. regname = register['name']
  569. start = (register['start'] - first_reg) * 2
  570. end = start + register['length'] * 2
  571. values = data[start:end]
  572. results[regname] = self._convert_value(values=values,
  573. signed=register['signed'],
  574. decimals=register['decimals'])
  575. if regname == "power_factor_total" and results[regname] == 0:
  576. results[regname] = 1 # The SMA will send out a 0 when the power factor is 100%
  577. return results
  578. def _convert_value(self, values, signed=False, decimals=0):
  579. """
  580. Convert a list of returned integers to the intended value.
  581. Arguments:
  582. * bytestring: a list of integers that together represent the value
  583. * signed: whether the value is a signed value
  584. * decimals: number of decimals the return value should contain
  585. """
  586. numberOfBytes = len(values)
  587. formatcode_o = '>'
  588. if numberOfBytes == 1:
  589. if signed:
  590. formatcode_o += "b"
  591. else:
  592. formatcode_o += "B"
  593. if numberOfBytes == 2:
  594. if signed:
  595. formatcode_o += "h"
  596. else:
  597. formatcode_o += "H"
  598. if numberOfBytes == 4:
  599. if signed:
  600. formatcode_o += "l"
  601. else:
  602. formatcode_o += "L"
  603. value = struct.unpack(formatcode_o, bytes(values))[0]
  604. if value in SMAMeter.NULLS:
  605. return None
  606. else:
  607. return float(value) / 10 ** decimals
  608. REGS = [
  609. {'name': 'current_ac', 'start': 40188, 'length': 1, 'signed': False, 'decimals': 1},
  610. {'name': 'current_l1', 'start': 40189, 'length': 1, 'signed': False, 'decimals': 1},
  611. {'name': 'current_l2', 'start': 40190, 'length': 1, 'signed': False, 'decimals': 1},
  612. {'name': 'current_l3', 'start': 40191, 'length': 1, 'signed': False, 'decimals': 1},
  613. {'name': 'voltage_l1_l2', 'start': 40193, 'length': 1, 'signed': False, 'decimals': 1},
  614. {'name': 'voltage_l2_l3', 'start': 40194, 'length': 1, 'signed': False, 'decimals': 1},
  615. {'name': 'voltage_l3_l1', 'start': 40195, 'length': 1, 'signed': False, 'decimals': 1},
  616. {'name': 'voltage_l1_n', 'start': 40196, 'length': 1, 'signed': False, 'decimals': 1},
  617. {'name': 'voltage_l2_n', 'start': 40197, 'length': 1, 'signed': False, 'decimals': 1},
  618. {'name': 'voltage_l3_n', 'start': 40198, 'length': 1, 'signed': False, 'decimals': 1},
  619. {'name': 'active_power_total', 'start': 40200, 'length': 1, 'signed': True, 'decimals': -1},
  620. {'name': 'frequency', 'start': 40202, 'length': 1, 'signed': False, 'decimals': 2},
  621. {'name': 'apparent_power_total', 'start': 40204, 'length': 1, 'signed': True, 'decimals': -1},
  622. {'name': 'reactive_power_total', 'start': 40206, 'length': 1, 'signed': True, 'decimals': -1},
  623. {'name': 'power_factor_total', 'start': 40208, 'length': 1, 'signed': True, 'decimals': 3},
  624. {'name': 'active_export', 'start': 40210, 'length': 2, 'signed': False, 'decimals': 3},
  625. {'name': 'dc_power', 'start': 40217, 'length': 1, 'signed': False, 'decimals': -1},
  626. {'name': 'temperature_internal', 'start': 40219, 'length': 1, 'signed': False, 'decimals': 0},
  627. {'name': 'temperature_other', 'start': 40222, 'length': 1, 'signed': False, 'decimals': 0},
  628. {'name': 'operating_status', 'start': 40224, 'length': 1, 'signed': False, 'decimals': 0}
  629. ]
  630. REG_OFFSET = 1
  631. PROTOCOL_CODE = 0
  632. FUNCTION_CODE = 3
  633. NULLS = [2**16 - 1, 2**15 - 1, 2**15, -2**15]
  634. if __name__ == "__main__":
  635. import argparse
  636. parser = argparse.ArgumentParser(description='Read energy meters.')
  637. parser.add_argument('--device', '-d', dest='device', required=True, type=str, help='the serial port or IP-address for the device')
  638. parser.add_argument('--baud', '-b', dest='baud', required=False, default=38400, type=int, help='the serial baudrate')
  639. parser.add_argument('--type', '-t', dest='devtype', required=True, type=str, help='the type of energy meter: abb or sma')
  640. parser.add_argument('--slaveaddress', '-a', dest='slaveaddress', default=1, nargs=1, type=int, help='the modbus slave address (only when using serial)')
  641. parser.add_argument('--registers', '-r', dest='regs', type=str, help='the register you wish to read (default: all)', nargs='+')
  642. args = parser.parse_args()
  643. types = {'abb': ABBMeter,
  644. 'sma': SMAMeter}
  645. meter = types[args.devtype](port=args.device, baudrate=args.baud, slaveaddress=args.slaveaddress[0])
  646. if args.regs is None:
  647. result = meter.read()
  648. elif len(args.regs) == 1:
  649. result = meter.read(args.regs[0])
  650. else:
  651. result = meter.read(args.regs)
  652. if args.regs is None or len(args.regs) > 1:
  653. for key in result:
  654. print(key + ": " + str(result[key]))
  655. else:
  656. print(result)