Class: ModBus::ReadWriteProxy

Inherits:
ReadOnlyProxy show all
Defined in:
lib/rmodbus/proxy.rb

Instance Method Summary collapse

Methods inherited from ReadOnlyProxy

#[], #initialize

Constructor Details

This class inherits a constructor from ModBus::ReadOnlyProxy

Instance Method Details

#[]=(key, val) ⇒ Object

Write single or multiple values to a modbus slave depending on whether a Fixnum or a Range was given. Note that in the case of multiples, a pluralized version of the method is sent to the slave. Also when writing multiple values, the number of elements must match the number of registers in the range or an exception is raised



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rmodbus/proxy.rb', line 31

def []=(key, val)
  if key.instance_of?(0.class)
    @slave.send(:"write_#{@type}", key, val)
  elsif key.instance_of?(Range)
    if key.count != val.size
      raise ModBus::Errors::ProxyException,
            "The size of the range must match the size of the values (#{key.count} != #{val.size})"
    end

    @slave.send(:"write_#{@type}s", key.first, val)
  else
    raise ModBus::Errors::ProxyException, "Invalid argument, must be integer or range. Was #{key.class}"
  end
end