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

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