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

exception when read() method goes in timeout #15

Open eugenio opened 14 years ago

eugenio commented 14 years ago

Why there isn't builtin exception when read() goes in timeout? I tried several approaches but i couldn't manage to make ruby trow an exception when the read() method goes in timeout. Also there is no indication in the documentation on how to handle errors when reading or writing to a serial port. I'm running Ubuntu lucid (10.04) with ruby 1.8 but I also have 1.9.1 and all the gems are at the latest version available including serialport.

hparra commented 14 years ago

I don't think it's designed to throw an exception. Timeout allows read() to stop blocking and continue executing code.

eugenio commented 14 years ago

Thanks for answering my question. However I'm in the need of that particular feature (the exception from the timed out read() ) because the device that the program is designed to interact with require a initialization string to be sent and the answer to that particular string to be read from the program before rest of the program is allowed to proceed. Would it be possible to modify the read function to include an exception to be thrown if the device doesn't answer in a certain time? Could you do it or show me a prototype of the function to modify the library with? or can you give me some lead any example of code that could be suitable for the purpose? Thanks in advance for anything you can do and congrats for the great work you're doing with the library.

solyarisoftware commented 10 years ago

Hi Hector! In facts I agree with the point Eugenio explained. In my case I'd use the serial port (windows/linux) to print on a thermal printer.

To see if the printer if powered ON (and online) I can ask a status to the device.

The problem arise because, even if I set serial port timeouts, without an exception I am not able to understand if the printer is alive or powered off.

I tryied using Ruby Timeout (on Windows OS), but unfortunately the code below do not run as expected (id the printer is off, the read hang and I do not have Timeout::Error ) .. ?

Any idea to solve the point? Thank you

begin Timeout.timeout(2) do puts printer.read end rescue Timeout::Error puts "SerialPort timeout." rescue => e puts "[open] Serialport ERROR: Failed to open: #{ e.inspect }" end

solyarisoftware commented 10 years ago

BTW, I solved, see: http://stackoverflow.com/questions/23489812/ruby-serialport-timeout-exception/23521862#23521862