Class: ModBus::Client Abstract

Inherits:
Object
  • Object
show all
Includes:
Debug, Errors, Options
Defined in:
lib/rmodbus/client.rb,
lib/rmodbus/client/slave.rb

Overview

This class is abstract.

Direct Known Subclasses

RTUClient, TCPClient

Defined Under Namespace

Classes: Slave

Instance Attribute Summary

Attributes included from Options

#raise_exception_on_mismatch, #read_retries, #read_retry_timeout

Attributes included from Debug

#logger, #raise_exception_on_mismatch, #read_retries, #read_retry_timeout

Instance Method Summary collapse

Constructor Details

#initialize(*args) { ... } ⇒ Client

Initialized client (alias :connect)

Examples:

Client.new(any_args) do |client|
  client.closed? #=> false
end

Parameters:

  • *args

    depends on implementation

Yields:

  • return client object and close it before exit



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rmodbus/client.rb', line 20

def initialize(*args)
  # Defaults
  @logger = nil
  @raise_exception_on_mismatch = false
  @read_retry_timeout = 1
  @read_retries = 1

  @io = open_connection(*args)
  if block_given?
    begin
      yield self
    ensure
      close
    end
  else
    self
  end
end

Instance Method Details

#closeObject

Close connections



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

def close
  @io.close unless @io.closed?
end

#closed?Boolean

Check connections

Returns:

  • (Boolean)


67
68
69
# File 'lib/rmodbus/client.rb', line 67

def closed?
  @io.closed?
end

#with_slave(uid) ⇒ Slave

Given slave object

Examples:

cl = Client.new
cl.with_slave(1) do |slave|
  slave.holding_registers[0..100]
end

Parameters:

  • uid (Integer, #read)

    slave devise

Returns:

  • (Slave)

    slave object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rmodbus/client.rb', line 52

def with_slave(uid)
  slave = get_slave(uid, @io)
  slave.logger = logger
  slave.raise_exception_on_mismatch = raise_exception_on_mismatch
  slave.read_retries = read_retries
  slave.read_retry_timeout = read_retry_timeout
  if block_given?
    yield slave
  else
    slave
  end
end