hparra / ruby-serialport

ruby-serialport is a Ruby library that provides a class for using RS-232 serial ports
http://rubygems.org/gems/serialport
GNU General Public License v2.0
246 stars 58 forks source link

Threading & serial Reads #22

Closed dencur closed 14 years ago

dencur commented 14 years ago

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
hparra commented 14 years ago

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

dencur commented 14 years ago

Thanks for the reply hparra.

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

zeeed commented 14 years ago

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.

hparra commented 14 years ago

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.