Class: ModBus::TCPServer
- Inherits:
-
GServer
- Object
- GServer
- ModBus::TCPServer
- Includes:
- Debug, Server
- Defined in:
- lib/rmodbus/tcp_server.rb
Overview
TCP server implementation
Constant Summary
Constant Summary
Constants included from Server
Instance Attribute Summary
Attributes included from Server
#coils, #discrete_inputs, #holding_registers, #input_registers, #uid
Attributes included from Debug
#debug, #raise_exception_on_mismatch, #read_retries, #read_retry_timeout
Instance Method Summary (collapse)
-
- (TCPServer) initialize(port = 502, uid = 1, opts = {})
constructor
Init server.
-
- (Object) serve(io)
Serve requests.
Methods included from Server
#exec_req, #parse_read_func, #parse_write_coil_func, #parse_write_multiple_coils_func, #parse_write_multiple_registers_func, #parse_write_register_func
Methods included from Debug
Constructor Details
- (TCPServer) initialize(port = 502, uid = 1, opts = {})
Init server
36 37 38 39 40 41 |
# File 'lib/rmodbus/tcp_server.rb', line 36 def initialize(port = 502, uid = 1, opts = {}) @uid = uid 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
- (Object) serve(io)
Serve requests
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rmodbus/tcp_server.rb', line 45 def serve(io) while not stopped? req = io.read(7) if req[2,2] == "\x00\x00" or req.getbyte(6) == @uid tr = req[0,2] len = req[4,2].unpack('n')[0] req = io.read(len - 1) log "Server RX (#{req.size} bytes): #{logging_bytes(req)}" pdu = exec_req(req, @coils, @discrete_inputs, @holding_registers, @input_registers) resp = tr + "\0\0" + (pdu.size + 1).to_word + @uid.chr + pdu log "Server TX (#{resp.size} bytes): #{logging_bytes(resp)}" io.write resp end end end |