class LXP::Packet::ReadHold

Public Class Methods

new() click to toggle source
Calls superclass method LXP::Packet::Base::new
# File lib/lxp/packet/read_hold.rb, line 8
def initialize
  super

  self.tcp_function = TcpFunctions::TRANSLATED_DATA
  self.device_function = DeviceFunctions::READ_HOLD

  self.data_length = 18

  # in ReadHold packets, the value is the number of registers
  # we want to read. Default to 1.
  self.value = 1
end

Public Instance Methods

[](reg_num) click to toggle source

Subscript notation is used when the ReadHold packet has multiple registers in it. This is indicated by value_length > 2.

In this case, register tells us the first register in the values, then each 2 bytes are subsequent registers.

# File lib/lxp/packet/read_hold.rb, line 36
def [](reg_num)
  offset = (reg_num - register) * 2

  return if offset.negative? || offset > data_length

  value(offset)
end
to_h() click to toggle source

Return a Hash of all register->values in the ReadHold packet.

# File lib/lxp/packet/read_hold.rb, line 45
def to_h
  r = register

  values.each_slice(2).each_with_index.map do |v, idx|
    [r + idx, Utils.int(v)]
  end.to_h
end
value(offset = 0) click to toggle source

Return the first value in a ReadHold packet. This is normally used when the packet only has one value.

values returns an Array. This converts it to an int.

# File lib/lxp/packet/read_hold.rb, line 26
def value(offset = 0)
  Utils.int(values[offset, 2])
end