# Serialport won't thread (new to ruby)

**URL:** <https://www.ruby-forum.com/t/serialport-wont-thread-new-to-ruby/190229>\
**Category:** Ruby\
**Created:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/serialport-wont-thread-new-to-ruby/190229 "2010-06-17T16:22:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![secesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f04885/32.png) [@secesh](https://www.ruby-forum.com/u/secesh)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/serialport-wont-thread-new-to-ruby/190229/1 "2010-06-17T16:22:27Z")

</div>

def readSerial  
while 1  
puts “reading line from TTY\n”  
TTY.read\_timeout=1  
lineTTY = TTY.read.unpack(“H\*”)  
lineTTY.each { |l|  
if l.sub!(/^0262([0-9A-F]{12})06/i, “”)  
puts “Got ACK on: #{$1}\n”  
#This message is simply an ACK from the PLM of a  
message I sent  
#to the PLM. because I incorporate error-detection  
elsewhere,  
#I assume I send good data to the PLM, and only get  
ACKs.  
#NAKs from the PLM are “15” instead of “06”  
redo  
elsif l.sub!(/^0250([0-9A-F]{6})([0-9A-F]{6})([0-9A-F]{2})  
([0-9A-F]{2})([0-9A-F]{2})/i, “”)  
#I wish I could condense the repition out of the  
above, but doing so makes me loose variables below.  
puts “A SD was sent from #{$1} to #{$2}. Flag was:  
#{$3}. Commands were: #{$4} #{$5}\n”  
redo  
elsif l.length \> 0  
print "unknown: " + l + “\n”  
end  
}  
sleep(1)  
end  
end

def watchGets  
while 1  
line=gets.chomp  
exit if line==“q”  
end  
end

* * *

# Given the above, this works:

readSerial

# But the problem is that rather than just calling that, I want to

thread.

# But the below hangs (I get one line of “Reading line from TTY” –

but only one).

# and while threadTTY hangs, threadGets works fine in the background.

threadGets = Thread.new{watchGets}  
threadTTY = Thread.new{readSerial}  
threadGets.join  
threadTTY.join

# Thanks!

---

<div class="post-metadata">

**Author:** ![secesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f04885/32.png) [@secesh](https://www.ruby-forum.com/u/secesh)\
**Post date:** [June 17, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/serialport-wont-thread-new-to-ruby/190229/2 "2010-06-17T22:18:03Z")

</div>

> # Given the above, this works:
> 
> readSerial
> 
> # But the problem is that rather than just calling that, I want to
> 
> thread.
> 
> # But the below hangs (I get one line of “Reading line from TTY” –
> 
> but only one).
> 
> # and while threadTTY hangs, threadGets works fine in the background.
> 
> threadGets = Thread.new{watchGets}  
> threadTTY = Thread.new{readSerial}  
> threadGets.join  
> threadTTY.join

What is TTY?  
Also are you on windows? Which version of ruby?  
-r
