class MicroAethAE51::Com

Initializes a link between the the system and the device. used to transmit and recieive MicroAeth::Message

Attributes

com[RW]
com_thread[R]
messages[RW]

Public Class Methods

new() click to toggle source
# File lib/micro_aeth-ae51.rb, line 100
def initialize
  port     = '/dev/serial/by-id/usb-AethLabs_microAeth_Model_AE51_AE51-S4-649-1303-if00-port0'
  baud     = 500_000
  bytesize = 8
  stopbits = 1
  parity   = SerialPort::MARK
  @com     = SerialPort.new port, baud, bytesize, stopbits, parity
  @messages = []
end

Public Instance Methods

clear_buffer() click to toggle source
# File lib/micro_aeth-ae51.rb, line 132
def clear_buffer
  begin
    while true 
      Timeout::timeout(0.5) { read_message }
    end
  rescue Timeout::Error
    nil
  end
end
erase_flash() click to toggle source
# File lib/micro_aeth-ae51.rb, line 119
def erase_flash
  begin 
    clear_buffer
    write MicroAeth::Instruction::EraseFlash
    Timeout::timeout(60) { wait_for_acknowledge }
    sleep 45
    Timeout::timeout(60) { wait_for_acknowledge }
    write MicroAeth::Instruction::StartWrite
    Timeout::timeout(60) { wait_for_acknowledge }
  rescue Timeout::Error
    retry
  end 
end
read() click to toggle source

@return A ruby thread which continually reads

from the MicroAeth::Com#com instance
# File lib/micro_aeth-ae51.rb, line 160
def read
  @com_thread = Thread.new do
    begin
      while true
        @messages << ( Message.new read_message )
      end
    # Intermitently, it the serialport library raises end of file...
    rescue EOFError
      sleep 1
      retry
    end 
  end
end
read_message() click to toggle source
# File lib/micro_aeth-ae51.rb, line 210
def read_message
  m, c = '',''
  while c != "\x02"; c = @com.readchar; end
  c = @com.readchar
  m << c
  len = c.byte
  0.upto len do |i|
    c = @com.readchar
    m << c
  end
  while c != "\x03"
    c = @com.readchar
    m << c
  end
  m[0..-2]
end
sigma_ap(m, m_prev) click to toggle source
# File lib/micro_aeth-ae51.rb, line 202
def sigma_ap m, m_prev
  if m_prev.nil? 
    "NaN" 
  else 
    Math::PI * (0.3 ** 2) / 4.0 / m.flow.to_f * 60.0 * 
      Math.log( m_prev.sen1.to_f / m.sen1.to_f * m.ref.to_f / m_prev.ref.to_f) * (10.0 ** 8)
  end
end
start() click to toggle source
# File lib/micro_aeth-ae51.rb, line 148
def start
  puts "The MicroAeth is starting!.."
  begin 
    clear_buffer
  rescue [EOFError, Timeout::Error]
    raise "Problem stating the MicroAeth"
  end
end
start_write_to_file(file_name) click to toggle source

@file a ruby file object

# File lib/micro_aeth-ae51.rb, line 176
def start_write_to_file file_name
  @stop_writing_to_file = false
  @thread = Thread.new do
    clear_buffer
    m_prev = nil
    while @stop_writing_to_file != true
      begin
        m = Message.new read_message
      rescue RuntimeError
        retry
      end
      erase_flash if m.status == 64
      atn = Math.log( m.ref.to_f / m.sen1.to_f) * 100
      file = File.new file_name, 'a'
      print message = [Time.now, m.ref, m.sen1, atn, m.flow, m.pcb_temp, m.status, m.battery, sigma_ap( m, m_prev)].join(',') + "\n"
      file << message
      file.close
      m_prev = m
    end
  end
end
stop_write_to_file() click to toggle source
# File lib/micro_aeth-ae51.rb, line 197
def stop_write_to_file
  @stop_writing_to_file = true
  @thread.join 30
end
wait_for_acknowledge() click to toggle source
# File lib/micro_aeth-ae51.rb, line 142
def wait_for_acknowledge
  while read_message != "\u0006AE5X:A\u0014".force_encoding("ASCII-8BIT")
    nil
  end
end
write(instruction) click to toggle source

@m The message to be written

# File lib/micro_aeth-ae51.rb, line 112
def write instruction
  data = MicroAeth::Instruction::M + instruction
  len = data.length.chr
  crc = data ^ len
  @com.write (MicroAeth::Instruction::STX + len + data + crc + MicroAeth::Instruction::ETX).force_encoding("ASCII-8BIT")
end