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

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

View File

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