2011-06-13 33 views
0

2つのスレッドを追加して簡単なRubyプログラムをセットアップしようとしています。Rubyはプログラムが終了した後にコンソールコマンドラインに入ります

  • 1つのスレッドは、シリアルポートでデータを確認し、見つかった場合は変数に値を設定します。

  • 2つ目のスレッドは 'gets'の呼び出しであり、返されるとプログラムが停止します。

  • メインスレッドは、最初の追加スレッドによって格納された値をコンソールに単純に出力します(プログラムはコンソールベースのデータロガーです)。

私の問題は、出力の「Enter」キーを押してください。メモからわかるように、2番目のスレッドが正常に起動することです。

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 

が、どのデータが=

を取得し、入力がプログラムにそれをしない

から返されました。その代わりに、プログラムがctrl-cで強制終了された後、コンソールのコマンドラインで見ることができます。

私はこれをWindowsのルビーコンソールで実行しています。これはこれまで正しく動作していましたが、コードをある時点で再配置していますが、今は協調していません。私は以下のプログラム全体をコピーしました。私はこれがとてもシンプルなものだと思うが、私は完全に立ち往生しているので、見てくれてありがとう。

ジョン

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 

答えて

0

私はエラーがどこかにあったと思うが、通知はスレッドの動作によって隠されていました。無関係な変更を加え、デバッグした後、再び動作するようになりました。申し訳ありませんが、私はより具体的にすることはできません。

+0

'-d'を使うと、スレッドが参加したときにそれを強制終了するのではなく、スレッドが関わっているときに例外がすぐにプログラムを終了させるようにすることができます。 –

+0

それは良い考えです。ありがとう – CHsurfer

0

あなたは、そのスレッドは、プログラムが終了する前にその作業を終了したい場合は

thread = Thread.new 
    # blah 
end 

thread.join 

を持っている必要があります。

+0

実際には、そのスレッドがプログラムを停止したいが、入力がシステムに入力されていない。しかし、答えをありがとう。 – CHsurfer

関連する問題