From 5e1712afacdd1384440856b1be1efa4acf827ffd Mon Sep 17 00:00:00 2001 From: Aart van Halteren Date: Wed, 26 Jun 2013 22:39:51 +0200 Subject: [PATCH] Remove '\r' from serial datastream --- app/helpers/Synchronizer.rb | 2 +- smartmeter.rb | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/helpers/Synchronizer.rb b/app/helpers/Synchronizer.rb index cf164d5..a171021 100644 --- a/app/helpers/Synchronizer.rb +++ b/app/helpers/Synchronizer.rb @@ -5,4 +5,4 @@ class Synchronizer set_initial_state SearchingForSyncState -end \ No newline at end of file +end diff --git a/smartmeter.rb b/smartmeter.rb index 188413c..df12350 100644 --- a/smartmeter.rb +++ b/smartmeter.rb @@ -13,7 +13,11 @@ ActiveRecord::Base.establish_connection(connection_details) # # Open connection to serial port ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN) -# +lines = "" +for i in 0..22 + lines = lines+ser.readline("\n").gsub(/\r/, '') +end + # # read until newline # response = ser.readline("\r") # response.chomp! @@ -21,8 +25,8 @@ ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN) sync = Synchronizer.new #lines = File.read("example_blurp.txt")[rand(1500)..-1] -lines = ser.readpartial(4096) # read max 4k of data p "There are #{lines.length} characters" +p lines while (!lines.nil? && lines.length > 0) lines = sync.handle_byte_stream(lines) #p lines