I am trying to use serial port by ruby on rails. I use require 'serialport'. I installed gem serialport. I have got this error:
cannot load such file -- serialport
and this line is highlighted:
require 'serialport'
This is my script:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def hello
render text: "hello1, world!"
end
def cmd
require 'serialport'
@result = params[:parametr]
@dioda = params[:dioda]
#params for serial port
port_str = "/dev/ttyACM0" #may be different for you
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
case @result
when "1"
render html: "<strong>Led_on</strong>".html_safe
when "0"
render html: "<strong>Led_off</strong>".html_safe
when "sos"
render html: "<strong>Led_sos</strong>".html_safe
end
sleep 4
puts sp.readlines
end
end
如果您使用的是Rails,则需要将gem 'serialport'
放入Gemfile
,然后运行bundle install
。
Problem solved by changing configuration of passenger which used wrong ruby interpreter. When I checked as a user in command line everything seemed to be ok, but using Apache/Passenger was an error, everything because different ruby interpreters used in these two situations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.