Better handling of boundary conditions

Trying with serialport.readpartial
This commit is contained in:
Aart van Halteren
2013-06-26 16:03:28 -04:00
parent 58eee12fb5
commit 099d90c9d8
5 changed files with 34 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ class ConfirmingSyncPatternState < StatePattern::State
sync_length = Synchronizer::SYNC_PATTERN.length sync_length = Synchronizer::SYNC_PATTERN.length
# confirm rest of sync pattern # confirm rest of sync pattern
while ((idx < sync_length) && bytes[idx] == Synchronizer::SYNC_PATTERN[idx]) do idx = idx+1 end while (idx < bytes.length && idx < sync_length && bytes[idx] == Synchronizer::SYNC_PATTERN[idx]) do idx = idx+1 end
if (idx == sync_length) if (idx == sync_length)
#p "Sync pattern confirmed" #p "Sync pattern confirmed"

View File

@@ -1,18 +1,35 @@
class InSyncState < StatePattern::State class InSyncState < StatePattern::State
END_OF_FRAME = "!\n" # END_OF_FRAME = "!\n"
def handle_byte_stream(bytes) def handle_byte_stream(bytes)
idx = 0 idx = 0
sync_pattern_length = Synchronizer::SYNC_PATTERN.length
#p "Sync length: #{sync_length}"
frame = "" frame = ""
while (idx < bytes.length && bytes[idx] != END_OF_FRAME[0]) 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
p "------ FRAME -----"
frame_lines = frame.split("\n") # did we reach the end of the frame?
p frame_lines # should call to higher level if new_frame_starts(bytes,idx,sync_pattern_length)
p "##################" p "------ FRAME -----"
return "" frame_lines = frame.split("\n")
p frame_lines # should call to higher level
p "##################"
return bytes[idx+sync_pattern_length..-1]
else
return nil
end
end end
private
def new_frame_starts(bytes,idx,sync_pattern_length)
return bytes[idx..idx+sync_pattern_length-1].eql?(Synchronizer::SYNC_PATTERN)
end
end end

View File

@@ -2,7 +2,7 @@ class SearchingForSyncState < StatePattern::State
def handle_byte_stream(bytes) def handle_byte_stream(bytes)
idx = 0; idx = 0;
# spool unwanted bytes # spool unwanted bytes
while (bytes[idx] != Synchronizer::SYNC_PATTERN[0]) do idx = idx+1 end while (idx < bytes.length && bytes[idx] != Synchronizer::SYNC_PATTERN[0]) do idx = idx+1 end
#p "Found pattern at idx = #{idx}" #p "Found pattern at idx = #{idx}"
transition_to(ConfirmingSyncPatternState) transition_to(ConfirmingSyncPatternState)

View File

@@ -117,5 +117,5 @@
0-1:96.1.0(3238303131303031323439333134383132) 0-1:96.1.0(3238303131303031323439333134383132)
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3) 0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
(00309.466) (00309.466)
0-1:24.4.0(1) 0-1:24.4.0(3)
! !

View File

@@ -12,7 +12,7 @@ connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details) ActiveRecord::Base.establish_connection(connection_details)
# # Open connection to serial port # # Open connection to serial port
# ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN) ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
# #
# # read until newline # # read until newline
# response = ser.readline("\r") # response = ser.readline("\r")
@@ -20,9 +20,12 @@ ActiveRecord::Base.establish_connection(connection_details)
# print "#{response}\n" # print "#{response}\n"
sync = Synchronizer.new sync = Synchronizer.new
lines = File.read("example_blurp.txt")[rand(500)..-1] #lines = File.read("example_blurp.txt")[rand(1500)..-1]
while (lines.length > 0) lines = ser.readpartial(4096) # read max 4k of data
p "There are #{lines.length} characters"
while (!lines.nil? && lines.length > 0)
lines = sync.handle_byte_stream(lines) lines = sync.handle_byte_stream(lines)
#p lines
end end
p sync #p sync