Class: ModBus::ReadOnlyProxy

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

Overview

Given a slave and a type of operation, execute a single or multiple read using hash syntax

Direct Known Subclasses

ReadWriteProxy

Instance Method Summary collapse

Constructor Details

#initialize(slave, type) ⇒ ReadOnlyProxy

Initialize a proxy for a slave and a type of operation



7
8
9
# File 'lib/rmodbus/proxy.rb', line 7

def initialize(slave, type)
  @slave, @type = slave, type
end

Instance Method Details

#[](key) ⇒ Object

Read single or multiple values from 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



13
14
15
16
17
18
19
20
21
# File 'lib/rmodbus/proxy.rb', line 13

def [](key)
  if key.instance_of?(0.class)
    @slave.send(:"read_#{@type}", key)
  elsif key.instance_of?(Range)
    @slave.send(:"read_#{@type}s", key.first, key.count)
  else
    raise ModBus::Errors::ProxyException, "Invalid argument, must be integer or range. Was #{key.class}"
  end
end