Loading ...
Sorry, an error occurred while loading the content.

Has anyone used XBee with Intellibrain?

Expand Messages
  • Don
    I am having difficulty using XBee with the Intellibrain 2. Probably it is something I am doing wrong. I wonder if anyone else has experience with this RF
    Message 1 of 1 , Mar 16, 2007
    • 0 Attachment
      I am having difficulty using XBee with the Intellibrain 2. Probably
      it is something I am doing wrong. I wonder if anyone else has
      experience with this RF modem on the Intellibrain?

      At this point I cannot get a proper acknowledgement back from the XBee
      when sending it a command preamble ("+++") on the COM port. It is
      supposed to respond with OK and a CR character. Instead, it returns
      either "xZy" or a garbage character followed by "xZy". Once in a
      while it repeats the sequence.

      Well, if anyone is interested, here is the code. It is a modfication
      of code that was written for the Parallax Javelin by Jon Keinath. It
      includes a few debugging statements that write to the LCD display
      object that is passed to the constructor. The constructor never gets
      past the first call to the waitOK() method, which is called by the
      enterCMD() method, which is called by the setGuardTime() method.

      The datasheet for the XBee is here:
      http://www.maxstream.net/products/xbee/manual_xb_oem-rf-modules_802.15.4.pdf



      import com.ridgesoft.intellibrain.IntelliBrain;
      import com.ridgesoft.intellibrain.IntelliBrainDigitalIO;
      import com.ridgesoft.io.Display;
      import javax.comm.SerialPort;
      import com.ridgesoft.io.*;
      import java.io.*;
      import javax.comm.UnsupportedCommOperationException;

      /**
      * This class provides an interface to the XBee
      * Wireless communication module.
      *
      * @author Jon Keinath, www.jonkeinath.com, (jonkeinath@...)
      * modified for the Intellibrain 2 by
      * Don French March 15, 2007.
      */
      public class XBee
      {

      //------------------------------------CONSTANTS----------------------------
      private static final String CR = "\r"; //0x0D;

      public static final int baud9600 = 3; //Default Baud
      public static final int baud1200 = 0; //Standard Bauds
      public static final int baud2400 = 1; // |
      public static final int baud4800 = 2; // |
      public static final int baud19200 = 4; // |
      public static final int baud38400 = 5; // |
      public static final int baud57600 = 6; // |
      //dBm Levels Default = 4
      //XBEE-PRO | XBEE
      public static final int dBm10n10 = 0; // 10dBm | -10dBm
      public static final int dBm12n6 = 1; // 12dBm | - 6dBm
      public static final int dBm14n4 = 2; // 14dBm | - 4dBm
      public static final int dBm16n2 = 3; // 16dBm | - 2dBm
      public static final int dBm18n0 = 4; // 18dBm | 0dBm


      //--------------------------------VARIABLES--------------------------------


      private static int myAddr; //Node Address
      in Hex
      private static int destAddr; //Destination
      in Hex

      private static int guardTime = 3000; //Guard Time
      private static LCD lcd;

      private static int rtsPin; //RTS Pin
      private static StringBuffer buff = new StringBuffer(32); //Buffer
      for Commands

      //------------------------TEMPORARY
      VARIABLES------------------------------

      private static boolean test = true;
      private static char c = ' ';
      private static int v=0;
      private static int data; //Data variable
      private static int db; //db
      private static int i = 0; //i
      private static InputStream rx;
      private static OutputStream tx;
      int cnt=0;


      /**
      * Creates a XBee Wireless Communication Object.
      *
      * @param myAddress Current XBee Node Address
      * @param name Name for Current XBee Node
      */

      public XBee(int myAddress, String name, LCD lcd)
      {
      this.lcd = lcd;
      try {
      IntelliBrainIO.com1.setSerialPortParams(9600,
      javax.comm.SerialPort.DATABITS_8,
      javax.comm.SerialPort.STOPBITS_1,
      javax.comm.SerialPort.PARITY_NONE);
      } catch (UnsupportedCommOperationException e1) {
      // TODO Auto-generated catch block
      }

      try {
      rx = IntelliBrainIO.com1.getInputStream();
      tx = IntelliBrainIO.com1.getOutputStream();
      } catch (IOException e) {
      }

      setGuardTime(guardTime);

      setAddr((byte)myAddress); //Set Address of XBee
      setIdentify(name); //Identifier of Node
      }

      /**
      * Sends a String.
      *
      * @param toSend a string to send
      */
      public void send(String toSend)
      {
      byte[] b = toSend.getBytes();

      lcd.print (0,b);
      delay_ms(1000);
      try{
      tx.write(toSend.getBytes());
      }catch(IOException e){}
      }



      /**
      * Sends a string to a specific Node
      *
      * @param toSend a string to send
      * @param destNode Destination Address
      */
      public void send(String toSend,int destNode)
      {
      if (destNode != destAddr) setDestNode(destNode);
      send(toSend); //use generic
      send method
      }

      /**
      * Sends a StringBuffer.
      *
      * @param toSend a StringBuffer to send
      */
      public void send(StringBuffer toSend)
      {
      send(toSend.toString());
      }

      /**
      * Sends a Int
      *
      * @param toSend an Int to send
      */
      public void send(int toSend)
      {
      StringBuffer b = new StringBuffer(6);
      b.append(toSend);
      send(buff);
      }

      /**
      * Sends a StringBuffer to a specific Node.
      *
      * @param toSend a StringBuffer to send
      * @param destNode Destination Address
      */
      public void send(StringBuffer toSend,int destNode)
      {
      if (destNode != destAddr) setDestNode(destNode); //change
      destnation if
      send(toSend); //
      required
      }

      /**
      * Set the Guard Time for entering Command Mode.
      *
      * @param time Guard Time
      */
      public void setGuardTime(int time) // in ms
      {
      enterCmd();
      send("ATGT ");
      send(time);
      send(CR);
      waitOK();
      exitCmd();
      guardTime = time;
      }

      /**
      * Set the destination Node of the XBee.
      *
      * @param node Node Address see Xbee Docs for Values
      */
      public void setDestNode(int node)
      {
      enterCmd();
      send("ATDL ");
      send(node);
      send(CR);
      waitOK();
      exitCmd();
      }

      /**
      * Change communication speed of XBee.
      *
      * @param XbeeBaud 0-6, see CONSTANTS Above
      * @param uartBaud "Uart.speed#####"
      */
      public void setCommSpeed(int XbeeBaud, int uartBaud)
      {
      enterCmd();
      send("ATBD ");
      send(XbeeBaud);
      send(CR);
      waitOK();
      exitCmd();
      try {
      IntelliBrainIO.com2.setSerialPortParams(uartBaud,
      javax.comm.SerialPort.DATABITS_8,
      javax.comm.SerialPort.STOPBITS_1,
      javax.comm.SerialPort.PARITY_NONE);
      } catch (UnsupportedCommOperationException e1) {
      // TODO Auto-generated catch block
      }
      }

      /**
      * setPacketSize for Transmission
      *
      * @param packetSize See XBee docs for details
      */
      public void setPackets(int packetSize)
      {
      if (packetSize < 0xFF)
      {
      enterCmd();
      send("ATRO ");
      send(packetSize);
      send(CR);
      waitOK();
      exitCmd();
      }
      }

      /**
      * Discover nodes in network.
      *
      * Returns info to debug screen (for now)
      */
      /*
      public void discover()
      {
      enterCmd();
      send("ATND");
      send(CR);
      //System.out.println("\nDISCOVERING NODES...");
      while (!byteAvailable())
      {
      data = readChar();
      if (data == 0x0D ) //System.out.print("\n");
      else //System.out.print((char)data);
      }
      exitCmd();
      //System.out.println("\n...DISCOVERY COMPLETE");
      }
      */
      /**
      * Set Power Level for Transmission.
      *
      * @param level 0-4, see vars above for details
      */
      public void setPowerLevel(int level)
      {
      enterCmd();
      send("ATPL");
      send(level);
      send(CR);
      waitOK();
      exitCmd();
      }

      /**
      * Get RF Level of last Received Byte
      *
      * @return RF Level of last Received Byte
      */
      public int getRFLevel()
      {
      enterCmd();
      send("ATDB");
      send(CR);
      db = 0;
      data = readInt();
      while (isDigit(data))
      {
      db = 10 * db + (data - '0');
      data = readInt();
      }
      exitCmd();
      return db;
      }

      /**
      * Save settings into nonvolital memory for use on next startup.
      *
      */
      public void save()
      {
      enterCmd();
      send("ATWR");
      send(CR);
      waitOK();
      exitCmd();
      }

      /**
      * Set XBee back to default values.
      *
      */
      public void defaults()
      {
      enterCmd();
      send("ATRE");
      send(CR);
      waitOK();
      exitCmd();

      }

      /**
      * Get Guard Time from the XBee.
      *
      * @return The Guard Time Value
      */
      public int getGuardTime()
      {
      enterCmd();
      send("ATGT");
      send(CR);
      db = 0;
      data = readInt();
      while (isDigit(data))
      {
      db = 10 * db + (data - '0');
      data = readInt();
      }
      exitCmd();
      return db;
      }

      public int readInt(){
      while(!byteAvailable());
      try{
      v = rx.read();
      }catch (IOException e){}
      return v;
      }

      /**
      * Check if a byte is waiting in the RX Uart buffer.
      *
      * @return True if byte is available
      */
      public boolean byteAvailable()
      {
      int av =0;
      try{
      av = rx.available();
      } catch(IOException e){}
      return (av != 0);
      }
      /**
      * Wait for a Value and Return it.
      *
      * @return byte received via XBee and rx
      */
      public int rxByte()
      {
      return readInt();
      }
      //==========================PRIVATE METHODS
      BELOW==============================
      /**
      * Set Identify of XBee Node.
      *
      * @param name Node Name for XBee
      */
      private void setIdentify(String name)
      {
      enterCmd();
      send("ATNI ");
      send(name);
      send(CR);
      waitOK();
      exitCmd();
      }

      /**
      * Enable RTS mode on XBee.
      *
      */
      private void enableRTS()
      {
      enterCmd();
      send("ATD6 1");
      send(CR);
      waitOK();
      exitCmd();
      }

      /**
      * Set Node Address on XBee.
      *
      * @param address Node Address for XBee
      */
      private void setAddr(int address)
      {
      enterCmd();
      send("ATMY ");
      send(address);
      send(CR);
      waitOK();
      exitCmd();
      }

      /**
      * Enter Command Mode on XBee.
      *
      */
      private void enterCmd()
      {
      delay_ms(guardTime); //Command Mode Guard
      Time 1.2sec
      send("+++"); //Enter Command Mode
      delay_ms(guardTime); //Command Mode Guard
      Time 1.2 sec
      waitOK(); //wait for XBee to send 'OK'
      }
      /**
      * Exit Command Mode on XBee.
      *
      */
      private void exitCmd()
      {
      send("ATCN"); //Exit Command Mode String
      send(CR); //<CR>
      waitOK();
      }

      /**
      * Test if character is a digit.
      *
      * @param ch Character to be tested
      * @return True if ch is a digit
      */
      public static boolean isDigit(int ch)
      {
      return ch<='F' && ch>='0'; //between 0 and 15
      }

      public char readChar(){
      while(!byteAvailable());
      try{
      int val = rx.read();
      c = (char)val;
      lcd.printChar(1, cnt++, c);
      lcd.printString(0, "count= " + cnt + " " + val);
      } catch(IOException e){}
      return c;
      }

      /**
      * Wait for an 'OK' from the XBee
      *
      */
      private void waitOK(){
      lcd.printString(0, "wait");
      while (true){
      char c = readChar();
      if(c == 'O'){
      c = readChar();
      if(c == 'K') {
      readChar(); // gets the cr
      lcd.printString(0, "got OK");
      return;
      }
      }
      }
      }

      private void delay_ms(int val){
      long time = System.currentTimeMillis();
      while(System.currentTimeMillis() < (time + val)){}
      }

      }// End Class
    Your message has been successfully submitted and would be delivered to recipients shortly.