# Serialport script hangs during sp.gets

**URL:** <https://www.ruby-forum.com/t/serialport-script-hangs-during-sp-gets/140608>\
**Category:** Ruby\
**Created:** [June 16, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/serialport-script-hangs-during-sp-gets/140608 "2008-06-16T19:22:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![japplewhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9c0ed/32.png) [@japplewhite](https://www.ruby-forum.com/u/japplewhite)\
**Post date:** [June 16, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/serialport-script-hangs-during-sp-gets/140608/1 "2008-06-16T19:22:35Z")

</div>

Hi ruby friends!

I’m using the ruby serialport library. The OS is windows.

I’m sending command strings over serial to an epson projector. The  
projector then interprets and executes the command and returns a string  
based on the results.

The serial port is receiving my commands when I print them, but the  
program hangs and crashes when I call gets, getc, or readline on the  
serial port. Example paste here:  
[http://rafb.net/p/GF6Ehz14.html](http://rafb.net/p/GF6Ehz14.html)

This isn’t a problem for commands like PWR ON and PWR OFF, since I can  
just run those blindly and assume success. For commands like PWR? or  
LAMP?, though, I need to see the return value.

The pastebin snippet isn’t very comprehensive, but I have tried MANY  
variations of gets, getc, readline, and with several different  
(relevant, not random) record separators.

Any ideas why it is hanging and not returning a string? FYI, it does  
return a string when I access the projector from Hyperterminal or  
telnet.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![japplewhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9c0ed/32.png) [@japplewhite](https://www.ruby-forum.com/u/japplewhite)\
**Post date:** [June 17, 2008, 10:10am UTC](https://www.ruby-forum.com/t/serialport-script-hangs-during-sp-gets/140608/2 "2008-06-17T10:10:42Z")

</div>

Hi

in my experience Ruby serialport library for Windows is in most cases  
not usable. I suppose the problem is Ruby’s implementation of threads  
(green threads) under Windows.

For serial communication under windows I now use a RS-232/422/485  
connections over Ethernet (i.e. Ethernet to serial) - so from windows  
(Ruby) perspective I use TCP/IP communication which works fine.

BR Philipp

Jordan Applewhite schrieb:

---

<div class="post-metadata">

**Author:** ![japplewhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9c0ed/32.png) [@japplewhite](https://www.ruby-forum.com/u/japplewhite)\
**Post date:** [June 17, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/serialport-script-hangs-during-sp-gets/140608/3 "2008-06-17T15:38:17Z")

</div>

> in my experience Ruby serialport library for Windows is in most cases  
> not usable. I suppose the problem is Ruby’s implementation of threads  
> (green threads) under Windows.

Someone was telling me the same thing yesterday in #ruby-lang. He was  
shocked that it was working at all with ruby 1.8.6. Nevertheless, it  
seems to be working today! All I had to do was set a seemingly  
arbitrary read\_timeout value. With the timeout, serialport.gets just  
returns whatever was in the input buffer.

> For serial communication under windows I now use a RS-232/422/485  
> connections over Ethernet (i.e. Ethernet to serial) - so from windows  
> (Ruby) perspective I use TCP/IP communication which works fine.

Still, I’m very interested in your solution. Are you describing having  
a cable with 8p8c on one end and dsub9 on the other, using ruby  
networking lib to send the appropriate data? Could you post a link to  
some example code, or perhaps specify the relevant ruby-doc pages?

Thanks! I appreciate your help!

---

<div class="post-metadata">

**Author:** ![japplewhite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9c0ed/32.png) [@japplewhite](https://www.ruby-forum.com/u/japplewhite)\
**Post date:** [September 15, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/serialport-script-hangs-during-sp-gets/140608/4 "2010-09-15T18:06:18Z")

</div>

> Someone was telling me the same thing yesterday in #ruby-lang. He was  
> shocked that it was working at all with ruby 1.8.6. Nevertheless, it  
> seems to be working today! All I had to do was set a seemingly  
> arbitrary read\_timeout value. With the timeout, serialport.gets just  
> returns whatever was in the input buffer.

Thanks so much for posting the response. You saved my day!
