Continuous loop reading data

This commit is contained in:
Aart van Halteren
2013-07-16 21:52:05 +02:00
parent 96071fb9b5
commit 632c59580b
2 changed files with 26 additions and 20 deletions

View File

@@ -13,22 +13,20 @@ class InSyncState < StatePattern::State
while (idx+sync_pattern_length < bytes.length && !new_frame_starts(bytes,idx,sync_pattern_length)) do
frame = frame + bytes[idx]
idx = idx +1
end
# did we reach the end of the frame?
if new_frame_starts(bytes,idx,sync_pattern_length)
p "------ FRAME -----"
frame_lines = frame.split("\n")
p frame_lines
p "##################"
p "------ FRAME -----"
# p frame_lines
# p "##################"
reading = handle_frame(frame_lines)
p reading
return bytes[idx+sync_pattern_length..-1]
else
return nil
return bytes
end
end
private

View File

@@ -4,6 +4,8 @@ require "active_record"
require "serialport"
require "state_pattern"
MAX_BYTES = 100
project_root = File.dirname(File.absolute_path(__FILE__))
Dir.glob(project_root + "/app/models/*.rb").each{|f| require f}
Dir.glob(project_root + "/app/helpers/*.rb").each{|f| require f}
@@ -11,28 +13,34 @@ Dir.glob(project_root + "/app/helpers/*.rb").each{|f| require f}
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details)
if __FILE__ == $0
def open_device
begin
# Open connection to serial port
ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
lines = ""
for i in 0..21
lines = lines+ser.readline("\n").gsub(/\r/, '')
end
io_device = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
rescue
p "Serialport Error - reverting to 'example_blurp.txt'"
lines = File.read("example_blurp.txt")[rand(1500)..-1]
p "There are #{lines.length} characters"
p lines
io_device = File.open("example_blurp.txt")
end
return io_device
end
def read_from(source)
source.read(MAX_BYTES).gsub(/\r/, '') rescue ""
end
if __FILE__ == $0
source = open_device
buffer = []
buffer = read_from(source)
#
# Process the received lines
#
sync = Synchronizer.new
while (!lines.nil? && lines.length > 0)
lines = sync.handle_byte_stream(lines)
#p lines
while (buffer && buffer.length > 0)
p "BUFFER: #{buffer}."
buffer = sync.handle_byte_stream(buffer)
buffer = buffer + read_from(source)
end
end
#p sync