Class: ModBus::RTUServer

Inherits:
Object
  • Object
show all
Includes:
Debug, RTU, SP, Server
Defined in:
lib/rmodbus/rtu_server.rb

Overview

RTU server implementation

Examples:

srv = RTUServer.new('/dev/ttyS1', 9600)
slave = srv.with_slave(1)
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 SP

#baud, #data_bits, #parity, #port, #read_timeout, #stop_bits

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 SP

#open_serial_port

Methods included from RTU

#clean_input_buff, #crc16, #read, #read_rtu_request, #read_rtu_response, #serve

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, #with_slave

Methods included from Debug

#log, #logging_bytes

Constructor Details

#initialize(port, baud = 9600, opts = {}) ⇒ RTUServer

Init RTU server

Parameters:

See Also:



21
22
23
24
25
26
27
28
# File 'lib/rmodbus/rtu_server.rb', line 21

def initialize(port, baud=9600, opts = {})
  Thread.abort_on_exception = true
  if port.is_a?(IO) || port.respond_to?(:read)
    @sp = port
  else
    @sp = open_serial_port(port, baud, opts)
  end
end

Instance Method Details

#joinObject

Join server



44
45
46
# File 'lib/rmodbus/rtu_server.rb', line 44

def join
  @serv.join
end

#startObject

Start server



31
32
33
34
35
# File 'lib/rmodbus/rtu_server.rb', line 31

def start
  @serv = Thread.new do
    serve(@sp)
  end
end

#stopObject

Stop server



38
39
40
41
# File 'lib/rmodbus/rtu_server.rb', line 38

def stop
  Thread.kill(@serv)
  @sp.close
end