Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding my own serial port class and a new xbee class

  • Loading branch information...
commit 0526162e6d12ea1ed2b7497ebf3c38ff548fafd9 1 parent aaf6411
@tclem authored
Showing with 180 additions and 9 deletions.
  1. +83 −0 serial.rb
  2. +90 −0 xbee.rb
  3. +7 −9 xbee_modem_setup.rb
View
83 serial.rb
@@ -0,0 +1,83 @@
+require 'rubygems'
+require 'serialport'
+
+class Serial
+ attr_reader :buffer, :port
+
+ def self.list
+ Dir.new("/dev").entries.select{|e| e =~ /tty\./}
+ end
+
+ def initialize(opts={})
+ Dir.chdir("/dev")
+ usb = Dir.glob("tty.usbserial*")
+
+ @baud_rate = opts[:baud_rate] || 9600
+ @data_bits = 8
+ @stop_bits = 1
+ @parity = SerialPort::NONE
+ @port_str = opts[:port] || "/dev/#{usb}"
+ puts "using #{usb} baud_rate: #{@baud_rate}"
+ @port = SerialPort.new(@port_str, @baud_rate, @data_bits, @stop_bits, @parity)
+ @port.read_timeout = 100
+ @buffer = "" # ? v. bytes
+ @buffer_until = convert_to_byte( opts[:buffer_until]) if opts[:buffer_until]
+ end
+
+ def read! &block
+ raise "buffer_until is not set, this will run forever!" unless @buffer_until
+ s = @port.getc
+ @buffer << s.chr if s
+ if (s == @buffer_until)
+ if block_given?
+ yield
+ else
+ @buffer
+ end
+ else
+ false
+ end
+ end
+
+ def buffer_until=(s)
+ @buffer_until = convert_to_byte(s)
+ end
+
+ def buffer_until
+ @buffer_until.chr
+ end
+
+ def clear_buffer!
+ @buffer = "" # ? v. bytes
+ end
+
+ def read_string_until(s)
+ s = convert_to_byte(s)
+ result = ""
+
+ stop = false
+ count = 0
+ @buffer.each_byte do |b|
+ unless stop
+ result << b.chr
+ count = count + 1
+ stop = true if b == s
+ end
+ end
+ @buffer.slice! 0..(count-1)
+ result
+ end
+
+ def convert_to_byte(s)
+ raise ArgumentError, "can only use a string or integer to buffer" unless s.is_a?(String) || s.is_a?(Integer)
+ if s.is_a? String
+ s[0]
+ else
+ s
+ end
+ end
+
+ def method_missing(method, *args, &block)
+ @port.send(method, *args)
+ end
+end
View
90 xbee.rb
@@ -0,0 +1,90 @@
+require 'serial'
+
+class Xbee
+
+ def initialize(opts={})
+ @s = Serial.new({:buffer_until => "\r"})
+ puts "connecting to xbee radio and configuring AT mode..."
+ @s.write("+++")
+ r = read_response
+ puts "xbee says: #{r}"
+ setup
+ end
+
+ def setup
+ @commands = [
+ {:cmd => "ATVR", :desc => "\t\tfirmware version", :value => nil},
+ {:cmd => "ATCH", :desc => "\t\tchannel", :value => "F"},
+ {:cmd => "ATID", :desc => "\t\tpan id", :value => "20F"},
+ {:cmd => "ATSL", :desc => "serial# lo", :value => nil},
+ {:cmd => "ATSH", :desc => "\tserial# hi", :value => nil},
+ {:cmd => "ATDL", :desc => "\t\tdest address lo", :value => "FFFF"},
+ {:cmd => "ATDH", :desc => "\t\tdest address hi", :value => nil},
+ {:cmd => "ATNI", :desc => "\t\tnetwork id", :value => nil},
+ ]
+ end
+
+ def show_settings
+ puts "Current settings on this xbee radio:"
+ @commands.each do |c|
+ r = write_cmd(c[:cmd])
+ puts "#{c[:cmd]}: 0x#{r} (#{r.to_i(16)}) #{c[:desc]}"
+ end
+ end
+
+ def write_settings
+ show_settings
+ puts
+ puts "Writing new settings to this xbee radio..."
+ puts
+
+ @commands.each do |c|
+ if c[:value].nil?
+ next
+ end
+ #puts "going to write this: #{c[:cmd]}#{c[:value]}\tATWR\t#{c[:cmd]}"
+ write_cmd("#{c[:cmd]}#{c[:value]}")
+ write_cmd("ATWR")
+ r = write_cmd(c[:cmd])
+ puts "#{c[:cmd]}: 0x#{r} (#{r.to_i(16)}) #{c[:desc]}"
+ end
+
+ puts "Successfully configure xbee with new settings"
+
+ end
+
+ def reset_settings
+ write_cmd("ATRE")
+ write_cmd("ATWR")
+ puts "Settings have been restored to factory defaults."
+ puts
+ show_settings
+ end
+
+ # Basic read/write operations
+ def write_cmd(cmd)
+ @s.write("#{cmd}\r")
+ r = read_response
+ r.slice!("\r") # get rid of last <CR>
+ r
+ end
+
+ def read_response
+ begin
+ r = @s.read!
+ finished = true if r
+ end until finished
+ @s.clear_buffer!
+ r
+ end
+
+ def method_missing(method, *args, &block)
+ @s.send(method, *args)
+ end
+
+end
+
+x = Xbee.new
+x.show_settings
+x.close
+
View
16 xbee_modem_setup.rb
@@ -96,7 +96,6 @@
-
commands = [
{ :ATVR => ""}, # firmware version
{ :ATCH => CH}, # channel
@@ -109,10 +108,8 @@
]
-
-
require 'rubygems'
-Kernel::require 'serialport'
+require 'serialport'
class Xbee
@@ -157,7 +154,7 @@ def setup
# Write each command
def cmd(cmd)
- @port.write(cmd + "\r")
+ @port.write(cmd + " \r")
verify(cmd)
end
@@ -185,10 +182,11 @@ def print_result(cmd, result)
commands.each do |command|
command.each do |k,v|
- p.cmd("#{k.to_s}#{v}, \r")
- if v != ""
- p.cmd("#{k.to_s},WR \r")
- end
+ # puts "#{k.to_s}#{v}"
+ p.cmd("#{k.to_s}#{v}")
+ # if v != ""
+ # p.cmd("#{k.to_s},WR \r")
+ # end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.