Go Down

Topic: Reading xbee data in Processing (Read 729 times) previous topic - next topic

danielle

Hi all,

I'm trying to send data from an xbee series 1 connected to an arduino Fio to the serial port using an xbee explorer usb. I want to use the data in processing. As a test I just I try sending "hello" with this simple example:
Code: [Select]

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.write(45); // send a byte with the value 45
   int bytesSent = Serial.write("hello"); //send the string "hello" and return the length of the string.
}

In the X-CTU monitor everything works fine, I can read the word 'hello'. In processing however I just get the following numbers:
104
108
111
repeated over and over.
I use this code in processing:
Code: [Select]
import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
void setup()
{
  size(200, 200);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
    println(val);
  }
}


Does anyone know what's happening here? Any help would be greatly appreciated. Best, d

Grumpy_Mike

In processing you are simply printing the value of the bytes you receive. You are not printing the ASCII code that this corresponds to. You need to do this to read the message hello.

Quote
In processing however I just get the following numbers:
104
108
111
repeated over and over.

I bet there are other numbers as well, in particular those that correspond to the letters in hello.

danielle

Hi thanks very much for your fast response. Where should I look to convert the bytes to ascII? TIA, danielle

danielle

I've got it! From the serial library in Processing I need to use this:
Code: [Select]
// Example by Tom Igoe

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
}

void draw() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
      println(myString);
    }
  }
}

More info here: http://www.processing.org/reference/libraries/serial/Serial_readStringUntil_.html


Go Up