Better handling of boundary conditions
Trying with serialport.readpartial
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -117,5 +117,5 @@
|
||||
0-1:96.1.0(3238303131303031323439333134383132)
|
||||
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
|
||||
(00309.466)
|
||||
0-1:24.4.0(1)
|
||||
0-1:24.4.0(3)
|
||||
!
|
||||
|
||||
@@ -12,7 +12,7 @@ connection_details = YAML::load(File.open('config/database.yml'))
|
||||
ActiveRecord::Base.establish_connection(connection_details)
|
||||
|
||||
# # 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
|
||||
# response = ser.readline("\r")
|
||||
@@ -20,9 +20,12 @@ ActiveRecord::Base.establish_connection(connection_details)
|
||||
# print "#{response}\n"
|
||||
|
||||
sync = Synchronizer.new
|
||||
lines = File.read("example_blurp.txt")[rand(500)..-1]
|
||||
while (lines.length > 0)
|
||||
#lines = File.read("example_blurp.txt")[rand(1500)..-1]
|
||||
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)
|
||||
#p lines
|
||||
end
|
||||
|
||||
p sync
|
||||
#p sync
|
||||
|
||||
Reference in New Issue
Block a user