Module: ModBus::TCP

Includes:
Errors
Included in:
RTUClient, TCPClient
Defined in:
lib/rmodbus/tcp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ipaddrObject (readonly)

Returns the value of attribute ipaddr.



6
7
8
# File 'lib/rmodbus/tcp.rb', line 6

def ipaddr
  @ipaddr
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/rmodbus/tcp.rb', line 6

def port
  @port
end

Instance Method Details

#open_tcp_connection(ipaddr, port, opts = {}) ⇒ Socket

Open TCP socket

Parameters:

  • ipaddr (String)

    IP address of remote server

  • port (Integer)

    connection port

  • opts (Hash) (defaults to: {})

    options of connection

Options Hash (opts):

  • :connect_timeout (Float, Integer)

    seconds timeout for open socket

Returns:

  • (Socket)

    socket

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rmodbus/tcp.rb', line 16

def open_tcp_connection(ipaddr, port, opts = {})
  @ipaddr, @port = ipaddr, port

  timeout = opts[:connect_timeout] ||= 1

  io = nil
  begin
    io = Socket.tcp(@ipaddr, @port, nil, nil, connect_timeout: timeout)
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT
    raise ModBusTimeout.new, 'Timed out attempting to create connection'
  end

  io
end