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

500 server error in development env when the serial port doesn't exist on server #48

Open JakeTheSnake3p0 opened 10 years ago

JakeTheSnake3p0 commented 10 years ago

Normally I use SerialPort on a server which has a virtual COM port set up to communicate over the LAN with another computer's point of sale display pole. This is set up as COM3 on the server. Everything works as expected...in production mode only. Having scoured the web for the error that was popping up, I came across a solution where I have to set config.cache_classes = true in development mode. Here's a sample of the code I'm using; keep in mind that regardless of whether or not I have a begin/rescue block, WEBrick still servers a 500 server error. I have to exit the controller method prematurely and return head :no_content before anything happens with SerialPort if I want to avoid this problem in development mode:

POST method - Ajax request from browser app has header X-Message which gets written to COM port (and thus sent back to display pole)
def update_display_pole
    begin
        SerialPort.open(Rails.configuration.serial_port,
                Rails.configuration.serial_port_baud_rate,
                Rails.configuration.serial_port_bits,
                Rails.configuration.serial_port_parity, 
                Rails.configuration.serial_port_flow) do |s|

            s.write request.headers['X-Message']

        end
        head :no_content
    rescue
        head :internal_server_error, 'X-Reason'=> "Cannot communicate with COM port"
    end
end

Now in production mode if the COM port doesn't exist, the begin/rescue block works as expected. However in development mode I don't get any errors other than "500 server error" in my logs.

hparra commented 10 years ago

I'm also stumped. It's been a long times since I've worked with rails. Could it be some be some threading issue? See config.threadsafe!