Class: OpenC3::SerialDriver

Inherits:
Object show all
Defined in:
lib/openc3/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, flow_control = :NONE, data_bits = 8) ⇒ 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) (defaults to: 10.0)

    Seconds to wait before aborting writes

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

    Seconds to wait before aborting reads. Pass nil to block until the read is complete.

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

    Currently supported :NONE and :RTSCTS (default :NONE)

  • data_bits (Integer) (defaults to: 8)

    Number of data bits (default 8)

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openc3/io/serial_driver.rb', line 47

def initialize(port_name,
               baud_rate,
               parity = :NONE,
               stop_bits = 1,
               write_timeout = 10.0,
               read_timeout = nil,
               flow_control = :NONE,
               data_bits = 8)
  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,
                                    0.01,
                                    1000,
                                    flow_control,
                                    data_bits)
  elsif RUBY_ENGINE == 'ruby'
    @driver = PosixSerialDriver.new(port_name,
                                    baud_rate,
                                    parity,
                                    stop_bits,
                                    write_timeout,
                                    read_timeout,
                                    flow_control,
                                    data_bits)
  else
    @driver = nil # JRuby Serial on Linux not currently supported
  end
end

Instance Method Details

#closeObject

Disconnects the driver from the comm port



83
84
85
# File 'lib/openc3/io/serial_driver.rb', line 83

def close
  @driver.close
end

#closed?Boolean

Returns Whether the serial port has been closed.

Returns:

  • (Boolean)

    Whether the serial port has been closed



88
89
90
# File 'lib/openc3/io/serial_driver.rb', line 88

def closed?
  @driver.closed?
end

#readString

Returns Binary data read from the serial port.

Returns:

  • (String)

    Binary data read from the serial port



98
99
100
# File 'lib/openc3/io/serial_driver.rb', line 98

def read
  @driver.read
end

#read_nonblockString

Returns Binary data read from the serial port.

Returns:

  • (String)

    Binary data read from the serial port



103
104
105
# File 'lib/openc3/io/serial_driver.rb', line 103

def read_nonblock
  @driver.read_nonblock
end

#write(data) ⇒ Object

Parameters:

  • data (String)

    Binary data to write to the serial port



93
94
95
# File 'lib/openc3/io/serial_driver.rb', line 93

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