module ModBus::TCP

Attributes

ipaddr[R]
port[R]

Public Instance Methods

open_tcp_connection(ipaddr, port, opts = {}) click to toggle source

Open TCP socket

@param [String] ipaddr IP address of remote server @param [Integer] port connection port @param [Hash] opts options of connection @option opts [Float, Integer] :connect_timeout seconds timeout for open socket @return [TCPSocket] socket

@raise [ModBusTimeout] timed out attempting to create connection

# File lib/rmodbus/tcp.rb, line 18
def open_tcp_connection(ipaddr, port, opts = {})
  @ipaddr, @port = ipaddr, port

  opts[:connect_timeout] ||= 1

  io = nil
  begin
    timeout(opts[:connect_timeout], ModBusTimeout) do
      io = TCPSocket.new(@ipaddr, @port)
    end
  rescue ModBusTimeout => err
    raise ModBusTimeout.new, 'Timed out attempting to create connection'
  end

  io
end