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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rmodbus/proxy.rb', line 39

def []=(key, val)
  if key.instance_of?(Fixnum)
    @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