极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13837|回复: 3

Ruby 串口读写代码

[复制链接]
发表于 2013-7-9 00:23:29 | 显示全部楼层 |阅读模式

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

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





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2013-7-9 00:26:47 | 显示全部楼层
pcDuino的串口 与 Arduino的串口进行通讯的例子。

[pre lang="Ruby" line="1"]#!/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  [/code]

Arduino 重复发1234567890 的情况
回复 支持 反对

使用道具 举报

发表于 2013-7-9 11:04:03 | 显示全部楼层
现在Ruby是不是很火啊?
回复 支持 反对

使用道具 举报

 楼主| 发表于 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怎么知道呢?





回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 19:07 , Processed in 0.042663 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表