Class: HackyHAL::DeviceControllers::GenericSerialPort

Inherits:
Base
  • Object
show all
Defined in:
lib/hacky_hal/device_controllers/generic_serial_port.rb

Direct Known Subclasses

EpsonProjector, IoGearAviorHdmiSwitch

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

Attributes included from Options

#options

Instance Method Summary collapse

Methods inherited from Base

#log

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(options = {})
  super(options)
  ensure_option(:serial_device_path)

  @serial_device_path = options[:serial_device_path]
  @serial_options = DEFAULT_OPTIONS.merge(options[:serial_options] || {})
end

Instance Attribute Details

#serial_device_pathObject (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_optionsObject (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
  @serial_options
end

Instance Method Details

#disconnectObject



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_portObject



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 = serial_options[:baud_rate]
    s.data_bits = serial_options[:data_bits]
    s.stop_bits = serial_options[:stop_bits]
    s.parity = serial_options[:parity]
    s.flow_control = serial_options[: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