szpapas 发表于 2013-7-9 00:23:29

Ruby 串口读写代码


这个是用在pcDuino上的代码(ubuntu 环境下的):

require 'serialport'
sp = SerialPort.new "/dev/ttyS1", 115200
sp.write "m1 on"
puts sp.read





szpapas 发表于 2013-7-9 00:26:47

pcDuino的串口 与 Arduino的串口进行通讯的例子。

#!/usr/bin/ruby
require 'serialport'
sp = SerialPort.new "/dev/ttyS1", 115200

open("/dev/tty", "r+") { |tty|
tty.sync = true
Thread.new {
    tty.puts "demo 123456"
    ss = ''
    while true do
      ss = ss + sprintf("%02X ", sp.getc)
      #tty.printf("%c", sp.getc)
      #ss = sp.read
      #puts ss
    end
    tty.puts "<< recv : #{ss}"
    #tty.printf("<< recv : #{ss}")
}
while (l = tty.gets) do
    l = l.chomp
    puts ">> #{Time.now.strftime('%D %T')}: #{l}"
    if l == "exit"
      exit
    elsif l == "on"
      sp.write("m1 on")
    elsif l == "off"
      sp.write("m1 off")   
    else
      sp.write(l.sub("\n", "\r"))
    end
   
end
}

sp.close

Arduino 重复发1234567890 的情况

likunyang 发表于 2013-7-9 11:04:03

现在Ruby是不是很火啊?

szpapas 发表于 2013-7-10 09:11:32

本帖最后由 szpapas 于 2013-7-10 16:02 编辑

好像一般。我有一个朋友喜欢python。我说ruby可以做什么,他立即就说python也可以呀。就是一个脚本语言,习惯了,你做什么事情,都喜欢拿它来对付。

Mac OS X 自带的脚本语言。Windows, Ubuntu下面需要自己安装。

我用Ruby大约也有7,8年了。感觉就是简单,高效。

用ruby调试串口,网络,比Windows方便,肯定有人会反驳我。但你不用Ruby怎么知道呢?





页: [1]
查看完整版本: Ruby 串口读写代码