Module: ModBus::SP

Included in:
RTUClient, RTUServer
Defined in:
lib/rmodbus/sp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#baudObject (readonly)

Returns the value of attribute baud.



9
10
11
# File 'lib/rmodbus/sp.rb', line 9

def baud
  @baud
end

#data_bitsObject (readonly)

Returns the value of attribute data_bits.



9
10
11
# File 'lib/rmodbus/sp.rb', line 9

def data_bits
  @data_bits
end

#parityObject (readonly)

Returns the value of attribute parity.



9
10
11
# File 'lib/rmodbus/sp.rb', line 9

def parity
  @parity
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/rmodbus/sp.rb', line 9

def port
  @port
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



9
10
11
# File 'lib/rmodbus/sp.rb', line 9

def read_timeout
  @read_timeout
end

#stop_bitsObject (readonly)

Returns the value of attribute stop_bits.



9
10
11
# File 'lib/rmodbus/sp.rb', line 9

def stop_bits
  @stop_bits
end

Instance Method Details

#open_serial_port(port, baud, opts = {}) ⇒ SerialPort

Open serial port

Parameters:

  • port (String)

    name serial ports (“/dev/ttyS0” POSIX, “com1” - Windows)

  • baud (Integer)

    rate serial port (default 9600)

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

    the options of serial port

Options Hash (opts):

  • :data_bits (Integer)

    from 5 to 8

  • :stop_bits (Integer)

    1 or 2

  • :parity (Integer)

    NONE, EVEN or ODD

  • :read_timeout (Integer)

    default 100 ms

Returns:

  • (SerialPort)

    io serial port



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rmodbus/sp.rb', line 20

def open_serial_port(port, baud, opts = {})
  @port, @baud = port, baud

  @data_bits, @stop_bits, @parity, @read_timeout = 8, 1, SerialPort::NONE, 100

  @data_bits = opts[:data_bits] unless opts[:data_bits].nil?
  @stop_bits = opts[:stop_bits] unless opts[:stop_bits].nil?
  @parity = opts[:parity] unless opts[:parity].nil?
  @read_timeout = opts[:read_timeout] unless opts[:read_timeout].nil?

  io = SerialPort.new(@port, @baud, @data_bits, @stop_bits, @parity)
  io.flow_control = SerialPort::NONE
  io.read_timeout = @read_timeout
  io
end