Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:11:37+00:00 2026-05-23T05:11:37+00:00

I am trying to set up a simple ruby program with a two additional

  • 0

I am trying to set up a simple ruby program with a two additional threads.

  • One thread is to check the serial port for data and populate a variable if anything is found.

  • The second thread is is a call to ‘gets’, which when returned will stop the program.

  • The main thread simply ouputs the values stored by the first additional thread to the console (The program is a console based data logger).

My problem is that the second thread starts ok, as you can see from the “Press Enter…” note in the output.

D:\Documents and Settings\JMEDDING\My Documents\Ruby scripts>c-scope.rb -t
"Press 'Enter' to end process"
   511,   511,             485 |    |    |    |    |    +    |    |    |    |    | 536
   511,   511,             485 |    |    |    |    |    +    |    |    |    |    | 536
   512,   511,             485 |    |    |    |    |    XO   |    |    |    |    | 536
   512,   512,             485 |    |    |    |    |    |+   |    |    |    |    | 536

but any data returned from

input = gets

does not make it into the program. Instead, it can be seen on the console command line after the program is killed with ctrl-c.

I am running this in the ruby console on windows. This used to work properly before, but I re-arranged the code at one point and now it is not cooperating. I have copied the entire program below. I guess this is something very simple, but I’m totally stuck, so thanks for taking a look.

Jon

require 'serialport'

TEST = true if ARGV[0] == '--test' || ARGV[0] == '-t' 

# Edit the two settings below
port =  "COM6"   #"/dev/tty.usbserial-A600b1Q6"
baud = 9600

begin
  if RUBY_PLATFORM =~ /win32/ || RUBY_PLATFORM == "i386-mingw32"
    require 'Win32/Console/ANSI' 
  end

rescue LoadError
  raise "You must 'gem install win32console' to use color on Windows"
end

if TEST
    sp = nil
else
    sp = SerialPort.new port, baud, 8, 1, SerialPort::NONE
end


symbols = ["X", "O", "@", "#"]
vals = []       #max four values to plot
input = nil
len = 50

max = 0
min = 1200

if TEST
    vals = [511, 511]
end

def colorize(text, color_code)
  "\033[1;#{color_code}m#{text}\033[0m"
end

def red(text); colorize(text, "31"); end
def green(text); colorize(text, "32"); end
def blue(text); colorize(text, "34"); end
def color_the_symbols(text, symbols)
    color = 31      # "+" = red, then green, yellow, blue, pink
    chars = ["+"] + symbols 
    chars.each_with_index do |s,i|
        text.gsub!(s, colorize(s, "#{color + i}"))
    end
    text
end


def base_string (len, min, max, vals, symbols)

    s = "" 
    (0..len).each {|i| s += i%5 == 0 ? "|" : " "}
    vals.each_with_index do |val, i|
        pos = len * (val - min)/ (max - min)
        char = symbols.include?(s[pos] ) ? "+" : symbols[i]
        s[pos] = char
    end

    color_the_symbols s, symbols
end

#listen to STDIN for stop signal
Thread.new do
    p "Press 'Enter' to end process"
    input = gets    #pressing enter will stop the other loops
end

sleep(1)

#listening thread
Thread.new do
    while !input
        if TEST
            (0...vals.count).each {|i| vals[i] += rand(3) - 1}
        else
            c = ""
            word = ""

            until c == "\n"
                c = sp.getc
                word << c.to_s
                #p "Flushing buffer #{c}" 
            end
            #p word.chomp
            vals[0] = Integer(word.chomp)
        end
        sleep(0.5)
    end
end

while !input    #will be true until enter key is pressed
    while vals.count == 0
        #wait for firs data to show up
        sleep (0.1)
    end

    for i in (0...vals.count) do
        #validate min and max
        max = (vals[i] * 1.05).to_i if vals[i] > max
        min = (vals[i] * 0.95).to_i if vals[i] < min        
    end
    output = base_string(len, min, max, vals, symbols)
    output = "             #{red(min.to_s)} " + output + " #{red(max.to_s)}"
    color = 32
    vals.each_with_index do |val,i|
        output = "  #{colorize("%4d," % val, "#{color + i}")}"  + output
    end
    puts output
    sleep(1)
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:11:38+00:00Added an answer on May 23, 2026 at 5:11 am

    I think there was a error somewhere, but the notification was being obscured by the threading behavior. I made some unrelated changes and after some debugging, it started working again. Sorry I can’t be more specific.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple Ruby program that I will run from
I'm trying to get two Ruby apps to work from the same port. I
I am trying to run a simple ruby program on windows platform using 'tk'
I am trying to set up a simple transaction for my Linq-to-Sql actions against
I'm trying to set up a simple server side RSA encryption of a small
I'm trying to set together a simple TextBox with some watermark text in the
I'm trying to set up a simple SQLite database in Android, handling the schema
I'm trying to set up the simple syndication example from the Django docs, in
I'm using Spring 3, and trying to set up a simple web-app using annotations
I'm trying to set up a Spring JPA Hibernate simple example WAR for deployment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.