module Voltronic::Protocol

Public Class Methods

for_io(io) click to toggle source

Create a new Protocol object using an input IO object

   # File lib/voltronic/protocol.rb
11 def self.for_io(io)
12   require 'voltronic/protocols/io'
13 
14   def self.for_io(io)
15     ::Voltronic::Protocol::IO.method(:new).call(io)
16   end
17 
18   for_io(io)
19 end
for_serialport(port_or_dev, baud = 2400, data_bits = 8, stop_bits = 1, parity = :none) click to toggle source

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

   # File lib/voltronic/protocol.rb
23 def self.for_serialport(port_or_dev, baud = 2400, data_bits = 8, stop_bits = 1, parity = :none)
24   begin
25     require 'serialport'
26   rescue Exception
27     raise LoadError.new 'RubyGem "serialport" required to make use of Axpert on a Serial Port'
28   end
29 
30   def self.for_serialport(port_or_dev, baud = 2400, data_bits = 8, stop_bits = 1, parity = :none)
31     port_or_dev = port_or_dev.to_s.strip
32 
33     baud = begin
34       parse = Integer(baud)
35       raise if (1 > parse)
36       parse
37     rescue
38       raise ArgumentError.new "Invalid baud #{baud}"
39     end
40 
41     data_bits = begin
42       parse = Integer(data_bits)
43       raise unless ((5 == parse) || (6 == parse) || (7 == parse) || (8 == parse))
44       parse
45     rescue
46       raise ArgumentError.new "Invalid data bits #{data_bits}"
47     end
48 
49     stop_bits = begin
50       parse = Integer(stop_bits)
51       raise unless ((1 == parse) || (2 == parse))
52       parse
53     rescue
54       raise ArgumentError.new "Invalid stop bits #{stop_bits}"
55     end
56 
57     parity = begin
58       parse = parity.to_s.strip.upcase
59       raise unless (('NONE' == parse) || ('EVEN' == parse) || ('ODD' == parse) || ('MARK' == parse) || ('SPACE' == parse))
60       ::SerialPort.const_get(parse.to_sym)
61     rescue
62       raise ArgumentError.new "Invalid Parity #{parity}"
63     end
64 
65     serial_io = ::SerialPort.new(port_or_dev, baud, data_bits, stop_bits, parity)
66     serial_io.read_timeout = -1
67     for_io(serial_io)
68   end
69 
70   for_serialport(port_or_dev, baud, data_bits, stop_bits, parity)
71 end
for_usb(dev) click to toggle source

Create a new Protocol object for Axpert device connected to USB

Currently only supported on Linux kernels that include HIDRaw

   # File lib/voltronic/protocol.rb
77 def self.for_usb(dev)
78   if !(RUBY_PLATFORM =~ /linux/)
79     raise NotImplementedError.new 'USB is currently only supported in Linux'
80   end
81 
82   def self.for_usb(dev)
83     for_io(::File.open(dev.to_s, (::IO::RDWR | ::IO::NONBLOCK)))
84   end
85 
86   for_usb(dev)
87 end

Public Instance Methods

execute(input, timeout = 1.0) click to toggle source

Execute a query on the device

   # File lib/voltronic/protocol.rb
91 def execute(input, timeout = 1.0)
92   raise NotImplementedError.new 'Protocol implementation does not implement execute(..)'
93 end