Class: Cosmos::SerialDriver

Inherits:
Object show all
Defined in:
lib/cosmos/io/serial_driver.rb

Overview

A platform independent serial driver

Constant Summary collapse

EVEN =
:EVEN
ODD =
:ODD
NONE =
:NONE
VALID_PARITY =
[EVEN,ODD,NONE]

Instance Method Summary collapse

Constructor Details

#initialize(port_name, baud_rate, parity = :NONE, stop_bits = 1, write_timeout = 10.0, read_timeout = nil) ⇒ SerialDriver

Returns a new instance of SerialDriver.

Parameters:

  • port_name (String)

    Name of the serial port

  • baud_rate (Integer)

    Serial port baud rate

  • parity (Symbol) (defaults to: :NONE)

    Must be one of :EVEN, :ODD or :NONE

  • stop_bits (Integer) (defaults to: 1)

    Number of stop bits

  • write_timeout (Float|nil) (defaults to: 10.0)

    Number of seconds to wait for the write to complete or nil to block

  • read_timeout (Float|nil) (defaults to: nil)

    Number of seconds to wait for the read to complete or nil to block

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cosmos/io/serial_driver.rb', line 34

def initialize(port_name,
               baud_rate,
               parity = :NONE,
               stop_bits = 1,
               write_timeout = 10.0,
               read_timeout = nil)
  raise(ArgumentError, "Invalid parity: #{parity}") unless VALID_PARITY.include? parity
  if Kernel.is_windows?
    @driver = Win32SerialDriver.new(port_name,
                                    baud_rate,
                                    parity,
                                    stop_bits,
                                    write_timeout,
                                    read_timeout)
  else
    @driver = PosixSerialDriver.new(port_name,
                                    baud_rate,
                                    parity,
                                    stop_bits,
                                    write_timeout,
                                    read_timeout)
  end
end

Instance Method Details

#closeObject

Disconnects the driver from the comm port



59
60
61
# File 'lib/cosmos/io/serial_driver.rb', line 59

def close
  @driver.close
end

#closed?Boolean

Returns Whether the serial port has been closed.

Returns:

  • (Boolean)

    Whether the serial port has been closed



64
65
66
# File 'lib/cosmos/io/serial_driver.rb', line 64

def closed?
  @driver.closed?
end

#readString

Returns Binary data read from the serial port.

Returns:

  • (String)

    Binary data read from the serial port



74
75
76
# File 'lib/cosmos/io/serial_driver.rb', line 74

def read
  @driver.read
end

#read_nonblockString

Returns Binary data read from the serial port.

Returns:

  • (String)

    Binary data read from the serial port



79
80
81
# File 'lib/cosmos/io/serial_driver.rb', line 79

def read_nonblock
  @driver.read_nonblock
end

#write(data) ⇒ Object

Parameters:

  • data (String)

    Binary data to write to the serial port



69
70
71
# File 'lib/cosmos/io/serial_driver.rb', line 69

def write(data)
  @driver.write(data)
end