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

Methods included from Debug

#log, #logging_bytes

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



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

def initialize(*args, &block)
  # 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



70
71
72
# File 'lib/rmodbus/client.rb', line 70

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

#closed?Boolean

Check connections

Returns:

  • (Boolean)


65
66
67
# File 'lib/rmodbus/client.rb', line 65

def closed?
  @io.closed?
end

#get_slave(uid, io) ⇒ Object (protected)



90
91
92
# File 'lib/rmodbus/client.rb', line 90

def get_slave(uid,io)
  Slave.new(uid, io)
end

#open_connection(*args) ⇒ Object (protected)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rmodbus/client.rb', line 75

def open_connection(*args)
  #Stub conn object
  @io = Object.new

  @io.instance_eval """
    def close
    end

    def closed?
      true
    end
    """
  @io
end

#with_slave(uid, &block) ⇒ 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



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

def with_slave(uid, &block)
  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