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
# 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)
#p "Sync pattern confirmed"

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

View File

@@ -2,7 +2,7 @@ class SearchingForSyncState < StatePattern::State
def handle_byte_stream(bytes)
idx = 0;
# 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}"
transition_to(ConfirmingSyncPatternState)