2 개의 추가 스레드로 간단한 루비 프로그램을 설정하려고합니다.Ruby는 프로그램 종료 후 콘솔 명령 행으로갑니다.
하나의 스레드는 직렬 포트에서 데이터를 확인하고 아무것도 발견되면 변수를 채 웁니다.
두 번째 스레드는 'gets'에 대한 호출이며, 반환되면 프로그램을 중지합니다.
- 메인 스레드는 첫 번째 추가 스레드에 의해 저장된 값을 콘솔에 단순히 출력합니다 (프로그램은 콘솔 기반 데이터 로거입니다).
내 문제는 출력에서 "Enter Enter ..."메모에서 볼 수 있듯이 두 번째 스레드가 정상적으로 시작된다는 것입니다.
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
'-d'를 사용하면 스레드가 결합 될 때 프로그램을 죽이는 대신 프로그램이 종료되는 것을 방지 할 수 있습니다. –
그게 좋은 생각입니다. 감사합니다 – CHsurfer