Class: HackyHAL::DeviceControllers::GenericSerialPort
- Defined in:
- lib/hacky_hal/device_controllers/generic_serial_port.rb
Direct Known Subclasses
Defined Under Namespace
Classes: CommandError
Constant Summary collapse
- MAX_READ_WRITE_RETRIES =
1
- DEFAULT_COMMAND_TIMEOUT =
3
- DEFAULT_OPTIONS =
{ baud_rate: 9600, data_bits: 8, stop_bits: 1, parity: SerialPort::NONE, flow_control: SerialPort::NONE }
Instance Attribute Summary collapse
-
#serial_device_path ⇒ Object
readonly
Returns the value of attribute serial_device_path.
-
#serial_options ⇒ Object
readonly
Returns the value of attribute serial_options.
Attributes included from Options
Instance Method Summary collapse
- #disconnect ⇒ Object
-
#initialize(options = {}) ⇒ GenericSerialPort
constructor
A new instance of GenericSerialPort.
- #read_command(timeout = DEFAULT_COMMAND_TIMEOUT) ⇒ Object
- #serial_port ⇒ Object
- #write_command(command) ⇒ Object
Methods inherited from Base
Methods included from Options
Constructor Details
#initialize(options = {}) ⇒ GenericSerialPort
Returns a new instance of GenericSerialPort.
23 24 25 26 27 28 29 |
# File 'lib/hacky_hal/device_controllers/generic_serial_port.rb', line 23 def initialize( = {}) super() ensure_option(:serial_device_path) @serial_device_path = [:serial_device_path] @serial_options = DEFAULT_OPTIONS.merge([:serial_options] || {}) end |
Instance Attribute Details
#serial_device_path ⇒ Object (readonly)
Returns the value of attribute serial_device_path.
21 22 23 |
# File 'lib/hacky_hal/device_controllers/generic_serial_port.rb', line 21 def serial_device_path @serial_device_path end |
#serial_options ⇒ Object (readonly)
Returns the value of attribute serial_options.
21 22 23 |
# File 'lib/hacky_hal/device_controllers/generic_serial_port.rb', line 21 def @serial_options end |
Instance Method Details
#disconnect ⇒ Object
55 56 57 58 |
# File 'lib/hacky_hal/device_controllers/generic_serial_port.rb', line 55 def disconnect @serial_port.close if @serial_port @serial_port = nil end |
#read_command(timeout = DEFAULT_COMMAND_TIMEOUT) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/hacky_hal/device_controllers/generic_serial_port.rb', line 41 def read_command(timeout = DEFAULT_COMMAND_TIMEOUT) set_read_timeout(timeout) if timeout begin output = read_line log("Read: #{output.inspect}", :debug) handle_error if error_response?(output) output rescue EOFError log("Read EOFError", :warn) nil end end |
#serial_port ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/hacky_hal/device_controllers/generic_serial_port.rb', line 60 def serial_port @serial_port ||= SerialPort.new(serial_device_path).tap do |s| s.baud = [:baud_rate] s.data_bits = [:data_bits] s.stop_bits = [:stop_bits] s.parity = [:parity] s.flow_control = [:flow_control] end end |
#write_command(command) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/hacky_hal/device_controllers/generic_serial_port.rb', line 31 def write_command(command) read_write_retry do serial_port.flush command = "#{command}\r\n" log("Wrote: #{command.inspect}", :debug) serial_port.write(command) true end end |