Search This Blog

May 16, 2012

Ruby access UART under Windows7


1. Download DevKit-tdm-32-4.5.2-20111229-1559-sfx and rubyinstaller-1.9.3-p125.exe
http://rubyinstaller.org/downloads

Install Ruby first.

2. Install DevKit
Follow http://github.com/oneclick/rubyinstaller/wiki/Development-Kit
a. $cd
b. $ruby dk.rb init.
c. $ruby dk.rb review
d. $ruby dk.rb install

3. Test Installation
Confirm your Ruby environment is correctly using the DevKit by running
"$gem install rdiscount --platform=ruby". RDiscount should install correctly
and you should see "Temporarily enhancing PATH to include DevKit..." in the screen
messages. Next run $ruby -rubygems -e "require 'rdiscount'; puts RDiscount.new('**Hello RubyInstaller**').to_html"
to confirm that the rdiscount gem is working.

4. Install serial port gem
Follow https://github.com/hparra/ruby-serialport
$ gem sources -a http://gemcutter.org
$ gem install serialport

5. Get serialport document
C:\Ruby193\lib\ruby\gems\1.9.1\doc\serialport-1.0.4\rdoc

6. Test device board uart function   
Prepare one MCU card, download firmware with UART example code.
it will send back characters come from PC uart console. Use Putty
test uart, it works.

7. Write ruby uart test code.
require "serialport"

port_str = 3 
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)

#just read forever
sp.printf("a")
while true do
  printf("%c", sp.getc)
end
sp.close      

8. Test ruby uart function
$ ruby uart.rb
it works.
We are done.
Post a Comment