1

I have an existing Ruby on Rails Heroku application. I want to get the serial port data from the COM 1 port in a Windows computer using this application, but since Heroku is a cloud-based platform running on a Linux server, I am unable to get serial port data from the local machine where the Heroku application is running. If I run the standalone Ruby code on that Windows machine, then it works fine and I am able to get my desired data.

I am getting the following error

 Unable to open COM1

How do I solve this issue?

The part of code of my Rails application through which I am accessing serial port data is:

port_str = 'COM1'  #may be different for you
baud_rate = 2400
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)


i=sp.gets.chomp
puts i

This code is working fine if I run it on that local machine. 'serialport' gem is there on my gemfile.

Euro Micelli
  • 33,285
  • 8
  • 51
  • 70
Abhradip
  • 393
  • 5
  • 27
  • You can't do that. You'd need to have code running on the Windows machine and have it send the data to the Heroku application. – the Tin Man May 01 '17 at 20:02
  • Now I am running a local rails app on windows machine on localhost:3000 and through my heroku app I amtrying to get the data as json format using api call.Now I am facing the error econnrefused connect(2) for localhost port 3000 – Abhradip May 02 '17 at 04:58
  • Things to consider: do you think it's reasonable for someone to load an entire Ruby and Rails stack just so you can read the com port? Or would they consider it a possible back-door or attempt to steal information, which it could easily do at that point. Those are the concerns we have to have these days. – the Tin Man May 02 '17 at 18:06

1 Answers1

1

Rails is a web server technology that runs on a web server. It builds HTML pages that get sent to a client computer and are rendered by the browser.

When you run Rails locally you are mimicing a real web server - localhost is basically running a web server on your local machine. That is why you can cheat and use Ruby code in your Rails app that locally can access the port of your local machine, but once you run your Rails app on a real web server (like in Heroku) you cannot do this, so you have the wrong tool for the job you are trying to do.

Not only that but since Rails is a web technology you have a web application that runs inside the browser and you cannot easily access the port on a client machine from a web browser. More information on that is in "How to read serial port data from JavaScript".

The ONLY reason your Ruby code is able to access the port is because it is not running in the browser when you run on localhost but it is running inside the web server that gets fired up on localhost, so when the app runs on a real web server that Ruby code will try to access the port of the server not any client machine.

Community
  • 1
  • 1
rmcsharry
  • 5,363
  • 6
  • 65
  • 108