Xbee Radio terminal program

Working with the Maxstream Xbee radios on OSX can be annoying because the screen program doesn’t print a linefeed automatically when it receives a carriage return. This Processing program works as a serial terminal program, but inserts a linefeed when it gets a carriage return.

Technorati Tags: , ,

  XBee terminal
  language: processing
  This program is a basic serial terminal program.  
  It replaces newline characters from the keyboard 
  with return characters.  It's designed for use with 
  Linux, Unix, and OSX and XBee radios, because the XBees 
  don't send newline characters back.
  created 2 Feb. 2007
  modified 24 Mar. 2007
  by Tom Igoe

import processing.serial.*;

Serial myPort;           // the serial port you're using
String portnum;          // name of the serial port
String outString = "";   // the string being sent out the serial port
String inString = "";    // the string coming in from the serial port
int receivedLines = 0;   // how many lines have been received in the serial port
int bufferedLines = 10;  // number of incoming lines to keep

void setup() {
  size(400, 300);        // window size

  // create a font with the second font available to the system:
  PFont myFont = createFont(PFont.list()[2], 14);

  // list all the serial ports:

  // based on the list of serial ports printed from the 
  //previous command, change the 0 to your port's number:
  portnum = Serial.list()[0];
  // initialize the serial port:
  myPort = new Serial(this, portnum, 9600);


void draw() {
  // clear the screen:
  // print the name of the serial port:
  text("Serial port: " + portnum, 10, 20);
  // Print out what you get:
  text("typed: " + outString, 10, 40);
  text("received:\n" + inString, 10, 80);

// This method responds to key presses when the 
// program window is active:
void keyPressed() {
  switch (key) {
    // in OSX, if the user types return, 
    // a linefeed is returned.  But to 
    // communicate with the Xbee, you want a carriage return:

  case '\n':        
    myPort.write(outString + "\r");
    outString = "";
  case 8:    // backspace
    // delete the last character in the string:
    outString = outString.substring(0, outString.length() -1);
  case '+':  // we have to send the + signs even without a return:
    // add the key to the end of the string:
    outString += key;
  case 65535:  // If the user types the shift key, don't type anything:
    // any other key typed, add it to outString:
    // add the key to the end of the string:
    outString += key;

// this method runs when bytes show up in the serial port:
void serialEvent(Serial myPort) {
  // read the next byte from the serial port:
  int inByte = myPort.read();
  // add it to  inString:
  inString += char(inByte);
  if (inByte == '\r') {
    // if the byte is a carriage return, print 
    // a newline and carriage return:
    inString += '\n';
    // count the number of newlines:
    // if there are more than 10 lines, delete the first one:
    if (receivedLines >  bufferedLines) {
// deletes the top line of inString so that it all fits on the screen:
void deleteFirstLine() { 
  // find the first newline:
  int firstChar = inString.indexOf('\n');
  // delete it:
  inString= inString.substring(firstChar+1);