11-7

Warning! This code does not work. Fix it!

This code is intended to read an Xbee radio, and check signal strength whenever it gets a string terminated by a carriage return (\r)

import processing.serial.*;

Serial myPort;

boolean dataMode = true;    // whether we're in data mode or command mode
String commandString = "";      // reply we got from an AT command
String dataString = "";
int guardTime = 10;
boolean stringReturned = false;

void setup() {
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  if (dataMode && stringReturned) {
    println("datamode = " + dataMode);
    println("The string was " + dataString);
    checkSignalStrength(); 
  }
}

void keyReleased() {
  switch (key) {
  case '=':
    myPort.write("+++");
    break;
  case 'a':
    myPort.write("AT\r");
    break;
  case 'd':
    myPort.write("ATDH, DL\r");
    break;
  case 'i':
    myPort.write("ATID\r");
    break;
  case 'm':
    myPort.write("ATMY\r");
    break;
  case 'r':
    myPort.write("ATDB\r");
    break;
  case 's':
    myPort.write("ATSH, SL\r");
  case 'v':
    myPort.write("ATVL\r");
  }
  dataMode = false;
}

void serialEvent(Serial myPort) {

  int inChar = myPort.read();
  if (dataMode) { 
    //print(char(inChar));
    // if we get a byte here, it's from someone else.
    // go ahead and measure the signal strength
    // you're in command mode now: read the command 
    if (inChar == '\r' ) {
      stringReturned = true;
    } 
    else {
      if (stringReturned) {
      clearString(dataString);
      }
      dataString += char(inChar); 
    }
  } 
  else {
    // you're in command mode now: read the command 
    if (inChar == '\r' ) {
      stringReturned = true;
    } 
    else {
      if (stringReturned) {
        clearString(commandString);
      }
      commandString += char(inChar); 
    }
  }
}


void checkSignalStrength() {
  println("Checking");
  // send +++
  myPort.write("+++");
  clearString(commandString);
  dataMode = false;
  boolean waitingForResponse = true;
  // wait for reply
  println("It's " + commandString.equals(""));
  while (waitingForResponse) {
    while (!commandString.equals("OK")) {
      // wait 
      delay(1);
      println("command = " + commandString);

    }
    waitingForResponse = false;
  }
  // send atdb
  println("We got here!!!!");
  myPort.write("ATDB\r");
  clearString(commandString);
  // wait for reply
  waitingForResponse = true;


}


void clearString(String thisString) {
  thisString = "";
  stringReturned = false;

}

  Edit | View | History | Print | Recent Changes | Search Page last modified on November 07, 2006, at 11:27 AM