ruby-nxt Progress

Posted by Tony Buser Fri, 04 Aug 2006 04:57:00 GMT

Following up on my ruby nxt datalogger from yesterday, I've got two way communication working now. Doesn't do much yet, just sends a play tone command to the nxt, sends a bluetooth message, or read a mailbox. It's a start.

Unlike the datalogger, this requires creating a serial port for the NXT device using Dev B service. (in OSX that's under Bluetooth Preferences -> Devices -> NXT -> Edit Serial Ports. Device Service: Dev B, Port type: RS-232, require pairing for security)

Also, if you want to send messages to a program running on the NXT, make sure you run this script first before starting your program on the NXT.

One problem I can't figure out is I created an NXT-G program and made it send a bluetooth message on connection 0 but I get no output on the serial port... however, if I go into the NXT and tell it to connect to my computer using my datalogger I get output there. The NXT shows my computer as being connected to both connection 0 and 1, which I thought wasn't possible. The documentation says the NXT can only be a Master or a Slave, not both. So who knows.

require "serialport"

@tty = SerialPort.new("/dev/tty.NXT-B", 57600, 8, 1, SerialPort::NONE)
@tty.flow_control = SerialPort::HARD

puts "bluetooth SPP connected"

if fork

  puts "input thread started"

  while (res = @tty.getc)
    puts "Response: %02x\t%s" % [res,res]
  end

else

  puts "output thread started"

  # play tone
  tone_cmd = [0x05,0x00,0x00,0x03,0xff,0x00,0x10,0x00,0x00]

  # write True to mailbox 0
  write_cmd = [0x06,0x00,0x00,0x09,0x00,0x02,0x01,0x00]

  # read mailbox 0 from slave?
  read_cmd = [0x05,0x00,0x00,0x13,0x0A,0x00,0x00]

  while true
    while (key = STDIN.gets.chomp) do
      if key == "w"
        puts "MessageWrite"
        write_cmd.each do |b|
          @tty.putc b
        end
      end
      if key == "r"
        puts "MessageRead"
        read_cmd.each do |b|
          @tty.putc b
        end
      end
      if key == "t"
        puts "PlayTone"
        tone_cmd.each do |b|
          @tty.putc b
        end
      end
    end
  end

end
Trackbacks

Use the following link to trackback from your own site:
http://www.juju.org/articles/trackback/403

Comments

Leave a response, Track co.mments

Comments