hparra / ruby-serialport

ruby-serialport is a Ruby library that provides a class for using RS-232 serial ports

Home Page:http://rubygems.org/gems/serialport

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Threading & serial Reads

dencur opened this issue · comments

When trying to read from the serial port in a threaded application the read never responds. Removing the read, the write still functions as expected.

Example:

 Thread.new do
   while(1)
     if(session=server.accept)
       puts "log: Connection from #{session.peeraddr[2]} at #{session.peeraddr[3]}"
       @buffer.put(session)   
     end
   end
 end

 while(1)
   if(@buffer.empty? == false)
     begin
       sp = SerialPort.new(@dev[0].port, @dev[0].baud, 8, 1, SerialPort::NONE)     
       sp.puts(incData.data)
       @cap1data = sp.read
       sp.close
     rescue => e
       puts "IO Error"
       puts e.message
       puts e.backtrace
     end
   end
 end

I've been successful doing this. I use numerous ports in a very threaded application.

  • What version of ruby are you using? 1.8 still used green threads and Windows had a serious problem with blocked I/O
  • Please check the documentation on read(). I rarely use this function, preferring others so I don't know if you're using it as intended.

Thanks for the reply hparra.

  • Mac OSX 10.6.4
  • ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-darwin10]

Could you include your code for reading and writing in a threaded application? I am just wondering what your using other than read. Thanks.

I had the exact same issue on OSX 10.6.4. Setting the read-timeout to -1 fixed it for me. In fact, setting it to any value would fix this problem.

Yes, depending on what you're doing setting read_timeout = -1 is the right behavior, but even if you do that, the thread I/O business I mentioned above still applies.