Module: Voltronic::Protocol

Defined in:
lib/voltronic/protocol.rb

Defined Under Namespace

Classes: IO

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_io(io) ⇒ Object

Create a new Protocol object using an input IO object



11
12
13
14
15
16
17
18
19
# File 'lib/voltronic/protocol.rb', line 11

def self.for_io(io)
  require 'voltronic/protocols/io'

  def self.for_io(io)
    ::Voltronic::Protocol::IO.method(:new).call(io)
  end

  for_io(io)
end

.for_serialport(port_or_dev, baud = 2400, data_bits = 8, stop_bits = 1, parity = :none) ⇒ Object

Create a new Protocol object for Axpert device connected to a serial port



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/voltronic/protocol.rb', line 23

def self.for_serialport(port_or_dev, baud = 2400, data_bits = 8, stop_bits = 1, parity = :none)
  begin
    require 'serialport'
  rescue Exception
    raise LoadError.new 'RubyGem "serialport" required to make use of Axpert on a Serial Port'
  end

  def self.for_serialport(port_or_dev, baud = 2400, data_bits = 8, stop_bits = 1, parity = :none)
    port_or_dev = port_or_dev.to_s.strip

    baud = begin
      parse = Integer(baud)
      raise if (1 > parse)
      parse
    rescue
      raise ArgumentError.new "Invalid baud #{baud}"
    end

    data_bits = begin
      parse = Integer(data_bits)
      raise unless ((5 == parse) || (6 == parse) || (7 == parse) || (8 == parse))
      parse
    rescue
      raise ArgumentError.new "Invalid data bits #{data_bits}"
    end

    stop_bits = begin
      parse = Integer(stop_bits)
      raise unless ((1 == parse) || (2 == parse))
      parse
    rescue
      raise ArgumentError.new "Invalid stop bits #{stop_bits}"
    end

    parity = begin
      parse = parity.to_s.strip.upcase
      raise unless (('NONE' == parse) || ('EVEN' == parse) || ('ODD' == parse) || ('MARK' == parse) || ('SPACE' == parse))
      ::SerialPort.const_get(parse.to_sym)
    rescue
      raise ArgumentError.new "Invalid Parity #{parity}"
    end

    serial_io = ::SerialPort.new(port_or_dev, baud, data_bits, stop_bits, parity)
    serial_io.read_timeout = -1
    for_io(serial_io)
  end

  for_serialport(port_or_dev, baud, data_bits, stop_bits, parity)
end

.for_usb(dev) ⇒ Object

Create a new Protocol object for Axpert device connected to USB

Currently only supported on Linux kernels that include HIDRaw



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/voltronic/protocol.rb', line 77

def self.for_usb(dev)
  if !(RUBY_PLATFORM =~ /linux/)
    raise NotImplementedError.new 'USB is currently only supported in Linux'
  end

  def self.for_usb(dev)
    for_io(::File.open(dev.to_s, (::IO::RDWR | ::IO::NONBLOCK)))
  end

  for_usb(dev)
end

Instance Method Details

#execute(input, timeout = 1.0) ⇒ Object

Execute a query on the device



91
92
93
# File 'lib/voltronic/protocol.rb', line 91

def execute(input, timeout = 1.0)
  raise NotImplementedError.new 'Protocol implementation does not implement execute(..)'
end