Class: Cosmos::SerialDriver
Overview
A platform independent serial driver
Constant Summary collapse
Instance Method Summary collapse
-
#close ⇒ Object
Disconnects the driver from the comm port.
-
#closed? ⇒ Boolean
Whether the serial port has been closed.
-
#initialize(port_name, baud_rate, parity = :NONE, stop_bits = 1, write_timeout = 10.0, read_timeout = nil, flow_control = :NONE, data_bits = 8) ⇒ SerialDriver
constructor
A new instance of SerialDriver.
-
#read ⇒ String
Binary data read from the serial port.
-
#read_nonblock ⇒ String
Binary data read from the serial port.
- #write(data) ⇒ Object
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.
45 46 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 |
# File 'lib/cosmos/io/serial_driver.rb', line 45 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
#close ⇒ Object
Disconnects the driver from the comm port
81 82 83 |
# File 'lib/cosmos/io/serial_driver.rb', line 81 def close @driver.close end |
#closed? ⇒ Boolean
Returns Whether the serial port has been closed.
86 87 88 |
# File 'lib/cosmos/io/serial_driver.rb', line 86 def closed? @driver.closed? end |
#read ⇒ String
Returns Binary data read from the serial port.
96 97 98 |
# File 'lib/cosmos/io/serial_driver.rb', line 96 def read @driver.read end |
#read_nonblock ⇒ String
Returns Binary data read from the serial port.
101 102 103 |
# File 'lib/cosmos/io/serial_driver.rb', line 101 def read_nonblock @driver.read_nonblock end |
#write(data) ⇒ Object
91 92 93 |
# File 'lib/cosmos/io/serial_driver.rb', line 91 def write(data) @driver.write(data) end |