Question

I have created a Ruby script that gets serial data and if it starts with certain characters, prints string (SMS), inserts string into a file, parses string, then inserts parsed data into a MySql database. If I put a sample test string in place of the serial data, everything works perfectly including the loop (include a ten second delay after database insert in the test) When I use the serial data, print and file append works fine however nothing is inserted into the database. The database table is created OK. My production code follows:

require "rubygems"  
require "serialport"  
require "data_mapper"

#params for serial port  
 port_str = "/dev/ttyACM0" 
 baud_rate = 19200  
 data_bits = 8  
 stop_bits = 1  
 parity = SerialPort::NONE

#Connect to mysql database
 DataMapper.setup(:default, {
  :adapter  => 'mysql',
  :host     => 'localhost',
  :database => 'Kourarau',
  :username => 'root',
  :password => 'xxxxxxx'
})

class StnA
    include DataMapper::Resource

    property :id,       Serial, :key => true
    property :datetime, DateTime
    property :stna_kW,  Integer
    property :total_kW,     Integer
    property :stna_kVAr,    Integer
    property :total_kVAr,   Integer
    property :tailrace, Integer
    property :lo_pond,  Integer
    property :up_pond,  Integer
    property :mode,     String
    property :pen_a_press,  Integer
    property :pen_b_press,  Integer
    property :volt_A,   Integer
    property :stat_A,   String
    property :surge,    Integer
    property :needle_a1,    Integer
    property :needle_b1,    Integer
    property :needle_a2,    Integer
    property :needle_b2,    Integer
end

DataMapper.finalize
DataMapper.auto_upgrade!

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)  

 loop do
   sp_char = sp.gets
   if sp_char.start_with?("Time")
        printf("%s", sp_char)
    open('statusA.current', 'a') { |f|
      f.puts(sp_char)
    }
    status_ar = sp_char.split (',')
        time_split = status_ar[0].split(' ') #required to split time and Station A information
        stna_kW = time_split[1].tr('^0-9', '')
        stna_kVAr = status_ar[1].tr('^0-9-', '')
        total_kW = status_ar[2].tr('^0-9', '')
        total_kVAr = status_ar[3].tr('^0-9-', '')
        tailrace = status_ar[4].tr('^0-9-', '')
        lo_pond = status_ar[5].tr('^0-9-', '')
        up_pond = status_ar[6].tr('^0-9-', '')
        mode = status_ar[7][4,1]
        pen_a_press = status_ar[8].tr('^0-9', '')
        pen_b_press = status_ar[9].tr('^0-9', '')
        volt_A = status_ar[10].tr('^0-9', '')
        stat_A = status_ar[11][6,2]
        surge = status_ar[11].tr('^0-9-', '')
        needle_split = status_ar[12].split('%')
        needle_a1 = needle_split[0].slice!(2..4)
        needle_b1 = needle_split[1].slice!(2..4)
        needle_a2 = needle_split[2].slice!(2..4)
        needle_b2 = needle_split[3].slice!(2..4)

     StnA.create(
        :datetime => DateTime.now,
        :total_kW => total_kW,
        :stna_kW => stna_kW,
        :stna_kVAr => stna_kVAr,
        :total_kVAr => total_kVAr,
        :tailrace => tailrace,
        :lo_pond => lo_pond,
        :up_pond => up_pond,
        :mode => mode,
        :pen_a_press => pen_a_press,
        :pen_b_press => pen_b_press,
        :volt_A => volt_A,
        :stat_A => stat_A,
        :surge => surge,
        :needle_a1 => needle_a1,
        :needle_b1 => needle_b1,
        :needle_a2 => needle_a2,
        :needle_b2 => needle_b2
     )
  end
# may require a break in the loop later 
end
Was it helpful?

Solution

Thanks to Zoltan for putting me on the right track. I added .chomp to sp.gets to remove the new line that sp.gets does. This allowed the database insert to occur.

Having done that, I still require the new line for the printf and file insert so will need to alter some code. Not expecting too many issues with this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top