Skip to content

Instantly share code, notes, and snippets.

@ymrl
Created October 20, 2011 05:35
Show Gist options
  • Save ymrl/1300500 to your computer and use it in GitHub Desktop.
Save ymrl/1300500 to your computer and use it in GitHub Desktop.
シリアルを読んでWebsocketに投げる何か(コンソールでシリアルの代わりもできる)
#coding:UTF-8
require 'rubygems'
require 'em-websocket'
require 'serialport'
EM::run do
@channel = EM::Channel.new
@serialport = SerialPort.new('/dev/tty.usbserial-********',9600,8,1,0)
# tty.usbserial-******** は適宜書き換える
EM::WebSocket.start(:host => "0.0.0.0", :port => 10080) do |ws|
ws.onopen do
sid = @channel.subscribe{|mes| ws.send mes}
ws.onclose do
@channel.unsubscribe(sid)
end
ws.onmessage do |mes|
@channel.push mes
puts mes
end
end
end
EM::defer do
loop do
mes = @serialport.gets.to_s.gsub(/[\r\n]/,'')
next if !mes or mes.size < 1
@channel.push mes
puts mes
end
end
# コンソールの入力をWebsocketに投げる
EM::defer do
loop do
mes = gets.to_s.gsub(/[\r\n]/,'')
next if !mes or mes.size < 1
@channel.push mes
puts mes
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment