Class: ModBus::TCPServer
- Inherits:
-
GServer
- Object
- GServer
- ModBus::TCPServer
- Defined in:
- lib/rmodbus/tcp_server.rb
Overview
TCP server implementation
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
-
#initialize(port = 502, uid = 1, opts = {}) ⇒ TCPServer
constructor
Init server.
-
#serve(io) ⇒ Object
Serve requests.
Constructor Details
#initialize(port = 502, uid = 1, opts = {}) ⇒ TCPServer
Init server
27 28 29 30 31 32 33 34 35 |
# File 'lib/rmodbus/tcp_server.rb', line 27 def initialize(port = 502, uid = 1, opts = {}) @uid = uid warn "[WARNING] Please, use UID = 255. It will be fixed in the next release." if @uid != 0xff 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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rmodbus/tcp_server.rb', line 39 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) if unit_id == @uid || unit_id == 0 log "Server RX (#{req.size} bytes): #{logging_bytes(req)}" pdu = exec_req(req, @coils, @discrete_inputs, @holding_registers, @input_registers) resp = tx_id + "\0\0" + (pdu.size + 1).to_word + @uid.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 |