Ruby EventMachine与arduino的串口连接?

时间:2015-03-23 23:14:14

标签: ruby concurrency arduino eventmachine

我有一个Ruby应用程序通过USB连接到串行设备(Arduino,非firmata类)。

我正在探索将EventMachine用于非阻塞双向I / O的可能性,但找不到任何示例。 EvenMachine是否支持串行连接?我们目前正在使用我们自己的非阻塞IO框架,我宁愿不重新发明轮子。

目前,所有控制都是通过serialport gem通过Serial对象完成的。我相信它是Ruby中IO类的子类。

1 个答案:

答案 0 :(得分:0)

require 'eventmachine'

class Sender < EventMachine::Connection
  def receive_data(data)
    puts "Just got: #{data}"
  end

  def unbind
    puts "Disconnected! Did you unplug the device?"
    EM.stop
  end
end

EM.run do
  serial = SerialPort.new '/dev/ttyACM0'
  # We can do polling here to give the thread a chance to send outbound msgs.
  EventMachine::PeriodicTimer.new(2) { serial.puts "Hello!"  }
  EM.attach serial, Sender
end