phpRPC + Ruby + Arduino = 远程控制
phpRPC + Ruby + Arduino = 遠程控制LED開關(?)
嗯,我知道這是個很無聊的Sample :P
關於phpRPC與Arduino請自行Google
觀看此demo之前請先安裝另外一篇所提到的serialport
與另外一個Gem
套件:phprpc
在這個範例中,我透過PHPRPC建立一個Server,並且透過RPC呼叫一些method去控制USB Serial Port
而在實現這個demo時,我們必須建立Server&Client兩端
底下是Source code:
複製內容到剪貼板
代碼:
# Server
#!/usr/bin/env ruby
require 'rubygems'
require 'serialport'
require 'phprpc'
class Cloud
def initialize
@@sp ||= SerialPort.new("/dev/cu.usb
serial-A600bMiv", {:baudrate => 9600})
end
def write(color)
@@sp.write("##{color}")
end
def close
@@sp.close
end
end
def randomColor
r = rand(256).to_s(16)
g = rand(256).to_s(16)
b = rand(256).to_s(16)
r = "0" + r if r.size == 1
g = "0" + g if g.size == 1
b = "0" + b if b.size == 1
color = r+b+g
cloud = Cloud.new
cloud.write(color)
"Color #{color} has been set."
end
def setColor(color)
cloud = Cloud.new
cloud.write(color)
"Color #{color} has been set."
end
def setRGBColor(r,g,b)
cloud = Cloud.new
cloud.write((r+g+b).to_s)
"Color #{(r+g+b)} has been set."
end
methods = %w"randomColor setColor setRGBColor"
server = PHPRPC::Server.new
server.debug = true
server.add(methods)
server.start
at_exit{
cloud.c
相关文档:
irb 是从命令行运行的
irb 的命令行选项(摘自 Porgramming Ruby 第二版)
-f
禁止读取~/.irbrc Suppress reading ~/.irbrc.
-m
数学模式(支持分数和矩阵) Math mode (fraction and matrix support is available).
-d
设置#DEBUG为true(同ruby -d一样) Set $DEBUG to true (same as ``ruby -d'').
-r lo ......
原文连接: http://hi.baidu.com/%B7%CF%B2%C5%CB%FB%B8%E7/blog/item/09c19411244152daf7039ec4.html
通过命令行查看ruby版本信息:
ruby -v
命令行运行程序:
方法1.
ruby -e 'print "hello ruby"'
-e 表示将后面的一行作为ruby程序
print 是ruby的一个内置函数
方法2.交互编译环境
irb (命令行输入后, ......
env setup
linux(ubuntu)下ruby开发环境搭建,包括一些常见问题解决
注意,本文只是我在搭建ruby学习环境时的一些笔记,因为是用gedit编辑的,所以格式化不是很好,另外,只是备忘而已。
2010.1.19
1. install ruby
$ tar xzf ruby-1.8.7-p248.tar.gz
$ mv ruby-1.8.7-p248 ruby187
$ cd ruby187/
$ ./configure
......
操作系统ubuntu,开发工具netbeans
vendor数据库表有字段service_category varchar(100)
<% form_for :vendor, @vendor, :url => sellers_path do |f| %>
<%= f.select(:service_category,
  ......