Class: Pzem016::HoldingRegisters

Inherits:
ModbusRegisters show all
Defined in:
lib/pzem016/pzem016.rb

Overview

Parsing logic for the “holding registers”, also known as read/write. PZEM-016 is configured via these

Instance Attribute Summary

Attributes inherited from ModbusRegisters

#raw, #slave

Instance Method Summary collapse

Methods inherited from ModbusRegisters

#initialize, #report, #to_json

Constructor Details

This class inherits a constructor from Pzem016::ModbusRegisters

Instance Method Details

#readObject

Specific holding register read logic for PZEM-016



65
66
67
# File 'lib/pzem016/pzem016.rb', line 65

def read
  @raw = @slave.holding_registers[0..6]
end

#reset_energyObject

Clears / resets the KWH (energy) counter on the PZEM-016



85
86
87
88
89
90
# File 'lib/pzem016/pzem016.rb', line 85

def reset_energy
  @slave.query("\x42")
rescue ModBus::Errors::IllegalFunction
  # the units respond with an error but in fact do reset the counter
  true
end

#set_address(addr) ⇒ Object

Sets the address of the slave to the new address addr



72
73
74
# File 'lib/pzem016/pzem016.rb', line 72

def set_address(addr)
  @slave.write_single_register(2, addr)
end

#set_alarm(watts) ⇒ Object

Sets the alarm threshold to the given number of watts



78
79
80
# File 'lib/pzem016/pzem016.rb', line 78

def set_alarm(watts)
  @slave.write_single_register(1, watts)
end

#to_hashObject

Specific logic for generating a hash of the holding registers in a PZEM-016



95
96
97
98
99
100
101
102
# File 'lib/pzem016/pzem016.rb', line 95

def to_hash
  {
    "SlaveAddr": @slave.uid,
    "DeviceName": @name,
    "Address": @raw[2],
    "AlarmWatts": @raw[1]
  }
end