Continuous loop reading data
This commit is contained in:
@@ -12,23 +12,21 @@ 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
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
end
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user