Files
smartmeter/app/helpers/ConfirmingSyncPatternState.rb
Aart van Halteren 099d90c9d8 Better handling of boundary conditions
Trying with serialport.readpartial
2013-06-26 16:03:28 -04:00

21 lines
636 B
Ruby

class ConfirmingSyncPatternState < StatePattern::State
# Assumes that bytes[0] == Synchronizer::SYNC_PATTERN[0]
def handle_byte_stream(bytes)
idx = 0;
sync_length = Synchronizer::SYNC_PATTERN.length
# confirm rest of sync pattern
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"
transition_to(InSyncState)
else
#p "Back to SearchingForSync state. idx = #{idx}."
transition_to(SearchingForSyncState)
end
# return the rest
return bytes[idx+1..-1]
end
end