Class: ModBus::TCPServer

Inherits:
GServer
  • Object
show all
Includes:
Debug, Server
Defined in:
lib/rmodbus/tcp_server.rb

Overview

TCP server implementation

Examples:

srv = TCPServer.new(10002)
slave = srv.with_slave(255)
slave.coils = [1,0,1,1]
slave.discrete_inputs = [1,1,0,0]
slave.holding_registers = [1,2,3,4]
slave.input_registers = [1,2,3,4]
srv.logger = Logger.new($stdout)
srv.start

Constant Summary

Constants included from Server

Server::Funcs

Instance Attribute Summary

Attributes included from Server

#promiscuous, #request_callback, #response_callback

Attributes included from Debug

#logger, #raise_exception_on_mismatch, #read_retries, #read_retry_timeout

Instance Method Summary collapse

Methods included from Server

#exec_req, #parse_mask_write_register_func, #parse_read_func, #parse_read_write_multiple_registers_func, #parse_request, #parse_response, #parse_write_coil_func, #parse_write_multiple_coils_func, #parse_write_multiple_registers_func, #parse_write_register_func, #process_func, #slaves, #validate_read_func, #validate_read_write_multiple_registers_func, #validate_write_coil_func, #validate_write_multiple_coils_func, #validate_write_multiple_registers_func, #validate_write_register_func

Methods included from Debug

#log, #logging_bytes

Constructor Details

#initialize(port = 502, opts = {}) ⇒ TCPServer

Init server

Parameters:

  • port (Integer) (defaults to: 502)

    listen port

  • uid (Integer)

    slave device

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

    options of server

Options Hash (opts):

  • :host (String)

    host of server default ‘127.0.0.1’

  • :max_connection (Float, Integer)

    max of TCP connection with server default 4



28
29
30
31
32
# File 'lib/rmodbus/tcp_server.rb', line 28

def initialize(port = 502, opts = {})
    opts[:host] = DEFAULT_HOST unless opts[:host]
    opts[:max_connection] = 4 unless opts[:max_connection]
	super(port, host = opts[:host], maxConnection = opts[:max_connection])
end

Instance Method Details

#serve(io) ⇒ Object

Serve requests

Parameters:

  • io (TCPSocket)

    socket



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rmodbus/tcp_server.rb', line 41

def serve(io)
  while not stopped?
    header = io.read(7)
    tx_id = header[0,2]
    proto_id = header[2,2]
    len = header[4,2].unpack('n')[0]
    unit_id = header.getbyte(6)
    if proto_id == "\x00\x00"
      req = io.read(len - 1)
      log "Server RX (#{req.size} bytes): #{logging_bytes(req)}"

      func = req.getbyte(0)
      params = parse_request(func, req)
      pdu = exec_req(unit_id, func, params, req)

      if pdu
        resp = tx_id + "\0\0" + (pdu.size + 1).to_word + unit_id.chr + pdu
        log "Server TX (#{resp.size} bytes): #{logging_bytes(resp)}"
        io.write resp
      else
        log "Ignored server RX (invalid unit ID #{unit_id}, #{req.size} bytes): #{logging_bytes(req)}"
      end
    end
  end
end

#with_slave(uid = 255) ⇒ Object

set the default param



35
36
37
# File 'lib/rmodbus/tcp_server.rb', line 35

def with_slave(uid = 255)
  super
end